Skip to content

Commit

Permalink
Merge branch 'av/wincred-with-at-in-username-fix'
Browse files Browse the repository at this point in the history
The credential helper for Windows (in contrib/) used to mishandle
a user name with an at-sign in it.

* av/wincred-with-at-in-username-fix:
  wincred: fix get credential if username has "@"
  • Loading branch information
gitster committed Feb 18, 2015
2 parents 3188ab3 + 13d261e commit c2d081c
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions contrib/credential/wincred/git-credential-wincred.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,23 @@ static void write_item(const char *what, LPCWSTR wbuf, int wlen)
* Match an (optional) expected string and a delimiter in the target string,
* consuming the matched text by updating the target pointer.
*/
static int match_part(LPCWSTR *ptarget, LPCWSTR want, LPCWSTR delim)

static LPCWSTR wcsstr_last(LPCWSTR str, LPCWSTR find)
{
LPCWSTR res = NULL, pos;
for (pos = wcsstr(str, find); pos; pos = wcsstr(pos + 1, find))
res = pos;
return res;
}

static int match_part_with_last(LPCWSTR *ptarget, LPCWSTR want, LPCWSTR delim, int last)
{
LPCWSTR delim_pos, start = *ptarget;
int len;

/* find start of delimiter (or end-of-string if delim is empty) */
if (*delim)
delim_pos = wcsstr(start, delim);
delim_pos = last ? wcsstr_last(start, delim) : wcsstr(start, delim);
else
delim_pos = start + wcslen(start);

Expand All @@ -138,6 +147,16 @@ static int match_part(LPCWSTR *ptarget, LPCWSTR want, LPCWSTR delim)
return !want || (!wcsncmp(want, start, len) && !want[len]);
}

static int match_part(LPCWSTR *ptarget, LPCWSTR want, LPCWSTR delim)
{
return match_part_with_last(ptarget, want, delim, 0);
}

static int match_part_last(LPCWSTR *ptarget, LPCWSTR want, LPCWSTR delim)
{
return match_part_with_last(ptarget, want, delim, 1);
}

static int match_cred(const CREDENTIALW *cred)
{
LPCWSTR target = cred->TargetName;
Expand All @@ -146,7 +165,7 @@ static int match_cred(const CREDENTIALW *cred)

return match_part(&target, L"git", L":") &&
match_part(&target, protocol, L"://") &&
match_part(&target, wusername, L"@") &&
match_part_last(&target, wusername, L"@") &&
match_part(&target, host, L"/") &&
match_part(&target, path, L"");
}
Expand Down

0 comments on commit c2d081c

Please sign in to comment.