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 such that we track allocated comment character strings via a separate non-constant variable `comment_line_str_allocated`. Adapt sites that set `comment_line_str` to set both and free the old value that was stored in `comment_line_str_allocated`. 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> --- builtin/commit.c | 7 +++++-- config.c | 4 +++- environment.c | 1 + environment.h | 1 + 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/builtin/commit.c b/builtin/commit.c index 66427ba82d..025b1c4686 100644 --- a/builtin/commit.c +++ b/builtin/commit.c @@ -684,7 +684,9 @@ static void adjust_comment_line_char(const struct strbuf *sb) const char *p; if (!memchr(sb->buf, candidates[0], sb->len)) { - comment_line_str = xstrfmt("%c", candidates[0]); + free(comment_line_str_allocated); + comment_line_str = comment_line_str_allocated = + xstrfmt("%c", candidates[0]); return; } @@ -705,7 +707,8 @@ static void adjust_comment_line_char(const struct strbuf *sb) if (!*p) die(_("unable to select a comment character that is not used\n" "in the current commit message")); - comment_line_str = xstrfmt("%c", *p); + free(comment_line_str_allocated); + comment_line_str = comment_line_str_allocated = xstrfmt("%c", *p); } static void prepare_amend_commit(struct commit *commit, struct strbuf *sb, diff --git a/config.c b/config.c index 6421894614..cb78b652ee 100644 --- a/config.c +++ b/config.c @@ -1596,7 +1596,9 @@ 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); - comment_line_str = xstrdup(value); + free(comment_line_str_allocated); + comment_line_str = comment_line_str_allocated = + xstrdup(value); auto_comment_line_char = 0; } else return error(_("%s must have at least one character"), var); diff --git a/environment.c b/environment.c index 5cea2c9f54..1a95798d5f 100644 --- a/environment.c +++ b/environment.c @@ -114,6 +114,7 @@ int protect_ntfs = PROTECT_NTFS_DEFAULT; * that is subject to stripspace. */ const char *comment_line_str = "#"; +char *comment_line_str_allocated; int auto_comment_line_char; /* Parallel index stat data preload? */ diff --git a/environment.h b/environment.h index e9f01d4d11..0e0906f125 100644 --- a/environment.h +++ b/environment.h @@ -9,6 +9,7 @@ struct strvec; * that is subject to stripspace. */ extern const char *comment_line_str; +extern char *comment_line_str_allocated; extern int auto_comment_line_char; /* -- 2.46.0.46.g406f326d27.dirty
Attachment:
signature.asc
Description: PGP signature