[PATCH 02/11] config: add git_config_pathname_dup()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxx>
---
 config.c | 11 +++++++++++
 config.h |  7 +++++++
 2 files changed, 18 insertions(+)

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 *);
-- 
2.44.0.872.g288abe5b5b





[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux