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
Jeff King <peff@peff.net>
Date
Nov 7, 2011, 18:34 UTC
Message-ID
<20111107183402.GA5118@sigill.intra.peff.net>
In-Reply-To
<m2k47b4wqi.fsf@igel.home>
On Mon, Nov 07, 2011 at 07:24:05PM +0100, Andreas Schwab wrote:
Show 7 quoted lines
> Jeff King <peff@peff.net> writes:
> 
> > Yes, but now you are getting into implementation-defined behavior, which
> > is also something to avoid.
> 
> The whole point of the statement is a sanity check to uncover bugs.  If
> you remove the first condition you completely ruin its point.

I'm somewhat dubious of the value of a bug-check that may or may not actually kick in depending on your compiler's choice of enum representation, and whose bugs are generally easier to check via static analysis (i.e., making sure the enum value is one of the enumerated values when it is initialized or assigned).

Yes, static analysis can miss some bugs (like passing the address of the enum through a void pointer (e.g., when memset'ing a struct)). But couldn't it just as easily be out of range in the other direction?

It seems like the bug trying to be caught is probably something like:
  enum foo v = function_which_returns_value_or_negative_error();
  if (v < 0)
     die("BUG");

But in that case the bug is on the first line, and it is easily caught by static analysis, no?

-Peff
Previous: Andreas SchwabNext: Jeff King
Message 12 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.