global: convert intentionally-leaking config strings to consts

There are multiple cases where we intentionally leak config strings: - `struct gpg_format` is used to track programs that can be used for signing commits, either via gpg(1), gpgsm(1) or ssh-keygen(1). The user can override the commands via several config variables. As the array is populated once, only, and the struct memers are never written to or free'd. - `struct ll_merge_driver` is used to track merge drivers. Same as with the GPG format, these drivers are populated once and then reused. Its data is never written to or free'd, either. - `struct userdiff_funcname` and `struct userdiff_driver` can be configured via `diff.<driver>.*` to add additional drivers. Again, these have a global lifetime and are never written to or free'd. All of these are intentionally kept alive and are never written to. Furthermore, all of these are being assigned both string constants in some places, and allocated strings in other places. This will cause warnings once we enable `-Wwrite-strings`, so let's mark the respective fields as `const char *` and cast away the constness when assigning those values. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Jun 7, 2024 at 08:37 UTC c113c5df7911bf7bc6a4542131ac5bf983532a97
4 files changed +21 -16
gpg-interface.c
+2 -2
@@ -34,7 +34,7 @@ static enum signature_trust_level configured_min_trust_level = TRUST_UNDEFINED;
34
35 struct gpg_format {
36 const char *name;
37 - char *program;
37 + const char *program;
38 const char **verify_args;
39 const char **sigs;
40 int (*verify_signed_buffer)(struct signature_check *sigc,
@@ -783,7 +783,7 @@ static int git_gpg_config(const char *var, const char *value,
783
784 if (fmtname) {
785 fmt = get_format_by_name(fmtname);
786 - return git_config_string(&fmt->program, var, value);
786 + return git_config_string((char **) &fmt->program, var, value);
787 }
788
789 return 0;
merge-ll.c
+8 -3
@@ -27,7 +27,7 @@ typedef enum ll_merge_result (*ll_merge_fn)(const struct ll_merge_driver *,
27
28 struct ll_merge_driver {
29 const char *name;
30 - char *description;
30 + const char *description;
31 ll_merge_fn fn;
32 char *recursive;
33 struct ll_merge_driver *next;
@@ -304,8 +304,13 @@ static int read_merge_config(const char *var, const char *value,
304 ll_user_merge_tail = &(fn->next);
305 }
306
307 - if (!strcmp("name", key))
308 - return git_config_string(&fn->description, var, value);
307 + if (!strcmp("name", key)) {
308 + /*
309 + * The description is leaking, but that's okay as we want to
310 + * keep around the merge drivers anyway.
311 + */
312 + return git_config_string((char **) &fn->description, var, value);
313 + }
314
315 if (!strcmp("driver", key)) {
316 if (!value)
userdiff.c
+5 -5
@@ -399,7 +399,7 @@ static struct userdiff_driver *userdiff_find_by_namelen(const char *name, size_t
399 static int parse_funcname(struct userdiff_funcname *f, const char *k,
400 const char *v, int cflags)
401 {
402 - if (git_config_string(&f->pattern, k, v) < 0)
402 + if (git_config_string((char **) &f->pattern, k, v) < 0)
403 return -1;
404 f->cflags = cflags;
405 return 0;
@@ -445,15 +445,15 @@ int userdiff_config(const char *k, const char *v)
445 if (!strcmp(type, "binary"))
446 return parse_tristate(&drv->binary, k, v);
447 if (!strcmp(type, "command"))
448 - return git_config_string(&drv->external, k, v);
448 + return git_config_string((char **) &drv->external, k, v);
449 if (!strcmp(type, "textconv"))
450 - return git_config_string(&drv->textconv, k, v);
450 + return git_config_string((char **) &drv->textconv, k, v);
451 if (!strcmp(type, "cachetextconv"))
452 return parse_bool(&drv->textconv_want_cache, k, v);
453 if (!strcmp(type, "wordregex"))
454 - return git_config_string(&drv->word_regex, k, v);
454 + return git_config_string((char **) &drv->word_regex, k, v);
455 if (!strcmp(type, "algorithm"))
456 - return git_config_string(&drv->algorithm, k, v);
456 + return git_config_string((char **) &drv->algorithm, k, v);
457
458 return 0;
459 }
userdiff.h
+6 -6
@@ -7,19 +7,19 @@ struct index_state;
7 struct repository;
8
9 struct userdiff_funcname {
10 - char *pattern;
10 + const char *pattern;
11 int cflags;
12 };
13
14 struct userdiff_driver {
15 const char *name;
16 - char *external;
17 - char *algorithm;
16 + const char *external;
17 + const char *algorithm;
18 int binary;
19 struct userdiff_funcname funcname;
20 - char *word_regex;
21 - char *word_regex_multi_byte;
22 - char *textconv;
20 + const char *word_regex;
21 + const char *word_regex_multi_byte;
22 + const char *textconv;
23 struct notes_cache *textconv_cache;
24 int textconv_want_cache;
25 };