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, 21:32 UTC
Message-ID
<20111107213219.GA13537@elie.hsd1.il.comcast.net>
In-Reply-To
<7vlirr1vi5.fsf@alter.siamese.dyndns.org>
Junio C Hamano wrote:
> Jonathan Nieder <jrnieder@gmail.com> writes:
>> [jn: drop the GREP_HEADER_FIELD_MAX <= p->field check, too,
>>  for symmetry]
>
> Umm, why is this removal of defensive programming practice an improvement?

Checking "p->filed < 0" makes static analyzers complain. There's no clean way I know of[*] to get them to shut up and keep the check. The fundamental question is whether the -Wtype-limits check is worth it or not:

 - on one hand, it catches real bugs, such as the mistaken check for
   negative size_t Ævar mentioned;
 - on the other hand, it trips for cases like this in which the type
   only happened to be unsigned on the build platform.  I consider
   that a gcc bug (and apparently clang shares it) --- see
   <http://bugs.debian.org/615525>.

So, the purpose of this patch was to work around this common bug in static analyzers.

Checking "GREP_HEADER_FIELD_MAX <= p->field" without checking for "p->field < 0", like the original patch did, would be only checking half of what it intends to and not add any real guarantees. And more importantly, it would be distracting to people like me and Andreas who would wonder "why doesn't this check p->field < 0, too?".

I guess the commit message should have said
	grep: remove a defensive check to work around common static analyzer bug
> This part is mostly my code and because I know what I wrote is almost
> always perfect, so I do not think there is any real harm done, but still...
Heh.
[*] There are plenty of cryptic, hackish ways possible, though. :)
	if ((size_t) p->field >= GREP_HEADER_FIELD_MAX)
		die(...);
Previous: Junio C HamanoNext: Junio C Hamano
Message 18 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.