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 <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Patrick Steinhardt
2024-08-14 08:52:14 +02:00
committed by Junio C Hamano
parent 5f6519b62c
commit 648abbe22d
4 changed files with 9 additions and 3 deletions

View File

@ -9,6 +9,7 @@ struct strvec;
* that is subject to stripspace.
*/
extern const char *comment_line_str;
extern char *comment_line_str_to_free;
extern int auto_comment_line_char;
/*