diff mbox series

[02/11] config: add git_config_pathname_dup()

Message ID 20240407010008.GB868358@coredump.intra.peff.net (mailing list archive)
State New
Headers show
Series git_config_string() considered harmful | expand

Commit Message

Jeff King April 7, 2024, 1 a.m. UTC
The git_config_pathname() function suffers the same potential leak issue
as git_config_string(), since it is basically the same thing but with
the added twist of interpolating the path rather than just duplicating
the value.

Let's provide a similar "dup()" variant to help call sites transition to
using the leak-free variant.

Signed-off-by: Jeff King <peff@peff.net>
---
 config.c | 11 +++++++++++
 config.h |  7 +++++++
 2 files changed, 18 insertions(+)
diff mbox series

Patch

diff --git a/config.c b/config.c
index 2194fb078a..a0aa45abd5 100644
--- a/config.c
+++ b/config.c
@@ -1364,6 +1364,17 @@  int git_config_pathname(const char **dest, const char *var, const char *value)
 	return 0;
 }
 
+int git_config_pathname_dup(char **dest, const char *var, const char *value)
+{
+	if (!value)
+		return config_error_nonbool(var);
+	free(*dest);
+	*dest = interpolate_path(value, 0);
+	if (!*dest)
+		die(_("failed to expand user dir in: '%s'"), value);
+	return 0;
+}
+
 int git_config_expiry_date(timestamp_t *timestamp, const char *var, const char *value)
 {
 	if (!value)
diff --git a/config.h b/config.h
index cdffc14ccf..fed21d3144 100644
--- a/config.h
+++ b/config.h
@@ -300,6 +300,13 @@  int git_config_string_dup(char **, const char *, const char *);
  */
 int git_config_pathname(const char **, const char *, const char *);
 
+/**
+ * Like git_config_pathname(), but frees any previously-allocated
+ * string at the destination pointer, avoiding a leak when a
+ * config variable is seen multiple times.
+ */
+int git_config_pathname_dup(char **, const char *, const char *);
+
 int git_config_expiry_date(timestamp_t *, const char *, const char *);
 int git_config_color(char *, const char *, const char *);
 int git_config_set_in_file_gently(const char *, const char *, const char *, const char *);