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

[PATCH 2/8] config: copy the return value of prefix_filename

From
Jeff King <peff@peff.net>
Date
Feb 16, 2012, 08:03 UTC
Message-ID
<20120216080352.GB11843@sigill.intra.peff.net>
In-Reply-To
<20120216080102.GA11793@sigill.intra.peff.net>

The prefix_filename function returns a pointer to a static buffer which may be overwritten by subsequent calls. Since we are going to keep the result around for a while, let's be sure to duplicate it for safety.

I don't think this can be triggered as a bug in the current code, but it's a good idea to be defensive, as any resulting bug would be quite subtle.

Signed-off-by: Jeff King <peff@peff.net>
---
I was tempted to simply use OPT_FILENAME to get the argument to "-f",
which would handle the prefixing for us. However, we can also get the
value from $GIT_CONFIG. That value doesn't actually run through this
code currently, but I think it should (and I fix this in patch 6).
 builtin/config.c |    7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/builtin/config.c b/builtin/config.c
index 09bf778..5a43a3c 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -391,9 +391,10 @@ int cmd_config(int argc, const char **argv, const char *prefix)
 		config_exclusive_filename = git_pathdup("config");
 	else if (given_config_file) {
 		if (!is_absolute_path(given_config_file) && prefix)
-			config_exclusive_filename = prefix_filename(prefix,
-								    strlen(prefix),
-								    given_config_file);
+			config_exclusive_filename =
+				xstrdup(prefix_filename(prefix,
+							strlen(prefix),
+							given_config_file));
 		else
 			config_exclusive_filename = given_config_file;
 	}
-- 
1.7.9.1.4.g8ffed
Previous: Jeff KingNext: Jeff King
Message 8 of 23 in “What's cooking in git.git (Feb 2012, #05; Mon, 13)”
  1. Junio C HamanoFeb 13, 2012
  2. An incremental update to "What's cooking"Junio C Hamano, Feb 14, 2012
  3. Jeff KingFeb 14, 2012
  4. Junio C HamanoFeb 14, 2012
  5. Jeff KingFeb 14, 2012
  6. 0/8 config-include fixesJeff King, Feb 16, 2012
  7. 1/8 t1300: add missing &&-chainingJeff King, Feb 16, 2012
  8. 2/8 config: copy the return value of prefix_filenameJeff King, Feb 16, 2012
  9. 3/8 config: teach git_config_set_multivar_in_file a default pathJeff King, Feb 16, 2012
  10. 4/8 config: teach git_config_rename_section a file argumentJeff King, Feb 16, 2012
  11. 5/8 config: provide a version of git_config with more optionsJeff King, Feb 16, 2012
  12. 6/8 config: stop using config_exclusive_filenameJeff King, Feb 16, 2012
  13. 7/8 config: eliminate config_exclusive_filenameJeff King, Feb 16, 2012
  14. 8/8 config: do not respect includes for single-file --listJeff King, Feb 16, 2012
  15. Junio C HamanoFeb 16, 2012
  16. Jeff KingFeb 17, 2012
  17. Junio C HamanoFeb 17, 2012
  18. Jeff KingFeb 17, 2012
  19. Jeff KingFeb 17, 2012
  20. 0/2 api-config documentation leftoversJeff King, Feb 17, 2012
  21. Junio C HamanoFeb 17, 2012
  22. 1/2 docs/api-config: minor clarificationsJeff King, Feb 17, 2012
  23. 2/2 docs/api-config: describe git_config_with_optionsJeff King, Feb 17, 2012

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.