]> git.ipfire.org Git - thirdparty/git.git/commitdiff
diff: don't crash with empty argument to -G or -S
authorbrian m. carlson <sandals@crustytoothpaste.net>
Mon, 17 Feb 2025 17:57:59 +0000 (17:57 +0000)
committerJunio C Hamano <gitster@pobox.com>
Tue, 18 Feb 2025 18:17:02 +0000 (10:17 -0800)
The pickaxe options, -G and -S, need either a regex or a string to look
through the history for.  An empty value isn't very useful since it
would either match everything or nothing, and what's worse, we presently
crash with a BUG like so when the user provides one:

    BUG: diffcore-pickaxe.c:241: should have needle under -G or -S

Since it's not very nice of us to crash and this wouldn't do anything
useful anyway, let's simply inform the user that they must provide a
non-empty argument and exit with an error if they provide an empty one
instead.

Reported-by: Jared Van Bortel <cebtenzzre@gmail.com>
Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
Acked-by: Elijah Newren <newren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
diff.c
t/t4209-log-pickaxe.sh

diff --git a/diff.c b/diff.c
index d28b4114c8dffba5cb5339152d198588550135a0..5a26300ec4a67f593d2a3aa83efeb0b2e7fc5d08 100644 (file)
--- a/diff.c
+++ b/diff.c
@@ -5493,6 +5493,8 @@ static int diff_opt_pickaxe_regex(const struct option *opt,
        BUG_ON_OPT_NEG(unset);
        options->pickaxe = arg;
        options->pickaxe_opts |= DIFF_PICKAXE_KIND_G;
+       if (arg && !*arg)
+               return error(_("-G requires a non-empty argument"));
        return 0;
 }
 
@@ -5504,6 +5506,8 @@ static int diff_opt_pickaxe_string(const struct option *opt,
        BUG_ON_OPT_NEG(unset);
        options->pickaxe = arg;
        options->pickaxe_opts |= DIFF_PICKAXE_KIND_S;
+       if (arg && !*arg)
+               return error(_("-S requires a non-empty argument"));
        return 0;
 }
 
index a675ace0819f2de892f30abfbae2137721bf07b5..0e2f80a268ed0d197a679705e0e959cdc6fab7b9 100755 (executable)
@@ -93,6 +93,22 @@ test_expect_success 'usage: --no-pickaxe-regex' '
        test_cmp expect actual
 '
 
+test_expect_success 'usage: -G and -S with empty argument' '
+       cat >expect <<-\EOF &&
+       error: -S requires a non-empty argument
+       EOF
+
+       test_expect_code 129 git log -S "" 2>actual &&
+       test_cmp expect actual &&
+
+       cat >expect <<-\EOF &&
+       error: -G requires a non-empty argument
+       EOF
+
+       test_expect_code 129 git log -G "" 2>actual &&
+       test_cmp expect actual
+'
+
 test_log       expect_initial  --grep initial
 test_log       expect_nomatch  --grep InItial
 test_log_icase expect_initial  --grep InItial