]> git.ipfire.org Git - thirdparty/git.git/commit
diff: reject negative values for -U/--unified
authorMichael Montalbo <mmontalbo@gmail.com>
Tue, 12 May 2026 18:10:21 +0000 (18:10 +0000)
committerJunio C Hamano <gitster@pobox.com>
Wed, 13 May 2026 01:13:26 +0000 (10:13 +0900)
commit94a9e6934c5978a150de549046e68dad608bcf9f
treef59688d572ecc658530cba19655960da7944e26e
parent321f0ea17b3bcb70867fad430e972548281803f0
diff: reject negative values for -U/--unified

Passing a negative value to -U is silently accepted and produces
corrupt unified diff output with malformed hunk headers:

    $ git log -1 -p -U-500 -- GIT-VERSION-GEN | grep '^@@'
    @@ -503,999- +503,999- @@

Line 503 of a 106-line file, count "999-" is not a valid integer.

The config variable diff.context already rejects negative values, but
the command line callback diff_opt_unified() uses strtol() with no
range check.

Change the type of diff_options.context and its static default from
int to unsigned int, matching the change to interhunkcontext in the
previous commit. The type change requires reworking the callback and
config parsing to validate in a local variable before assigning to
the now-unsigned field.

Unlike --inter-hunk-context which could be converted to OPT_UNSIGNED,
-U needs OPT_CALLBACK_F for PARSE_OPT_OPTARG (bare -U with no value
enables patch output). Add a range check in the callback instead.

Signed-off-by: Michael Montalbo <mmontalbo@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
diff.c
diff.h
t/t4055-diff-context.sh