# [PATCH 1/2] commit: be more precise when searching for headers

9 messages from 2017-02-25 to 2017-02-27. Participants: René Scharfe, Jeff King, Junio C Hamano, Jakub Narębski.
Thread: https://gitlist.dev/t/45228

## René Scharfe, 2017-02-25 19:21

Subject: [PATCH 1/2] commit: be more precise when searching for headers
Message-ID: <23989e76-24ba-90a4-91a9-9f66bfccb7c9@web.de>
URL: https://gitlist.dev/e/23989e76-24ba-90a4-91a9-9f66bfccb7c9%40web.de

```
Search for a space character only within the current line in
read_commit_extra_header_lines() instead of searching in the whole
buffer (and possibly beyond, if it's not NUL-terminated) and then
discarding any results after the end of the current line.

Signed-off-by: Rene Scharfe <l.s.r@web.de>
---
 commit.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/commit.c b/commit.c
index 2cf85158b4..173c6d3818 100644
--- a/commit.c
+++ b/commit.c
@@ -1354,8 +1354,8 @@ static struct commit_extra_header *read_commit_extra_header_lines(
 		strbuf_reset(&buf);
 		it = NULL;
 
-		eof = strchr(line, ' ');
-		if (next <= eof)
+		eof = memchr(line, ' ', next - line);
+		if (!eof)
 			eof = next;
 
 		if (standard_header_field(line, eof - line) ||
-- 
2.12.0


```

## René Scharfe, 2017-02-25 19:27

Subject: [PATCH 2/2] commit: don't check for space twice when looking for header
Message-ID: <b1d5c882-38b8-dd2d-2e5f-aafb8dfada81@web.de>
URL: https://gitlist.dev/e/b1d5c882-38b8-dd2d-2e5f-aafb8dfada81%40web.de
In-Reply-To: <23989e76-24ba-90a4-91a9-9f66bfccb7c9@web.de>

```
Both standard_header_field() and excluded_header_field() check if
there's a space after the buffer that's handed to them.  We already
check in the caller if that space is present.  Don't bother calling
the functions if it's missing, as they are guaranteed to return 0 in
that case, and remove the now redundant checks from them.

Signed-off-by: Rene Scharfe <l.s.r@web.de>
---
 commit.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)

diff --git a/commit.c b/commit.c
index 173c6d3818..fab8269731 100644
--- a/commit.c
+++ b/commit.c
@@ -1308,11 +1308,11 @@ void for_each_mergetag(each_mergetag_fn fn, struct commit *commit, void *data)
 
 static inline int standard_header_field(const char *field, size_t len)
 {
-	return ((len == 4 && !memcmp(field, "tree ", 5)) ||
-		(len == 6 && !memcmp(field, "parent ", 7)) ||
-		(len == 6 && !memcmp(field, "author ", 7)) ||
-		(len == 9 && !memcmp(field, "committer ", 10)) ||
-		(len == 8 && !memcmp(field, "encoding ", 9)));
+	return ((len == 4 && !memcmp(field, "tree", 4)) ||
+		(len == 6 && !memcmp(field, "parent", 6)) ||
+		(len == 6 && !memcmp(field, "author", 6)) ||
+		(len == 9 && !memcmp(field, "committer", 9)) ||
+		(len == 8 && !memcmp(field, "encoding", 8)));
 }
 
 static int excluded_header_field(const char *field, size_t len, const char **exclude)
@@ -1322,8 +1322,7 @@ static int excluded_header_field(const char *field, size_t len, const char **exc
 
 	while (*exclude) {
 		size_t xlen = strlen(*exclude);
-		if (len == xlen &&
-		    !memcmp(field, *exclude, xlen) && field[xlen] == ' ')
+		if (len == xlen && !memcmp(field, *exclude, xlen))
 			return 1;
 		exclude++;
 	}
@@ -1357,9 +1356,8 @@ static struct commit_extra_header *read_commit_extra_header_lines(
 		eof = memchr(line, ' ', next - line);
 		if (!eof)
 			eof = next;
-
-		if (standard_header_field(line, eof - line) ||
-		    excluded_header_field(line, eof - line, exclude))
+		else if (standard_header_field(line, eof - line) ||
+			 excluded_header_field(line, eof - line, exclude))
 			continue;
 
 		it = xcalloc(1, sizeof(*it));
-- 
2.12.0


```

## Jeff King, 2017-02-25 20:12

Subject: Re: [PATCH 1/2] commit: be more precise when searching for headers
Message-ID: <20170225201238.wkujdwg4jyvovelm@sigill.intra.peff.net>
URL: https://gitlist.dev/e/20170225201238.wkujdwg4jyvovelm%40sigill.intra.peff.net
In-Reply-To: <23989e76-24ba-90a4-91a9-9f66bfccb7c9@web.de>

