]> git.ipfire.org Git - thirdparty/git.git/commitdiff
rev-parse: fix segfault with missing --path-format argument
authorWolfgang Müller <wolf@oriole.systems>
Mon, 17 May 2021 08:02:42 +0000 (10:02 +0200)
committerJunio C Hamano <gitster@pobox.com>
Mon, 17 May 2021 09:39:29 +0000 (18:39 +0900)
Calling "git rev-parse --path-format" without an argument segfaults
instead of giving an error message. Commit fac60b8925 (rev-parse: add
option for absolute or relative path formatting, 2020-12-13) added the
argument parsing code but forgot to handle NULL.

Returning an error makes sense here because there is no default value we
could use. Add a test case to verify.

Signed-off-by: Wolfgang Müller <wolf@oriole.systems>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/rev-parse.c
t/t1500-rev-parse.sh

index 85bad9052ebcce8e84875cbfb0a5a4da4be7f3ac..7af8dab8bcecdbce21797a317d37ba372c2d4451 100644 (file)
@@ -759,6 +759,8 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
                                continue;
                        }
                        if (opt_with_value(arg, "--path-format", &arg)) {
+                               if (!arg)
+                                       die("--path-format requires an argument");
                                if (!strcmp(arg, "absolute")) {
                                        format = FORMAT_CANONICAL;
                                } else if (!strcmp(arg, "relative")) {
index deae916707f0c1fea99c929ad0863b4aabf86457..1c2df08333bc600e4fc22f1f55c1d6950b876d67 100755 (executable)
@@ -146,6 +146,10 @@ test_expect_success '--path-format can change in the middle of the command line'
        test_cmp expect actual
 '
 
+test_expect_success '--path-format does not segfault without an argument' '
+       test_must_fail git rev-parse --path-format
+'
+
 test_expect_success 'git-common-dir from worktree root' '
        echo .git >expect &&
        git rev-parse --git-common-dir >actual &&