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

[PATCH 2/2] parse-options: use and require int pointer for OPT_CMDMODE

From
René Scharfe <l.s.r@web.de>
Date
Sep 9, 2023, 21:14 UTC
Message-ID
<e6d8a291-03de-cfd3-3813-747fc2cad145@web.de>
In-Reply-To
<2d6f3d74-687a-2d40-5c0c-abc396aef80f@web.de>

Some uses of OPT_CMDMODE provide a pointer to an enum. It is dereferenced as an int pointer in parse-options.c::get_value(). These two types are incompatible, though -- the storage size of an enum can vary between platforms. C23 would allow us to specify the underlying type of the different enums, making them compatible, but with C99 the easiest safe option is to actually use int as the value type.

Convert the offending OPT_CMDMODE users and use the typed value_int point in the macro's definition to enforce that type for future ones.

Signed-off-by: René Scharfe <l.s.r@web.de>
---
 builtin/am.c         | 2 +-
 builtin/help.c       | 5 +++--
 builtin/ls-tree.c    | 2 +-
 builtin/rebase.c     | 2 +-
 builtin/replace.c    | 3 ++-
 builtin/stripspace.c | 2 +-
 parse-options.h      | 2 +-
 7 files changed, 10 insertions(+), 8 deletions(-)
diff --git a/builtin/am.c b/builtin/am.c
index 202040b62e..ebb72ebaaa 100644
--- a/builtin/am.c
+++ b/builtin/am.c
@@ -2269,7 +2269,7 @@ enum resume_type {
 };

 struct resume_mode {
-	enum resume_type mode;
+	int mode;
 	enum show_patch_type sub_mode;
 };

diff --git a/builtin/help.c b/builtin/help.c
index dc1fbe2b98..e8aedb932c 100644
--- a/builtin/help.c
+++ b/builtin/help.c
@@ -42,7 +42,7 @@ enum show_config_type {
 	SHOW_CONFIG_SECTIONS,
 };

-static enum help_action {
+enum help_action {
 	HELP_ACTION_ALL = 1,
 	HELP_ACTION_GUIDES,
 	HELP_ACTION_CONFIG,
@@ -50,7 +50,8 @@ static enum help_action {
 	HELP_ACTION_DEVELOPER_INTERFACES,
 	HELP_ACTION_CONFIG_FOR_COMPLETION,
 	HELP_ACTION_CONFIG_SECTIONS_FOR_COMPLETION,
-} cmd_mode;
+};
+static int cmd_mode;

 static const char *html_path;
 static int verbose = 1;
diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c
index 209d2dc0d5..6f8c43f729 100644
--- a/builtin/ls-tree.c
+++ b/builtin/ls-tree.c
@@ -346,7 +346,7 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix)
 	int i, full_tree = 0;
 	int full_name = !prefix || !*prefix;
 	read_tree_fn_t fn = NULL;
