# [PATCH] wincred: fix get credential if username has @

5 messages from 2014-11-12 to 2015-01-26. Participants: Aleksey Vasenev, Junio C Hamano, Erik Faye-Lund.
Thread: https://gitlist.dev/t/37946

## Aleksey Vasenev, 2014-11-12 22:52

Subject: [PATCH] wincred: fix get credential if username has @
Message-ID: <1415832746-5116-1-git-send-email-margtu-fivt@ya.ru>
URL: https://gitlist.dev/e/1415832746-5116-1-git-send-email-margtu-fivt%40ya.ru

```
Signed-off-by: Aleksey Vasenev <margtu-fivt@ya.ru>
---
 .../credential/wincred/git-credential-wincred.c    | 25 +++++++++++++++++++---
 1 file changed, 22 insertions(+), 3 deletions(-)

diff --git a/contrib/credential/wincred/git-credential-wincred.c b/contrib/credential/wincred/git-credential-wincred.c
index a1d38f0..0229443 100644
--- a/contrib/credential/wincred/git-credential-wincred.c
+++ b/contrib/credential/wincred/git-credential-wincred.c
@@ -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)
+
+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);
 
@@ -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;
@@ -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"");
 }
-- 
1.9.4.msysgit.2

```

## Aleksey Vasenev, 2014-11-19 21:47

Subject: [PATCH] wincred: fix get credential if username has @
Message-ID: <1416433652-6840-1-git-send-email-margtu-fivt@ya.ru>
URL: https://gitlist.dev/e/1416433652-6840-1-git-send-email-margtu-fivt%40ya.ru
In-Reply-To: <1415832746-5116-1-git-send-email-margtu-fivt@ya.ru>

```
Signed-off-by: Aleksey Vasenev <margtu-fivt@ya.ru>
---
 .../credential/wincred/git-credential-wincred.c    | 25 +++++++++++++++++++---
 1 file changed, 22 insertions(+), 3 deletions(-)

diff --git a/contrib/credential/wincred/git-credential-wincred.c b/contrib/credential/wincred/git-credential-wincred.c
index a1d38f0..0061340 100644
--- a/contrib/credential/wincred/git-credential-wincred.c
+++ b/contrib/credential/wincred/git-credential-wincred.c
@@ -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);
 
@@ -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;
@@ -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"");
 }
-- 
1.9.4.msysgit.2

```

## Junio C Hamano, 2014-11-19 22:41

Subject: Re: [PATCH] wincred: fix get credential if username has @
Message-ID: <xmqq1toyreyh.fsf@gitster.dls.corp.google.com>
URL: https://gitlist.dev/e/xmqq1toyreyh.fsf%40gitster.dls.corp.google.com
In-Reply-To: <1416433652-6840-1-git-send-email-margtu-fivt@ya.ru>

```
Aleksey Vasenev <margtu-fivt@ya.ru> writes:

>> To: git@vger.kernel.org
>> Cc: Junio C Hamano <gitster@pobox.com>, Aleksey Vasenev <margtu-fivt@ya.ru>

Sorry, but I am hardly qualified to review this one, especially
without any log message that explains what breaks and how it breaks
with the current code, which may lead the reader to understand how
the updated code fixes the issue.  Cc'ing me does not help us very
much.

    $ git shortlog --no-merges -n contrib/credential/wincred/

gives me a few names who may be able to give us some inputs, so I'll
Cc them.

Thanks.

> Signed-off-by: Aleksey Vasenev <margtu-fivt@ya.ru>
> ---
>  .../credential/wincred/git-credential-wincred.c    | 25 +++++++++++++++++++---
>  1 file changed, 22 insertions(+), 3 deletions(-)
>
> diff --git a/contrib/credential/wincred/git-credential-wincred.c b/contrib/credential/wincred/git-credential-wincred.c
> index a1d38f0..0061340 100644
> --- a/contrib/credential/wincred/git-credential-wincred.c
> +++ b/contrib/credential/wincred/git-credential-wincred.c
> @@ -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);
>  
> @@ -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;
> @@ -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"");
>  }

```