```
On Sat, Feb 25, 2017 at 08:21:52PM +0100, René Scharfe wrote:

> Search for a space character only within the current line in
> read_commit_extra_header_lines() instead of searching in the whole
> buffer (and possibly beyond, if it's not NUL-terminated) and then
> discarding any results after the end of the current line.
> [...]
> -		eof = strchr(line, ' ');
> -		if (next <= eof)
> +		eof = memchr(line, ' ', next - line);
> +		if (!eof)
>  			eof = next;

Nice. More efficient, and I think the intent is more clear.

-Peff

```

## Jeff King, 2017-02-25 20:15

Subject: Re: [PATCH 2/2] commit: don't check for space twice when looking for header
Message-ID: <20170225201522.uan52fwey6zjosym@sigill.intra.peff.net>
URL: https://gitlist.dev/e/20170225201522.uan52fwey6zjosym%40sigill.intra.peff.net
In-Reply-To: <b1d5c882-38b8-dd2d-2e5f-aafb8dfada81@web.de>

```
On Sat, Feb 25, 2017 at 08:27:40PM +0100, René Scharfe wrote:

> Both standard_header_field() and excluded_header_field() check if
> there's a space after the buffer that's handed to them.  We already
> check in the caller if that space is present.  Don't bother calling
> the functions if it's missing, as they are guaranteed to return 0 in
> that case, and remove the now redundant checks from them.

Makes sense, and I couldn't spot any errors in your logic or in the
code.

>  static inline int standard_header_field(const char *field, size_t len)
>  {
> -	return ((len == 4 && !memcmp(field, "tree ", 5)) ||
> -		(len == 6 && !memcmp(field, "parent ", 7)) ||
> -		(len == 6 && !memcmp(field, "author ", 7)) ||
> -		(len == 9 && !memcmp(field, "committer ", 10)) ||
> -		(len == 8 && !memcmp(field, "encoding ", 9)));
> +	return ((len == 4 && !memcmp(field, "tree", 4)) ||
> +		(len == 6 && !memcmp(field, "parent", 6)) ||
> +		(len == 6 && !memcmp(field, "author", 6)) ||
> +		(len == 9 && !memcmp(field, "committer", 9)) ||
> +		(len == 8 && !memcmp(field, "encoding", 8)));

Unrelated, but this could probably be spelled with a macro and strlen()
to avoid the magic numbers. It would probably be measurably slower for a
compiler which doesn't pre-compute strlen() on a string literal, though.

-Peff

```

## René Scharfe, 2017-02-25 21:39

Subject: Re: [PATCH 2/2] commit: don't check for space twice when looking for header
Message-ID: <6462e74c-1ee7-7ed0-0695-9889df803943@web.de>
URL: https://gitlist.dev/e/6462e74c-1ee7-7ed0-0695-9889df803943%40web.de
In-Reply-To: <20170225201522.uan52fwey6zjosym@sigill.intra.peff.net>

```
Am 25.02.2017 um 21:15 schrieb Jeff King:
> On Sat, Feb 25, 2017 at 08:27:40PM +0100, René Scharfe wrote:
>
>> Both standard_header_field() and excluded_header_field() check if
>> there's a space after the buffer that's handed to them.  We already
>> check in the caller if that space is present.  Don't bother calling
>> the functions if it's missing, as they are guaranteed to return 0 in
>> that case, and remove the now redundant checks from them.
>
> Makes sense, and I couldn't spot any errors in your logic or in the
> code.

Thanks for checking!

>>  static inline int standard_header_field(const char *field, size_t len)
>>  {
>> -	return ((len == 4 && !memcmp(field, "tree ", 5)) ||
>> -		(len == 6 && !memcmp(field, "parent ", 7)) ||
>> -		(len == 6 && !memcmp(field, "author ", 7)) ||
>> -		(len == 9 && !memcmp(field, "committer ", 10)) ||
>> -		(len == 8 && !memcmp(field, "encoding ", 9)));
>> +	return ((len == 4 && !memcmp(field, "tree", 4)) ||
>> +		(len == 6 && !memcmp(field, "parent", 6)) ||
>> +		(len == 6 && !memcmp(field, "author", 6)) ||
>> +		(len == 9 && !memcmp(field, "committer", 9)) ||
>> +		(len == 8 && !memcmp(field, "encoding", 8)));
>
> Unrelated, but this could probably be spelled with a macro and strlen()
> to avoid the magic numbers. It would probably be measurably slower for a
> compiler which doesn't pre-compute strlen() on a string literal, though.

sizeof(string_constant) - 1 might be a better choice here than strlen().

René

```

## Jeff King, 2017-02-25 21:51