-	enum ls_tree_cmdmode cmdmode = MODE_DEFAULT;
+	int cmdmode = MODE_DEFAULT;
 	int null_termination = 0;
 	struct ls_tree_options options = { 0 };
 	const struct option ls_tree_options[] = {
diff --git a/builtin/rebase.c b/builtin/rebase.c
index 50cb85751f..d11e749579 100644
--- a/builtin/rebase.c
+++ b/builtin/rebase.c
@@ -111,7 +111,7 @@ struct rebase_options {
 		REBASE_INTERACTIVE_EXPLICIT = 1<<4,
 	} flags;
 	struct strvec git_am_opts;
-	enum action action;
+	int action;
 	char *reflog_action;
 	int signoff;
 	int allow_rerere_autoupdate;
diff --git a/builtin/replace.c b/builtin/replace.c
index da59600ad2..d0063d3feb 100644
--- a/builtin/replace.c
+++ b/builtin/replace.c
@@ -553,7 +553,8 @@ int cmd_replace(int argc, const char **argv, const char *prefix)
 		MODE_GRAFT,
 		MODE_CONVERT_GRAFT_FILE,
 		MODE_REPLACE
-	} cmdmode = MODE_UNSPECIFIED;
+	};
+	int cmdmode = MODE_UNSPECIFIED;
 	struct option options[] = {
 		OPT_CMDMODE('l', "list", &cmdmode, N_("list replace refs"), MODE_LIST),
 		OPT_CMDMODE('d', "delete", &cmdmode, N_("delete replace refs"), MODE_DELETE),
diff --git a/builtin/stripspace.c b/builtin/stripspace.c
index 7b700a9fb1..f6de0b17dc 100644
--- a/builtin/stripspace.c
+++ b/builtin/stripspace.c
@@ -32,7 +32,7 @@ enum stripspace_mode {
 int cmd_stripspace(int argc, const char **argv, const char *prefix)
 {
 	struct strbuf buf = STRBUF_INIT;
-	enum stripspace_mode mode = STRIP_DEFAULT;
+	int mode = STRIP_DEFAULT;
 	int nongit;

 	const struct option options[] = {
diff --git a/parse-options.h b/parse-options.h
index 5e7475bd2d..349c3fca04 100644
--- a/parse-options.h
+++ b/parse-options.h
@@ -262,7 +262,7 @@ struct option {
 	.type = OPTION_SET_INT, \
 	.short_name = (s), \
 	.long_name = (l), \
-	.value = (v), \
+	.value_int = (v), \
 	.help = (h), \
 	.flags = PARSE_OPT_CMDMODE|PARSE_OPT_NOARG|PARSE_OPT_NONEG | (f), \
 	.defval = (i), \
--
2.42.0
Previous: René ScharfeNext: Oswald Buddenhagen
Message 2 of 36 in “parse-options: add int value pointer to struct option”
  1. 1/2 parse-options: add int value pointer to struct optionRené Scharfe, Sep 9, 2023
  2. 2/2 parse-options: use and require int pointer for OPT_CMDMODERené Scharfe, Sep 9, 2023
  3. Oswald BuddenhagenSep 10, 2023
  4. René ScharfeSep 11, 2023
  5. Jeff KingSep 12, 2023
  6. Junio C HamanoSep 16, 2023
  7. René ScharfeSep 18, 2023
  8. Oswald BuddenhagenSep 18, 2023
  9. René ScharfeSep 19, 2023
  10. am: fix error message in parse_opt_show_current_patch()Oswald Buddenhagen, Sep 21, 2023
  11. Junio C HamanoSep 21, 2023
  12. Oswald BuddenhagenSep 21, 2023
  13. Phillip WoodSep 18, 2023
  14. Junio C HamanoSep 18, 2023
  15. Phillip WoodSep 18, 2023
  16. René ScharfeOct 3, 2023
  17. Junio C HamanoOct 3, 2023
  18. René ScharfeSep 19, 2023
  19. Junio C HamanoSep 11, 2023
  20. René ScharfeSep 11, 2023
  21. Oswald BuddenhagenSep 19, 2023
  22. René ScharfeSep 20, 2023
  23. Oswald BuddenhagenSep 21, 2023
  24. René ScharfeOct 3, 2023
  25. Oswald BuddenhagenOct 3, 2023
  26. René ScharfeOct 3, 2023
  27. Oswald BuddenhagenOct 3, 2023
  28. Taylor BlauSep 10, 2023
  29. René ScharfeSep 11, 2023
  30. Junio C HamanoSep 11, 2023
  31. Oswald BuddenhagenSep 11, 2023
  32. Kristoffer HaugsbakkSep 18, 2023
  33. René ScharfeSep 18, 2023
  34. Oswald BuddenhagenSep 18, 2023
  35. Junio C HamanoSep 18, 2023
  36. René ScharfeSep 20, 2023

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.