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

[PATCH 04/11] config: use git_config_string_dup() for open-coded equivalents

From
Jeff King <peff@peff.net>
Date
Apr 7, 2024, 01:01 UTC
Message-ID
<20240407010100.GD868358@coredump.intra.peff.net>
In-Reply-To
<20240407005656.GA436890@coredump.intra.peff.net>

These are cases where the calling code does the exact same thing git_config_string_dup() would do. We can shorten the code a bit by using it.

Note in the final case that we rely on leaving the if-else chain to return "0" for success, and now we'll return more directly. The two are equivalent.

Signed-off-by: Jeff King <peff@peff.net>
---
 archive-tar.c  | 10 +++-------
 compat/mingw.c |  7 ++-----
 setup.c        |  5 +----
 3 files changed, 6 insertions(+), 16 deletions(-)
diff --git a/archive-tar.c b/archive-tar.c
index 8ae30125f8..6da7101553 100644
--- a/archive-tar.c
+++ b/archive-tar.c
@@ -393,13 +393,9 @@ static int tar_filter_config(const char *var, const char *value,
 		tar_filters[nr_tar_filters++] = ar;
 	}
 
-	if (!strcmp(type, "command")) {
-		if (!value)
-			return config_error_nonbool(var);
-		free(ar->filter_command);
-		ar->filter_command = xstrdup(value);
-		return 0;
-	}
+	if (!strcmp(type, "command"))
+		return git_config_string_dup(&ar->filter_command, var, value);
+
 	if (!strcmp(type, "remote")) {
 		if (git_config_bool(var, value))
 			ar->flags |= ARCHIVER_REMOTE;
diff --git a/compat/mingw.c b/compat/mingw.c
index 320fb99a90..aeccb3957f 100644
--- a/compat/mingw.c
+++ b/compat/mingw.c
@@ -255,11 +255,8 @@ int mingw_core_config(const char *var, const char *value,
 	}
 
 	if (!strcmp(var, "core.unsetenvvars")) {
-		if (!value)
-			return config_error_nonbool(var);
-		free(unset_environment_variables);
-		unset_environment_variables = xstrdup(value);
-		return 0;
+		return git_config_string_dup(&unset_environment_variables, var,
+					     value);
 	}
 
 	if (!strcmp(var, "core.restrictinheritedhandles")) {
diff --git a/setup.c b/setup.c
index 9f35a27978..7204fd2815 100644
--- a/setup.c
+++ b/setup.c
@@ -529,10 +529,7 @@ static int read_worktree_config(const char *var, const char *value,
 	if (strcmp(var, "core.bare") == 0) {
 		data->is_bare = git_config_bool(var, value);
 	} else if (strcmp(var, "core.worktree") == 0) {
-		if (!value)
-			return config_error_nonbool(var);
-		free(data->work_tree);
-		data->work_tree = xstrdup(value);
+		return git_config_string_dup(&data->work_tree, var, value);
 	}
 	return 0;
 }
-- 
2.44.0.872.g288abe5b5b
Previous: Jeff KingNext: Jeff King
Message 9 of 23 in “config: do not leak excludes_file”
  1. config: do not leak excludes_fileJunio C Hamano, Apr 6, 2024
  2. [WIP] git_config_pathname() leakfixJunio C Hamano, Apr 6, 2024
  3. 0/12 git_config_string() considered harmfulJeff King, Apr 7, 2024
  4. 01/11 config: make sequencer.c's git_config_string_dup() publicJeff King, Apr 7, 2024
  5. 02/11 config: add git_config_pathname_dup()Jeff King, Apr 7, 2024
  6. 03/11 config: prefer git_config_string_dup() for temp variablesJeff King, Apr 7, 2024
  7. Eric SunshineApr 7, 2024
  8. Jeff KingApr 7, 2024
  9. 04/11 config: use git_config_string_dup() for open-coded equivalentsJeff King, Apr 7, 2024
  10. 05/11 config: use git_config_string_dup() to fix leaky open codingJeff King, Apr 7, 2024
  11. 06/11 config: use git_config_string() in easy casesJeff King, Apr 7, 2024
  12. Eric SunshineApr 7, 2024
  13. Jeff KingApr 7, 2024
  14. 07/11 config: use git_config_pathname_dup() in easy casesJeff King, Apr 7, 2024
  15. 08/11 http: use git_config_string_dup()Jeff King, Apr 7, 2024
  16. 09/11 merge: use git_config_string_dup() for pull strategiesJeff King, Apr 7, 2024
  17. 10/11 userdiff: use git_config_string_dup() when we canJeff King, Apr 7, 2024
  18. 11/11 blame: use "dup" string_list for ignore-revs filesJeff King, Apr 7, 2024
  19. Eric SunshineApr 7, 2024
  20. Jeff KingApr 7, 2024
  21. Rubén JustoApr 7, 2024
  22. Jeff KingApr 8, 2024
  23. Rubén JustoApr 11, 2024

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.