Subject: Re: [PATCH 2/2] commit: don't check for space twice when looking for header
Message-ID: <20170225215144.hb7huqmv3seaoaqf@sigill.intra.peff.net>
URL: https://gitlist.dev/e/20170225215144.hb7huqmv3seaoaqf%40sigill.intra.peff.net
In-Reply-To: <6462e74c-1ee7-7ed0-0695-9889df803943@web.de>

```
On Sat, Feb 25, 2017 at 10:39:29PM +0100, René Scharfe wrote:

> > > +		(len == 8 && !memcmp(field, "encoding", 8)));
> > 
> > Unrelated, but this could probably be spelled with a macro and strlen()
> > to avoid the magic numbers. It would probably be measurably slower for a
> > compiler which doesn't pre-compute strlen() on a string literal, though.
> 
> sizeof(string_constant) - 1 might be a better choice here than strlen().

Yeah. If you use a macro, that works. If it's an inline function you'd
need strlen(). That's a tradeoff we've already made in skip_prefix_mem()
and strip_suffix(), but it's not like we expect this list to grow much,
so it may not be worth fussing with.

-Peff

```

## Junio C Hamano, 2017-02-27 19:18

Subject: Re: [PATCH 1/2] commit: be more precise when searching for headers
Message-ID: <xmqq8torxx15.fsf@gitster.mtv.corp.google.com>
URL: https://gitlist.dev/e/xmqq8torxx15.fsf%40gitster.mtv.corp.google.com
In-Reply-To: <23989e76-24ba-90a4-91a9-9f66bfccb7c9@web.de>

```
René Scharfe <l.s.r@web.de> writes:

> Search for a space character only within the current line in
> read_commit_extra_header_lines() instead of searching in the whole
> buffer (and possibly beyond, if it's not NUL-terminated) and then
> discarding any results after the end of the current line.
>
> Signed-off-by: Rene Scharfe <l.s.r@web.de>
> ---
>  commit.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)

Makes sense.

> diff --git a/commit.c b/commit.c
> index 2cf85158b4..173c6d3818 100644
> --- a/commit.c
> +++ b/commit.c
> @@ -1354,8 +1354,8 @@ static struct commit_extra_header *read_commit_extra_header_lines(
>  		strbuf_reset(&buf);
>  		it = NULL;
>  
> -		eof = strchr(line, ' ');
> -		if (next <= eof)
> +		eof = memchr(line, ' ', next - line);
> +		if (!eof)
>  			eof = next;
>  
>  		if (standard_header_field(line, eof - line) ||

```

## Jakub Narębski, 2017-02-27 22:27

Subject: Re: [PATCH 2/2] commit: don't check for space twice when looking for header
Message-ID: <1dd0884c-032c-fb04-67f6-8b181fd65eea@gmail.com>
URL: https://gitlist.dev/e/1dd0884c-032c-fb04-67f6-8b181fd65eea%40gmail.com
In-Reply-To: <b1d5c882-38b8-dd2d-2e5f-aafb8dfada81@web.de>

```
W dniu 25.02.2017 o 20:27, René Scharfe pisze:
> Both standard_header_field() and excluded_header_field() check if
> there's a space after the buffer that's handed to them.  We already
> check in the caller if that space is present.  Don't bother calling
> the functions if it's missing, as they are guaranteed to return 0 in
> that case, and remove the now redundant checks from them.
> 
> Signed-off-by: Rene Scharfe <l.s.r@web.de>
> ---
>  commit.c | 18 ++++++++----------
>  1 file changed, 8 insertions(+), 10 deletions(-)
> 
> diff --git a/commit.c b/commit.c
> index 173c6d3818..fab8269731 100644
> --- a/commit.c
> +++ b/commit.c
> @@ -1308,11 +1308,11 @@ void for_each_mergetag(each_mergetag_fn fn, struct commit *commit, void *data)
>  
>  static inline int standard_header_field(const char *field, size_t len)
>  {
> -	return ((len == 4 && !memcmp(field, "tree ", 5)) ||
> -		(len == 6 && !memcmp(field, "parent ", 7)) ||
> -		(len == 6 && !memcmp(field, "author ", 7)) ||
> -		(len == 9 && !memcmp(field, "committer ", 10)) ||
> -		(len == 8 && !memcmp(field, "encoding ", 9)));
> +	return ((len == 4 && !memcmp(field, "tree", 4)) ||
> +		(len == 6 && !memcmp(field, "parent", 6)) ||
> +		(len == 6 && !memcmp(field, "author", 6)) ||
> +		(len == 9 && !memcmp(field, "committer", 9)) ||
> +		(len == 8 && !memcmp(field, "encoding", 8)));

I agree (for what it is worth from me) with the rest of changes,
but I think current code is better self-documenting for this
function.

>  }
>  
>  static int excluded_header_field(const char *field, size_t len, const char **exclude)
> @@ -1322,8 +1322,7 @@ static int excluded_header_field(const char *field, size_t len, const char **exc
>  
>  	while (*exclude) {
>  		size_t xlen = strlen(*exclude);
> -		if (len == xlen &&
> -		    !memcmp(field, *exclude, xlen) && field[xlen] == ' ')
> +		if (len == xlen && !memcmp(field, *exclude, xlen))
>  			return 1;
>  		exclude++;
>  	}
> @@ -1357,9 +1356,8 @@ static struct commit_extra_header *read_commit_extra_header_lines(
>  		eof = memchr(line, ' ', next - line);
>  		if (!eof)
>  			eof = next;
> -
> -		if (standard_header_field(line, eof - line) ||
> -		    excluded_header_field(line, eof - line, exclude))
> +		else if (standard_header_field(line, eof - line) ||
> +			 excluded_header_field(line, eof - line, exclude))
>  			continue;
>  
>  		it = xcalloc(1, sizeof(*it));
> 


```

