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

[PATCH 2/4] multi-pack-index: avoid writing to global in option callback

From
Jeff King <peff@peff.net>
Date
Oct 6, 2022, 13:10 UTC
Message-ID
<Yz7T3SHEnYjDDPnn@coredump.intra.peff.net>
In-Reply-To
<Yz7Tjy7Rh8cXVxYQ@coredump.intra.peff.net>
We declare the --object-dir option like:
  OPT_CALLBACK(0, "object-dir", &opts.object_dir, ...);

but the pointer to opts.object_dir is completely unused. Instead, the callback writes directly to a global. Which fortunately happens to be opts.object_dir. So everything works as expected, but it's unnecessarily confusing.

Instead, let's have the callback write to the option value pointer that has been passed in. This also quiets a -Wunused-parameter warning (since we don't otherwise look at "opt").

Signed-off-by: Jeff King <peff@peff.net>
---
 builtin/multi-pack-index.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/builtin/multi-pack-index.c b/builtin/multi-pack-index.c
index 9b126d6ce0..9a18a82b05 100644
--- a/builtin/multi-pack-index.c
+++ b/builtin/multi-pack-index.c
@@ -56,11 +56,12 @@ static struct opts_multi_pack_index {
 static int parse_object_dir(const struct option *opt, const char *arg,
 			    int unset)
 {
-	free(opts.object_dir);
+	char **value = opt->value;
+	free(*value);
 	if (unset)
-		opts.object_dir = xstrdup(get_object_directory());
+		*value = xstrdup(get_object_directory());
 	else
-		opts.object_dir = real_pathdup(arg, 1);
+		*value = real_pathdup(arg, 1);
 	return 0;
 }
 
-- 
2.38.0.661.g581b1c9920
Previous: Jeff KingNext: Jeff King
Message 3 of 11 in “a few small unused-parameter fixes”
  1. 0/4 a few small unused-parameter fixesJeff King, Oct 6, 2022
  2. 1/4 test-submodule: inline resolve_relative_url() functionJeff King, Oct 6, 2022
  3. 2/4 multi-pack-index: avoid writing to global in option callbackJeff King, Oct 6, 2022
  4. 3/4 commit: avoid writing to global in option callbackJeff King, Oct 6, 2022
  5. 4/4 attr: convert DEBUG_ATTR to use trace APIJeff King, Oct 6, 2022
  6. attr: drop DEBUG_ATTR codeJeff King, Oct 6, 2022
  7. Junio C HamanoOct 6, 2022
  8. Derrick StoleeOct 6, 2022
  9. Junio C HamanoOct 6, 2022
  10. Jeff KingOct 11, 2022
  11. Jeff KingOct 11, 2022

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.