userdiff: fix leaking memory for configured diff drivers

The userdiff structures may be initialized either statically on the
stack or dynamically via configuration keys. In the latter case we end
up leaking memory because we didn't have any infrastructure to discern
those strings which have been allocated statically and those which have
been allocated dynamically.

Refactor the code such that we have two pointers for each of these
strings: one that holds the value as accessed by other subsystems, and
one that points to the same string in case it has been allocated. Like
this, we can safely free the second pointer and thus plug those memory
leaks.

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:50 +02:00
committed by Junio C Hamano
parent 1bc158e750
commit 38678e5df5
7 changed files with 43 additions and 11 deletions

View File

@ -8,6 +8,7 @@ struct repository;
struct userdiff_funcname {
const char *pattern;
char *pattern_owned;
int cflags;
};
@ -20,11 +21,14 @@ struct userdiff_driver {
const char *name;
struct external_diff external;
const char *algorithm;
char *algorithm_owned;
int binary;
struct userdiff_funcname funcname;
const char *word_regex;
char *word_regex_owned;
const char *word_regex_multi_byte;
const char *textconv;
char *textconv_owned;
struct notes_cache *textconv_cache;
int textconv_want_cache;
};