@@ -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)
@@ -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 *);
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(+)