Skip to content

Commit ba176db

Browse files
peffgitster
authored andcommitted
config: handle NULL value when parsing non-bools
When the config parser sees an "implicit" bool like: [core] someVariable it passes NULL to the config callback. Any callback code which expects a string must check for NULL. This usually happens via helpers like git_config_string(), etc, but some custom code forgets to do so and will segfault. These are all fairly vanilla cases where the solution is just the usual pattern of: if (!value) return config_error_nonbool(var); though note that in a few cases we have to split initializers like: int some_var = initializer(); into: int some_var; if (!value) return config_error_nonbool(var); some_var = initializer(); There are still some broken instances after this patch, which I'll address on their own in individual patches after this one. Reported-by: Carlos Andrés Ramírez Cataño <[email protected]> Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 564d025 commit ba176db

File tree

11 files changed

+47
-5
lines changed

11 files changed

+47
-5
lines changed

builtin/blame.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,8 @@ static int git_blame_config(const char *var, const char *value,
748748
}
749749

750750
if (!strcmp(var, "blame.coloring")) {
751+
if (!value)
752+
return config_error_nonbool(var);
751753
if (!strcmp(value, "repeatedLines")) {
752754
coloring_mode |= OUTPUT_COLOR_LINE;
753755
} else if (!strcmp(value, "highlightRecent")) {

builtin/checkout.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,6 +1202,8 @@ static int git_checkout_config(const char *var, const char *value,
12021202
struct checkout_opts *opts = cb;
12031203

12041204
if (!strcmp(var, "diff.ignoresubmodules")) {
1205+
if (!value)
1206+
return config_error_nonbool(var);
12051207
handle_ignore_submodules_arg(&opts->diff_options, value);
12061208
return 0;
12071209
}

builtin/clone.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,8 @@ static int git_clone_config(const char *k, const char *v,
791791
const struct config_context *ctx, void *cb)
792792
{
793793
if (!strcmp(k, "clone.defaultremotename")) {
794+
if (!v)
795+
return config_error_nonbool(k);
794796
free(remote_name);
795797
remote_name = xstrdup(v);
796798
}

builtin/log.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,8 +594,11 @@ static int git_log_config(const char *var, const char *value,
594594
decoration_style = 0; /* maybe warn? */
595595
return 0;
596596
}
597-
if (!strcmp(var, "log.diffmerges"))
597+
if (!strcmp(var, "log.diffmerges")) {
598+
if (!value)
599+
return config_error_nonbool(var);
598600
return diff_merges_config(value);
601+
}
599602
if (!strcmp(var, "log.showroot")) {
600603
default_show_root = git_config_bool(var, value);
601604
return 0;

builtin/pack-objects.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3204,14 +3204,18 @@ static int git_pack_config(const char *k, const char *v,
32043204
return 0;
32053205
}
32063206
if (!strcmp(k, "uploadpack.blobpackfileuri")) {
3207-
struct configured_exclusion *ex = xmalloc(sizeof(*ex));
3207+
struct configured_exclusion *ex;
32083208
const char *oid_end, *pack_end;
32093209
/*
32103210
* Stores the pack hash. This is not a true object ID, but is
32113211
* of the same form.
32123212
*/
32133213
struct object_id pack_hash;
32143214

3215+
if (!v)
3216+
return config_error_nonbool(k);
3217+
3218+
ex = xmalloc(sizeof(*ex));
32153219
if (parse_oid_hex(v, &ex->e.oid, &oid_end) ||
32163220
*oid_end != ' ' ||
32173221
parse_oid_hex(oid_end + 1, &pack_hash, &pack_end) ||

compat/mingw.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,8 @@ int mingw_core_config(const char *var, const char *value,
255255
}
256256

257257
if (!strcmp(var, "core.unsetenvvars")) {
258+
if (!value)
259+
return config_error_nonbool(var);
258260
free(unset_environment_variables);
259261
unset_environment_variables = xstrdup(value);
260262
return 0;

config.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,6 +1386,8 @@ static int git_default_core_config(const char *var, const char *value,
13861386
return 0;
13871387
}
13881388
if (!strcmp(var, "core.checkstat")) {
1389+
if (!value)
1390+
return config_error_nonbool(var);
13891391
if (!strcasecmp(value, "default"))
13901392
check_stat = 1;
13911393
else if (!strcasecmp(value, "minimal"))
@@ -1547,11 +1549,15 @@ static int git_default_core_config(const char *var, const char *value,
15471549
}
15481550

15491551
if (!strcmp(var, "core.checkroundtripencoding")) {
1552+
if (!value)
1553+
return config_error_nonbool(var);
15501554
check_roundtrip_encoding = xstrdup(value);
15511555
return 0;
15521556
}
15531557

15541558
if (!strcmp(var, "core.notesref")) {
1559+
if (!value)
1560+
return config_error_nonbool(var);
15551561
notes_ref_name = xstrdup(value);
15561562
return 0;
15571563
}
@@ -1619,6 +1625,8 @@ static int git_default_core_config(const char *var, const char *value,
16191625
}
16201626

16211627
if (!strcmp(var, "core.createobject")) {
1628+
if (!value)
1629+
return config_error_nonbool(var);
16221630
if (!strcmp(value, "rename"))
16231631
object_creation_mode = OBJECT_CREATION_USES_RENAMES;
16241632
else if (!strcmp(value, "link"))

diff.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,10 @@ int git_diff_ui_config(const char *var, const char *value,
372372
return 0;
373373
}
374374
if (!strcmp(var, "diff.colormovedws")) {
375-
unsigned cm = parse_color_moved_ws(value);
375+
unsigned cm;
376+
if (!value)
377+
return config_error_nonbool(var);
378+
cm = parse_color_moved_ws(value);
376379
if (cm & COLOR_MOVED_WS_ERROR)
377380
return -1;
378381
diff_color_moved_ws_default = cm;
@@ -426,10 +429,15 @@ int git_diff_ui_config(const char *var, const char *value,
426429
if (!strcmp(var, "diff.orderfile"))
427430
return git_config_pathname(&diff_order_file_cfg, var, value);
428431

429-
if (!strcmp(var, "diff.ignoresubmodules"))
432+
if (!strcmp(var, "diff.ignoresubmodules")) {
433+
if (!value)
434+
return config_error_nonbool(var);
430435
handle_ignore_submodules_arg(&default_diff_options, value);
436+
}
431437

432438
if (!strcmp(var, "diff.submodule")) {
439+
if (!value)
440+
return config_error_nonbool(var);
433441
if (parse_submodule_params(&default_diff_options, value))
434442
warning(_("Unknown value for 'diff.submodule' config variable: '%s'"),
435443
value);
@@ -473,7 +481,10 @@ int git_diff_basic_config(const char *var, const char *value,
473481
}
474482

475483
if (!strcmp(var, "diff.wserrorhighlight")) {
476-
int val = parse_ws_error_highlight(value);
484+
int val;
485+
if (!value)
486+
return config_error_nonbool(var);
487+
val = parse_ws_error_highlight(value);
477488
if (val < 0)
478489
return -1;
479490
ws_error_highlight_default = val;
@@ -490,6 +501,8 @@ int git_diff_basic_config(const char *var, const char *value,
490501

491502
if (!strcmp(var, "diff.dirstat")) {
492503
struct strbuf errmsg = STRBUF_INIT;
504+
if (!value)
505+
return config_error_nonbool(var);
493506
default_diff_options.dirstat_permille = diff_dirstat_permille_default;
494507
if (parse_dirstat_params(&default_diff_options, value, &errmsg))
495508
warning(_("Found errors in 'diff.dirstat' config variable:\n%s"),

mailinfo.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,6 +1253,8 @@ static int git_mailinfo_config(const char *var, const char *value,
12531253
return 0;
12541254
}
12551255
if (!strcmp(var, "mailinfo.quotedcr")) {
1256+
if (!value)
1257+
return config_error_nonbool(var);
12561258
if (mailinfo_parse_quoted_cr_action(value, &mi->quoted_cr) != 0)
12571259
return error(_("bad action '%s' for '%s'"), value, var);
12581260
return 0;

notes-utils.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ static int notes_rewrite_config(const char *k, const char *v,
112112
}
113113
return 0;
114114
} else if (!c->refs_from_env && !strcmp(k, "notes.rewriteref")) {
115+
if (!v)
116+
return config_error_nonbool(k);
115117
/* note that a refs/ prefix is implied in the
116118
* underlying for_each_glob_ref */
117119
if (starts_with(v, "refs/notes/"))

0 commit comments

Comments
 (0)