# [PATCH] commit.c: use strchrnul() to scan for one line

3 messages from 2016-05-15 to 2016-05-16. Participants: Junio C Hamano, Johannes Schindelin.
Thread: https://gitlist.dev/t/42330

## Junio C Hamano, 2016-05-15 22:23

Subject: [PATCH] commit.c: use strchrnul() to scan for one line
Message-ID: <xmqq37pj55bg.fsf@gitster.mtv.corp.google.com>
URL: https://gitlist.dev/e/xmqq37pj55bg.fsf%40gitster.mtv.corp.google.com

```

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 commit.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/commit.c b/commit.c
index 3f4f371..1f9ee8a 100644
--- a/commit.c
+++ b/commit.c
@@ -415,8 +415,7 @@ int find_commit_subject(const char *commit_buffer, const char **subject)
 		p++;
 	if (*p) {
 		p += 2;
-		for (eol = p; *eol && *eol != '\n'; eol++)
-			; /* do nothing */
+		eol = strchrnul(p, '\n');
 	} else
 		eol = p;
 
-- 
2.8.2-748-gfb85f76

```

## Johannes Schindelin, 2016-05-16 06:12

Subject: Re: [PATCH] commit.c: use strchrnul() to scan for one line
Message-ID: <alpine.DEB.2.20.1605160810020.3303@virtualbox>
URL: https://gitlist.dev/e/alpine.DEB.2.20.1605160810020.3303%40virtualbox
In-Reply-To: <xmqq37pj55bg.fsf@gitster.mtv.corp.google.com>

```
Hi Junio,

On Sun, 15 May 2016, Junio C Hamano wrote:

> diff --git a/commit.c b/commit.c
> index 3f4f371..1f9ee8a 100644
> --- a/commit.c
> +++ b/commit.c
> @@ -415,8 +415,7 @@ int find_commit_subject(const char *commit_buffer, const char **subject)
>  		p++;
>  	if (*p) {
>  		p += 2;
> -		for (eol = p; *eol && *eol != '\n'; eol++)
> -			; /* do nothing */
> +		eol = strchrnul(p, '\n');
>  	} else
>  		eol = p;

ACK. This was my fault, when I introduced the code in 9509af68 (Make
git-revert & git-cherry-pick a builtin, 2007-03-01). To be fair,
strchrnul() was introduced only later, in 659c69c (Add strchrnul(),
2007-11-09).

Ciao,
Dscho

```

## Junio C Hamano, 2016-05-16 15:57

Subject: Re: [PATCH] commit.c: use strchrnul() to scan for one line
Message-ID: <xmqq1t523shk.fsf@gitster.mtv.corp.google.com>
URL: https://gitlist.dev/e/xmqq1t523shk.fsf%40gitster.mtv.corp.google.com
In-Reply-To: <alpine.DEB.2.20.1605160810020.3303@virtualbox>

```
Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:

> On Sun, 15 May 2016, Junio C Hamano wrote:
>
>> diff --git a/commit.c b/commit.c
>> index 3f4f371..1f9ee8a 100644
>> --- a/commit.c
>> +++ b/commit.c
>> @@ -415,8 +415,7 @@ int find_commit_subject(const char *commit_buffer, const char **subject)
>>  		p++;
>>  	if (*p) {
>>  		p += 2;
>> -		for (eol = p; *eol && *eol != '\n'; eol++)
>> -			; /* do nothing */
>> +		eol = strchrnul(p, '\n');
>>  	} else
>>  		eol = p;
>
> ACK. This was my fault, when I introduced the code in 9509af68 (Make
> git-revert & git-cherry-pick a builtin, 2007-03-01). To be fair,
> strchrnul() was introduced only later, in 659c69c (Add strchrnul(),
> 2007-11-09).

Oh, there is no fault.

I was reading through attr.c to see how bad a work to revamp
attribute lookup would look like, found a hand-rolled strchrnul()
that predates the widespread use of the function, and looked for
similar patterns while I was updating it.  As there were many false
positives (i.e. "Yes, this loop is looking for the end of line, but
it does something else to the characters on the line while doing so,
so it cannot become strchrnul()") that I needed eyeballing in order
to reject and avoid incorrect conversion, it is very much appreciated
that you double-checked this one that I spotted.

Thanks.

```
