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

[PATCH] status: store format option as an int

From
Jonathan Nieder <jrnieder@gmail.com>
Date
Apr 16, 2011, 05:27 UTC
Message-ID
<20110416052704.GA10807@elie>
In-Reply-To
<20110416013723.GA23105@sigill.intra.peff.net>

It is unsafe to pass a pointer to a value of enumerated type to OPT_SET_INT (as v1.7.0-rc0~137^2~14, 2009-0905) does, since it might have the wrong alignment or width (C99 only says "Each enumerated type shall be compatible with char, a signed integer type, or an unsigned integer type. The choice of type is implementation-defined, but shall be capable of representing the values of all the members of the enumeration.)

Probably this didn't come up in practice because by default GCC uses an 'int' to represent small enums unless passed -fshort-enums (except on certain architectures where -fshort-enums is the default).

Noticed-by: Jeff King <peff@peff.net>
Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
---
Jeff King wrote:
> Hmm. That does work (with either option, or both), but it is somewhat of
> an accident. There is an enum specifying the format the user wants. We
> hand it to parse-options for those options, telling it that the value is
> an int.
Yikes.  That's an accident waiting to happen.  How about this, to start?
 builtin/commit.c |    7 ++++---
 1 files changed, 4 insertions(+), 3 deletions(-)
diff --git a/builtin/commit.c b/builtin/commit.c
index 6e32166..b28848d 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -94,11 +94,12 @@ static const char *only_include_assumed;
 static struct strbuf message;
 
 static int null_termination;
-static enum {
-	STATUS_FORMAT_LONG,
+enum status_format {
+	STATUS_FORMAT_LONG = 0,
 	STATUS_FORMAT_SHORT,
 	STATUS_FORMAT_PORCELAIN
-} status_format = STATUS_FORMAT_LONG;
+};
+static int status_format;
 static int status_show_branch;
 
 static int opt_parse_m(const struct option *opt, const char *arg, int unset)
-- 
1.7.5.rc2
Previous: Jeff KingNext: Jonathan Nieder
Message 5 of 10 in “git status -s -v: no override”
  1. Jacek MasiulaniecApr 15, 2011
  2. Jeff KingApr 16, 2011
  3. Jonathan NiederApr 16, 2011
  4. Jeff KingApr 16, 2011
  5. status: store format option as an intJonathan Nieder, Apr 16, 2011
  6. Jonathan NiederApr 16, 2011
  7. Junio C HamanoApr 16, 2011
  8. Jonathan NiederApr 16, 2011
  9. Jeff KingApr 16, 2011
  10. status: store format option as an intJonathan Nieder, May 15, 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.