git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/4] diff: reject negative values for -U/--unified

From
Michael Montalbo via GitGitGadget <gitgitgadget@gmail.com>
Date
May 5, 2026, 23:02 UTC
Message-ID
<f0478d434ce43e51fd92792c3e583df8a515bf05.1778022144.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.2105.git.1778022144.gitgitgadget@gmail.com>
From: Michael Montalbo <mmontalbo@gmail.com>

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>
---
 diff.c                  | 12 ++++++++----
 diff.h                  |  2 +-
 t/t4055-diff-context.sh |  5 +++++
 3 files changed, 14 insertions(+), 5 deletions(-)
diff --git a/diff.c b/diff.c
index 5df28e49c5..1771b2c444 100644
--- a/diff.c
+++ b/diff.c
@@ -60,7 +60,7 @@ static int diff_suppress_blank_empty;
 static enum git_colorbool diff_use_color_default = GIT_COLOR_UNKNOWN;
 static int diff_color_moved_default;
 static int diff_color_moved_ws_default;
-static int diff_context_default = 3;
+static unsigned int diff_context_default = 3;
 static unsigned int diff_interhunk_context_default;
 static char *diff_word_regex_cfg;
 static struct external_diff external_diff_cfg;
@@ -382,9 +382,10 @@ int git_diff_ui_config(const char *var, const char *value,
 		return 0;
 	}
 	if (!strcmp(var, "diff.context")) {
-		diff_context_default = git_config_int(var, value, ctx->kvi);
-		if (diff_context_default < 0)
+		int val = git_config_int(var, value, ctx->kvi);
+		if (val < 0)
 			return -1;
+		diff_context_default = val;
 		return 0;
 	}
 	if (!strcmp(var, "diff.interhunkcontext")) {
@@ -5924,9 +5925,12 @@ static int diff_opt_unified(const struct option *opt,
 	BUG_ON_OPT_NEG(unset);
 
 	if (arg) {
-		options->context = strtol(arg, &s, 10);
+		long val = strtol(arg, &s, 10);
 		if (*s)
 			return error(_("%s expects a numerical value"), "--unified");
+		if (val < 0)
+			return error(_("%s expects a non-negative integer"), "--unified");
+		options->context = val;
 	}
 	enable_patch_output(&options->output_format);
 
diff --git a/diff.h b/diff.h
index 033d633db4..bb5cddaf34 100644
--- a/diff.h
+++ b/diff.h
@@ -294,7 +294,7 @@ struct diff_options {
 	enum git_colorbool use_color;
 
 	/* Number of context lines to generate in patch output. */
-	int context;
+	unsigned int context;
 
 	unsigned int interhunkcontext;
 
diff --git a/t/t4055-diff-context.sh b/t/t4055-diff-context.sh
index 1384a81957..b26f6eea7c 100755
--- a/t/t4055-diff-context.sh
+++ b/t/t4055-diff-context.sh
@@ -82,6 +82,11 @@ test_expect_success 'negative integer config parsing' '
 	test_grep "bad config variable" output
 '
 
+test_expect_success '-U-1 is rejected' '
+	test_must_fail git diff -U-1 2>err &&
+	test_grep "expects a non-negative integer" err
+'
+
 test_expect_success '-U0 is valid, so is diff.context=0' '
 	test_config diff.context 0 &&
 	git diff >output &&
-- 
gitgitgadget
Previous: Michael Montalbo via GitGitGadgetNext: Michael Montalbo via GitGitGadget
Message 3 of 15 in “diff: reject negative context values”
  1. 0/4 diff: reject negative context valuesMichael Montalbo via GitGitGadget, May 5, 2026
  2. 1/4 diff: reject negative values for --inter-hunk-contextMichael Montalbo via GitGitGadget, May 5, 2026
  3. 2/4 diff: reject negative values for -U/--unifiedMichael Montalbo via GitGitGadget, May 5, 2026
  4. 3/4 xdiff: guard against negative context lengthsMichael Montalbo via GitGitGadget, May 5, 2026
  5. 4/4 parse-options: clarify PARSE_OPT_NONEG does not reject negative numbersMichael Montalbo via GitGitGadget, May 5, 2026
  6. Junio C HamanoMay 9, 2026
  7. Michael MontalboMay 10, 2026
  8. Junio C HamanoMay 10, 2026
  9. Michael MontalboMay 10, 2026
  10. 0/4 diff: reject negative context valuesMichael Montalbo via GitGitGadget, May 12, 2026
  11. 1/4 diff: reject negative values for --inter-hunk-contextMichael Montalbo via GitGitGadget, May 12, 2026
  12. 2/4 diff: reject negative values for -U/--unifiedMichael Montalbo via GitGitGadget, May 12, 2026
  13. 3/4 xdiff: guard against negative context lengthsMichael Montalbo via GitGitGadget, May 12, 2026
  14. 4/4 parse-options: clarify what "negated" means for PARSE_OPT_NONEGMichael Montalbo via GitGitGadget, May 12, 2026
  15. Junio C HamanoMay 13, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.