## René Scharfe, 2017-02-27 22:54

Subject: Re: [PATCH 2/2] commit: don't check for space twice when looking for header
Message-ID: <cd9f3874-7428-9906-f805-31fe4661d4b6@web.de>
URL: https://gitlist.dev/e/cd9f3874-7428-9906-f805-31fe4661d4b6%40web.de
In-Reply-To: <1dd0884c-032c-fb04-67f6-8b181fd65eea@gmail.com>

```
Am 27.02.2017 um 23:27 schrieb Jakub Narębski:
> W dniu 25.02.2017 o 20:27, René Scharfe pisze:
>> Both standard_header_field() and excluded_header_field() check if
>> there's a space after the buffer that's handed to them.  We already
>> check in the caller if that space is present.  Don't bother calling
>> the functions if it's missing, as they are guaranteed to return 0 in
>> that case, and remove the now redundant checks from them.
>>
>> Signed-off-by: Rene Scharfe <l.s.r@web.de>
>> ---
>>  commit.c | 18 ++++++++----------
>>  1 file changed, 8 insertions(+), 10 deletions(-)
>>
>> diff --git a/commit.c b/commit.c
>> index 173c6d3818..fab8269731 100644
>> --- a/commit.c
>> +++ b/commit.c
>> @@ -1308,11 +1308,11 @@ void for_each_mergetag(each_mergetag_fn fn, struct commit *commit, void *data)
>>
>>  static inline int standard_header_field(const char *field, size_t len)
>>  {
>> -	return ((len == 4 && !memcmp(field, "tree ", 5)) ||
>> -		(len == 6 && !memcmp(field, "parent ", 7)) ||
>> -		(len == 6 && !memcmp(field, "author ", 7)) ||
>> -		(len == 9 && !memcmp(field, "committer ", 10)) ||
>> -		(len == 8 && !memcmp(field, "encoding ", 9)));
>> +	return ((len == 4 && !memcmp(field, "tree", 4)) ||
>> +		(len == 6 && !memcmp(field, "parent", 6)) ||
>> +		(len == 6 && !memcmp(field, "author", 6)) ||
>> +		(len == 9 && !memcmp(field, "committer", 9)) ||
>> +		(len == 8 && !memcmp(field, "encoding", 8)));
>
> I agree (for what it is worth from me) with the rest of changes,
> but I think current code is better self-documenting for this
> function.

Having a function that is given a buffer/length pair and accessing the 
byte after it raises questions, though. :)

Nicer than keeping the space would be to use excluded_header_field() for 
standard headers as well as a next step, I think -- but that would be a 
bit slower.

>
>>  }
>>
>>  static int excluded_header_field(const char *field, size_t len, const char **exclude)
>> @@ -1322,8 +1322,7 @@ static int excluded_header_field(const char *field, size_t len, const char **exc
>>
>>  	while (*exclude) {
>>  		size_t xlen = strlen(*exclude);
>> -		if (len == xlen &&
>> -		    !memcmp(field, *exclude, xlen) && field[xlen] == ' ')
>> +		if (len == xlen && !memcmp(field, *exclude, xlen))
>>  			return 1;
>>  		exclude++;
>>  	}
>> @@ -1357,9 +1356,8 @@ static struct commit_extra_header *read_commit_extra_header_lines(
>>  		eof = memchr(line, ' ', next - line);
>>  		if (!eof)
>>  			eof = next;
>> -
>> -		if (standard_header_field(line, eof - line) ||
>> -		    excluded_header_field(line, eof - line, exclude))
>> +		else if (standard_header_field(line, eof - line) ||
>> +			 excluded_header_field(line, eof - line, exclude))
>>  			continue;
>>
>>  		it = xcalloc(1, sizeof(*it));
>>
>

```
