git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: "Wolfgang Müller" <wolf@oriole.systems>
To: git@vger.kernel.org
Cc: "Wolfgang Müller" <wolf@oriole.systems>
Subject: [RFC PATCH] rev-parse: fix segfault with missing --path-format argument
Date: Sun, 16 May 2021 14:04:49 +0200	[thread overview]
Message-ID: <20210516120449.661636-1-wolf@oriole.systems> (raw)

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>
---
Since this is my first contribution to the project and I'm still
learning the ropes, I left this patch as RFC.

For a bit of background information, I ran into this expecting the
following to work:

	git rev-parse --path-format relative --show-toplevel

I'm unsure how many git subcommands specifically require "=" between the
option and the argument, but before now I always expected things to
"just work" when leaving it out.

This fix is based on maint.

Thanks for your time and attention.

 builtin/rev-parse.c  | 2 ++
 t/t1500-rev-parse.sh | 4 ++++
 2 files changed, 6 insertions(+)

diff --git a/builtin/rev-parse.c b/builtin/rev-parse.c
index 85bad9052e..7af8dab8bc 100644
--- a/builtin/rev-parse.c
+++ b/builtin/rev-parse.c
@@ -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")) {
diff --git a/t/t1500-rev-parse.sh b/t/t1500-rev-parse.sh
index deae916707..a1a8ce5265 100755
--- a/t/t1500-rev-parse.sh
+++ b/t/t1500-rev-parse.sh
@@ -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 --show-toplevel
+'
+
 test_expect_success 'git-common-dir from worktree root' '
 	echo .git >expect &&
 	git rev-parse --git-common-dir >actual &&
-- 
2.31.1


             reply	other threads:[~2021-05-16 12:14 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-16 12:04 Wolfgang Müller [this message]
2021-05-16 12:53 ` [RFC PATCH] rev-parse: fix segfault with missing --path-format argument Junio C Hamano
2021-05-16 14:31   ` Wolfgang Müller
2021-05-16 21:59     ` Junio C Hamano
2021-05-17  7:19       ` Wolfgang Müller
2021-05-17  8:02 ` [PATCH v2 0/2] rev-parse: Fix segfault and translate messages Wolfgang Müller
2021-05-17  8:02   ` [PATCH v2 1/2] rev-parse: fix segfault with missing --path-format argument Wolfgang Müller
2021-05-17  8:16     ` Jeff King
2021-05-19  9:52       ` Wolfgang Müller
2021-05-19 10:19         ` Wolfgang Müller
2021-05-19 14:21         ` Jeff King
2021-05-17  8:02   ` [PATCH v2 2/2] rev-parse: Mark die() messages for translation Wolfgang Müller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210516120449.661636-1-wolf@oriole.systems \
    --to=wolf@oriole.systems \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).