Skip to content

Commit 648abbe

Browse files
pks-tgitster
authored andcommitted
config: fix leaking comment character config
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_to_free`. Adapt sites that set `comment_line_str` to set both and free the old value that was stored in `comment_line_str_to_free`. 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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5f6519b commit 648abbe

File tree

4 files changed

+9
-3
lines changed

4 files changed

+9
-3
lines changed

builtin/commit.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,9 @@ static void adjust_comment_line_char(const struct strbuf *sb)
684684
const char *p;
685685

686686
if (!memchr(sb->buf, candidates[0], sb->len)) {
687-
comment_line_str = xstrfmt("%c", candidates[0]);
687+
free(comment_line_str_to_free);
688+
comment_line_str = comment_line_str_to_free =
689+
xstrfmt("%c", candidates[0]);
688690
return;
689691
}
690692

@@ -705,7 +707,8 @@ static void adjust_comment_line_char(const struct strbuf *sb)
705707
if (!*p)
706708
die(_("unable to select a comment character that is not used\n"
707709
"in the current commit message"));
708-
comment_line_str = xstrfmt("%c", *p);
710+
free(comment_line_str_to_free);
711+
comment_line_str = comment_line_str_to_free = xstrfmt("%c", *p);
709712
}
710713

711714
static void prepare_amend_commit(struct commit *commit, struct strbuf *sb,

config.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1596,7 +1596,8 @@ static int git_default_core_config(const char *var, const char *value,
15961596
else if (value[0]) {
15971597
if (strchr(value, '\n'))
15981598
return error(_("%s cannot contain newline"), var);
1599-
comment_line_str = xstrdup(value);
1599+
comment_line_str = value;
1600+
FREE_AND_NULL(comment_line_str_to_free);
16001601
auto_comment_line_char = 0;
16011602
} else
16021603
return error(_("%s must have at least one character"), var);

environment.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ int protect_ntfs = PROTECT_NTFS_DEFAULT;
114114
* that is subject to stripspace.
115115
*/
116116
const char *comment_line_str = "#";
117+
char *comment_line_str_to_free;
117118
int auto_comment_line_char;
118119

119120
/* Parallel index stat data preload? */

environment.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ struct strvec;
99
* that is subject to stripspace.
1010
*/
1111
extern const char *comment_line_str;
12+
extern char *comment_line_str_to_free;
1213
extern int auto_comment_line_char;
1314

1415
/*

0 commit comments

Comments
 (0)