When the comment line character has been specified multiple times in the configuration, then `git_default_core_config()` will cause a memory leak because it unconditionally copies the string into `comment_line_str` without free'ing the previous value. In fact, it can't easily free the value in the first place because it may contain a string constant. Refactor the code so that we initialize the value with another array. This allows us to free the value in case the string is not pointing to that constant array anymore. This memory leak is being hit in t3404. As there are still other memory leaks in that file we cannot yet mark it as passing with leak checking enabled. Signed-off-by: Patrick Steinhardt <ps@xxxxxx> --- config.c | 2 ++ environment.c | 3 ++- environment.h | 1 + 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/config.c b/config.c index 6421894614..63e0211c7d 100644 --- a/config.c +++ b/config.c @@ -1596,6 +1596,8 @@ static int git_default_core_config(const char *var, const char *value, else if (value[0]) { if (strchr(value, '\n')) return error(_("%s cannot contain newline"), var); + if (comment_line_str != comment_line_str_default) + free((char *) comment_line_str); comment_line_str = xstrdup(value); auto_comment_line_char = 0; } else diff --git a/environment.c b/environment.c index 5cea2c9f54..8297c6e37b 100644 --- a/environment.c +++ b/environment.c @@ -113,7 +113,8 @@ int protect_ntfs = PROTECT_NTFS_DEFAULT; * The character that begins a commented line in user-editable file * that is subject to stripspace. */ -const char *comment_line_str = "#"; +const char comment_line_str_default[] = "#"; +const char *comment_line_str = comment_line_str_default; int auto_comment_line_char; /* Parallel index stat data preload? */ diff --git a/environment.h b/environment.h index e9f01d4d11..5e5d9a8045 100644 --- a/environment.h +++ b/environment.h @@ -8,6 +8,7 @@ struct strvec; * The character that begins a commented line in user-editable file * that is subject to stripspace. */ +extern const char comment_line_str_default[]; extern const char *comment_line_str; extern int auto_comment_line_char; -- 2.46.0.dirty
Attachment:
signature.asc
Description: PGP signature