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

Re: [PATCH 2/3] convert "enum date_mode" into a struct

From
Jeff King <peff@peff.net>
Date
Jul 7, 2015, 21:13 UTC
Message-ID
<20150707211304.GA16683@peff.net>
In-Reply-To
<xmqq4mlf3cgf.fsf@gitster.dls.corp.google.com>
On Tue, Jul 07, 2015 at 02:05:52PM -0700, Junio C Hamano wrote:
Show 5 quoted lines
> And that is because DATE_NORMAL is defined to be 0; we can claim
> that the compiler is being stupid to take one of the enum
> date_mode_type values that happens to be 0 and misinterpret it as
> the program wanted to pass a NULL pointer to a structure, but that
> is not what happened.

Ah, I didn't think the compiler would coerce an enum into a pointer constant. That seems kind of insane. But it is indeed what gcc does.

In that case, we can indeed do the NULL-pointer thing I mentioned. Which is not even _that_ ugly; it follows the standard.

The "cast DATE_RELATIVE to a pointer and uncast it on the other side" thing _does_ violate the standard. It is not needed for this, but it would make the DATE_MODE() macro reentrant.

> > +	static const struct fallback_mode = { DATE_NORMAL };
> 
> Yes, that is nasty.  Renumbering the enum to begin with 1 may be a
> much saner solution, unless somebody does

I am worried more about somebody who does memset(0) on a struct, and expects that to default to DATE_NORMAL.

> In any case, I did another evil merge to fix it.

OK. Do you want to leave it be, then, or would you prefer me to do the NULL fallback? Or we could bump the enum to start with 1, and then explicitly treat "0" as a synonym for DATE_NORMAL (in case it comes in through a memset or similar).

-Peff
Previous: Junio C HamanoNext: Junio C Hamano
Message 12 of 31 in “several date related issues”
  1. H.Merijn BrandJun 25, 2015
  2. Jeff KingJun 25, 2015
  3. H.Merijn BrandJun 25, 2015
  4. 0/3 localized date formatJeff King, Jun 25, 2015
  5. 1/3 show-branch: use DATE_RELATIVE instead of magic numberJeff King, Jun 25, 2015
  6. 2/3 convert "enum date_mode" into a structJeff King, Jun 25, 2015
  7. John KeepingJun 25, 2015
  8. Jeff KingJun 25, 2015
  9. Junio C HamanoJul 7, 2015
  10. Jeff KingJul 7, 2015
  11. Junio C HamanoJul 7, 2015
  12. Jeff KingJul 7, 2015
  13. Junio C HamanoJul 7, 2015
  14. 3/3 introduce "format" date-modeJeff King, Jun 25, 2015
  15. Eric SunshineJun 29, 2015
  16. Jeff KingJun 30, 2015
  17. Junio C HamanoJun 30, 2015
  18. Jeff KingJun 30, 2015
  19. Junio C HamanoJun 30, 2015
  20. Jeff KingJun 30, 2015
  21. Eric SunshineJun 30, 2015
  22. Jeff KingJun 30, 2015
  23. Eric SunshineJun 30, 2015
  24. Jeff KingJun 30, 2015
  25. Junio C HamanoJun 30, 2015
  26. Eric SunshineJun 30, 2015
  27. Jeff KingJun 30, 2015
  28. Jeff KingJun 30, 2015
  29. Eric SunshineJun 30, 2015
  30. Eric SunshineJul 21, 2015
  31. Jeff KingJul 21, 2015

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.