git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 3/3] grep: get rid of useless x < 0 comparison on an enum member

From
Jonathan Nieder <jrnieder@gmail.com>
Date
Nov 7, 2011, 19:49 UTC
Message-ID
<20111107194912.GA12469@elie.hsd1.il.comcast.net>
In-Reply-To
<1320581184-4557-4-git-send-email-avarab@gmail.com>
Hi,
Show 8 quoted lines
> --- a/grep.c
> +++ b/grep.c
> @@ -327,7 +327,7 @@ static struct grep_expr *prep_header_patterns(struct grep_opt *opt)
>  	for (p = opt->header_list; p; p = p->next) {
>  		if (p->token != GREP_PATTERN_HEAD)
>  			die("bug: a non-header pattern in grep header list.");
> -		if (p->field < 0 || GREP_HEADER_FIELD_MAX <= p->field)
> +		if (GREP_HEADER_FIELD_MAX <= p->field)
I imagine the following would be less controversial.
-- >8 --
From: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Subject: grep: get rid of bounds check on "enum grep_header_field" value
The grep_header_field enum is defined as:
    enum grep_header_field {
    	GREP_HEADER_AUTHOR = 0,
    	GREP_HEADER_COMMITTER
    };

Git's grep code sanity-checks the value of p->field before using it in order to avoid reading and writing past the end of an array. Unfortunately that test trips up misguided static analyzers like "gcc -Wtype-limits" that do not recognize that C allows this enum to be a signed integer type and an "x < 0" comparison really could fire if someone passed in an uninitialized value.

Let's just drop the check. Luckily git always does initialize p->field correctly, and if it were to stop doing so, then hopefully running the test suite with valgrind would catch it.

Noticed with clang -Wtautological-compare.
[jn: drop the GREP_HEADER_FIELD_MAX <= p->field check, too,
 for symmetry]
Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
---
 grep.c |    2 --
 1 files changed, 0 insertions(+), 2 deletions(-)
diff --git a/grep.c b/grep.c
index b29d09c7..424c46cd 100644
--- a/grep.c
+++ b/grep.c
@@ -327,8 +327,6 @@ static struct grep_expr *prep_header_patterns(struct grep_opt *opt)
 	for (p = opt->header_list; p; p = p->next) {
 		if (p->token != GREP_PATTERN_HEAD)
 			die("bug: a non-header pattern in grep header list.");
-		if (p->field < 0 || GREP_HEADER_FIELD_MAX <= p->field)
-			die("bug: unknown header field %d", p->field);
 		compile_regexp(p, opt);
 	}
 
-- 
1.7.8.rc0
Previous: Andreas SchwabNext: Junio C Hamano
Message 16 of 22 in “Fix code issues spotted by clang”
  1. 0/3 Fix code issues spotted by clangÆvar Arnfjörð Bjarmason, Nov 6, 2011
  2. 1/3 apply: get rid of useless x < 0 comparison on a size_t typeÆvar Arnfjörð Bjarmason, Nov 6, 2011
  3. Junio C HamanoNov 6, 2011
  4. Giuseppe BilottaNov 7, 2011
  5. 2/3 diff/apply: cast variable in call to free()Ævar Arnfjörð Bjarmason, Nov 6, 2011
  6. 3/3 grep: get rid of useless x < 0 comparison on an enum memberÆvar Arnfjörð Bjarmason, Nov 6, 2011
  7. Andreas SchwabNov 6, 2011
  8. Ævar Arnfjörð BjarmasonNov 7, 2011
  9. Andreas SchwabNov 7, 2011
  10. Jeff KingNov 7, 2011
  11. Andreas SchwabNov 7, 2011
  12. Jeff KingNov 7, 2011
  13. Jeff KingNov 7, 2011
  14. Ævar Arnfjörð BjarmasonNov 7, 2011
  15. Andreas SchwabNov 7, 2011
  16. Jonathan NiederNov 7, 2011
  17. Junio C HamanoNov 7, 2011
  18. Jonathan NiederNov 7, 2011
  19. Junio C HamanoNov 7, 2011
  20. Jonathan NiederNov 7, 2011
  21. Ævar Arnfjörð BjarmasonNov 6, 2011
  22. Elijah NewrenNov 8, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.