builtin_diff(): read $GIT_DIFF_OPTS closer to use

The value returned by getenv() is not guaranteed to remain valid across other environment function calls. But in between our call and using the value, we run fill_textconv(), which may do quite a bit of work, including spawning sub-processes. We can make this safer by calling getenv() right before we actually look at its value. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jan 11, 2019 at 17:17 UTC 0da0e9268b4825cafb27bb0e07b43fae30bb33da
1 file changed +4 -1
diff.c
+4 -1
@@ -3444,7 +3444,7 @@ static void builtin_diff(const char *name_a,
3444 o->found_changes = 1;
3445 } else {
3446 /* Crazy xdl interfaces.. */
3447 - const char *diffopts = getenv("GIT_DIFF_OPTS");
3447 + const char *diffopts;
3448 const char *v;
3449 xpparam_t xpp;
3450 xdemitconf_t xecfg;
@@ -3487,12 +3487,15 @@ static void builtin_diff(const char *name_a,
3487 xecfg.flags |= XDL_EMIT_FUNCCONTEXT;
3488 if (pe)
3489 xdiff_set_find_func(&xecfg, pe->pattern, pe->cflags);
3490 +
3491 + diffopts = getenv("GIT_DIFF_OPTS");
3492 if (!diffopts)
3493 ;
3494 else if (skip_prefix(diffopts, "--unified=", &v))
3495 xecfg.ctxlen = strtoul(v, NULL, 10);
3496 else if (skip_prefix(diffopts, "-u", &v))
3497 xecfg.ctxlen = strtoul(v, NULL, 10);
3498 +
3499 if (o->word_diff)
3500 init_diff_words_data(&ecbdata, o, one, two);
3501 if (xdi_diff_outf(&mf1, &mf2, fn_out_consume, &ecbdata,