Skip to content

Commit 60289b5

Browse files
pks-tgitster
authored andcommitted
pretty: fix memory leaks when parsing pretty formats
When parsing pretty formats from the config we leak the name and user format whenever these are set multiple times. This is because we do not free any already-set value in case there is one. Plugging this leak for the name is trivial. For the user format we need to be a bit more careful, because we may end up assigning a pointer into the allocated region when the string is prefixed with either "format" or "tformat:". In order to make it safe to unconditionally free the user format we thus strdup the stripped string into the field instead of a pointer into the string. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 643c6f5 commit 60289b5

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

pretty.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ static int git_pretty_formats_config(const char *var, const char *value,
6363
void *cb UNUSED)
6464
{
6565
struct cmt_fmt_map *commit_format = NULL;
66-
const char *name;
66+
const char *name, *stripped;
6767
char *fmt;
6868
int i;
6969

@@ -90,15 +90,21 @@ static int git_pretty_formats_config(const char *var, const char *value,
9090
commit_formats_len++;
9191
}
9292

93+
free((char *)commit_format->name);
9394
commit_format->name = xstrdup(name);
9495
commit_format->format = CMIT_FMT_USERFORMAT;
9596
if (git_config_string(&fmt, var, value))
9697
return -1;
9798

98-
if (skip_prefix(fmt, "format:", &commit_format->user_format)) {
99+
free((char *)commit_format->user_format);
100+
if (skip_prefix(fmt, "format:", &stripped)) {
99101
commit_format->is_tformat = 0;
100-
} else if (skip_prefix(fmt, "tformat:", &commit_format->user_format)) {
102+
commit_format->user_format = xstrdup(stripped);
103+
free(fmt);
104+
} else if (skip_prefix(fmt, "tformat:", &stripped)) {
101105
commit_format->is_tformat = 1;
106+
commit_format->user_format = xstrdup(stripped);
107+
free(fmt);
102108
} else if (strchr(fmt, '%')) {
103109
commit_format->is_tformat = 1;
104110
commit_format->user_format = fmt;

0 commit comments

Comments
 (0)