wincred: fix get credential if username has "@"
Such a username with "@" in it isn't all that unusual these days. cf. https://groups.google.com/forum/#!msg/msysgit/YVuCqmwwRyY/HULHj5OoE88J Signed-off-by: Aleksey Vasenev <margtu-fivt@ya.ru> Acked-by: Erik Faye-Lund <kusmabite@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Aleksey Vasenev committed
Nov 20, 2014 at 00:47 UTC
13d261e53abdfe0ee61eb04b483a01509c0b43d2
1 file changed
+22
-3
contrib/credential/wincred/git-credential-wincred.c
+22
-3
@@ -111,14 +111,23 @@ static void write_item(const char *what, LPCWSTR wbuf, int wlen)
111
* Match an (optional) expected string and a delimiter in the target string,
112
* consuming the matched text by updating the target pointer.
113
*/
114
-static int match_part(LPCWSTR *ptarget, LPCWSTR want, LPCWSTR delim)
114
+
115
+static LPCWSTR wcsstr_last(LPCWSTR str, LPCWSTR find)
116
+{
117
+ LPCWSTR res = NULL, pos;
118
+ for (pos = wcsstr(str, find); pos; pos = wcsstr(pos + 1, find))
119
+ res = pos;
120
+ return res;
121
+}
122
+
123
+static int match_part_with_last(LPCWSTR *ptarget, LPCWSTR want, LPCWSTR delim, int last)
124
{
125
LPCWSTR delim_pos, start = *ptarget;
126
int len;
127
128
/* find start of delimiter (or end-of-string if delim is empty) */
129
if (*delim)
121
- delim_pos = wcsstr(start, delim);
130
+ delim_pos = last ? wcsstr_last(start, delim) : wcsstr(start, delim);
131
else
132
delim_pos = start + wcslen(start);
133
@@ -138,6 +147,16 @@ static int match_part(LPCWSTR *ptarget, LPCWSTR want, LPCWSTR delim)
147
return !want || (!wcsncmp(want, start, len) && !want[len]);
148
}
149
150
+static int match_part(LPCWSTR *ptarget, LPCWSTR want, LPCWSTR delim)
151
+{
152
+ return match_part_with_last(ptarget, want, delim, 0);
153
+}
154
+
155
+static int match_part_last(LPCWSTR *ptarget, LPCWSTR want, LPCWSTR delim)
156
+{
157
+ return match_part_with_last(ptarget, want, delim, 1);
158
+}
159
+
160
static int match_cred(const CREDENTIALW *cred)
161
{
162
LPCWSTR target = cred->TargetName;
@@ -146,7 +165,7 @@ static int match_cred(const CREDENTIALW *cred)
165
166
return match_part(&target, L"git", L":") &&
167
match_part(&target, protocol, L"://") &&
149
- match_part(&target, wusername, L"@") &&
168
+ match_part_last(&target, wusername, L"@") &&
169
match_part(&target, host, L"/") &&
170
match_part(&target, path, L"");
171
}