## Erik Faye-Lund, 2015-01-25 21:12

Subject: Re: [PATCH] wincred: fix get credential if username has @
Message-ID: <CABPQNSaNHU=eJtK=s4P=p0hesEB0fxnwRo1zx5sMDo7zXgYM_A@mail.gmail.com>
URL: https://gitlist.dev/e/CABPQNSaNHU%3DeJtK%3Ds4P%3Dp0hesEB0fxnwRo1zx5sMDo7zXgYM_A%40mail.gmail.com
In-Reply-To: <xmqq1toyreyh.fsf@gitster.dls.corp.google.com>

```
Sorry for the extremely delayed reply, I had a bug in my mail-filters.
Hopefully fixed now.

On Wed, Nov 19, 2014 at 11:41 PM, Junio C Hamano <gitster@pobox.com> wrote:
> Aleksey Vasenev <margtu-fivt@ya.ru> writes:
>
>>> To: git@vger.kernel.org
>>> Cc: Junio C Hamano <gitster@pobox.com>, Aleksey Vasenev <margtu-fivt@ya.ru>
>
> Sorry, but I am hardly qualified to review this one, especially
> without any log message that explains what breaks and how it breaks
> with the current code, which may lead the reader to understand how
> the updated code fixes the issue.  Cc'ing me does not help us very
> much.
>
>     $ git shortlog --no-merges -n contrib/credential/wincred/
>
> gives me a few names who may be able to give us some inputs, so I'll
> Cc them.
>
> Thanks.

I noticed the breakage myself around the same time, and posted about it here:

https://groups.google.com/d/msg/msysgit/YVuCqmwwRyY/HULHj5OoE88J

Unfortunately, it stopped there.

>> Signed-off-by: Aleksey Vasenev <margtu-fivt@ya.ru>
>> ---
>>  .../credential/wincred/git-credential-wincred.c    | 25 +++++++++++++++++++---
>>  1 file changed, 22 insertions(+), 3 deletions(-)
>>
>> diff --git a/contrib/credential/wincred/git-credential-wincred.c b/contrib/credential/wincred/git-credential-wincred.c
>> index a1d38f0..0061340 100644
>> --- a/contrib/credential/wincred/git-credential-wincred.c
>> +++ b/contrib/credential/wincred/git-credential-wincred.c
>> @@ -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;
>> +}
>> +

Ugh, there's no wcsrstr? I guess this is a reasonable way to emulate it...

>> +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);
>>
>> @@ -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;
>> @@ -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"");
>>  }

Looks reasonable enough to me.

Acked-by: Erik Faye-Lund <kusmabite@gmail.com>

```

## Junio C Hamano, 2015-01-26 04:25

Subject: Re: [PATCH] wincred: fix get credential if username has @
Message-ID: <xmqqy4oqmalq.fsf@gitster.dls.corp.google.com>
URL: https://gitlist.dev/e/xmqqy4oqmalq.fsf%40gitster.dls.corp.google.com
In-Reply-To: <CABPQNSaNHU=eJtK=s4P=p0hesEB0fxnwRo1zx5sMDo7zXgYM_A@mail.gmail.com>

```
Erik Faye-Lund <kusmabite@gmail.com> writes:

> Sorry for the extremely delayed reply, I had a bug in my mail-filters.
> Hopefully fixed now.
>
> ...
>
> I noticed the breakage myself around the same time, and posted about it here:
>
> https://groups.google.com/d/msg/msysgit/YVuCqmwwRyY/HULHj5OoE88J
>
> Unfortunately, it stopped there.
> ...
>
> Looks reasonable enough to me.
>
> Acked-by: Erik Faye-Lund <kusmabite@gmail.com>

OK.  Will apply to my tree, so future Git for Windows would
hopefully have it as part of its upstream updates.

Thanks.

```
