# [PATCH] update-index: fix segfault with missing --cacheinfo argument

2 messages from 2014-06-04 to 2014-06-04. Participants: Jeff King, Junio C Hamano.
Thread: https://gitlist.dev/t/36832

## Jeff King, 2014-06-04 07:11

Subject: [PATCH] update-index: fix segfault with missing --cacheinfo argument
Message-ID: <20140604071110.GA22158@sigill.intra.peff.net>
URL: https://gitlist.dev/e/20140604071110.GA22158%40sigill.intra.peff.net

```
Running "git update-index --cacheinfo" without any further
arguments results in a segfault rather than an error
message. Commit ec160ae (update-index: teach --cacheinfo a
new syntax "mode,sha1,path", 2014-03-23) added code to
examine the format of the argument, but forgot to handle the
NULL case.

Returning an error from the parser is enough, since we then
treat it as an old-style "--cacheinfo <mode> <sha1> <path>",
and complain that we have less than 3 arguments to read.

Signed-off-by: Jeff King <peff@peff.net>
---
 builtin/update-index.c        | 3 +++
 t/t2107-update-index-basic.sh | 4 ++++
 2 files changed, 7 insertions(+)

diff --git a/builtin/update-index.c b/builtin/update-index.c
index ba54e19..ebea285 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -637,6 +637,9 @@ static int parse_new_style_cacheinfo(const char *arg,
 	unsigned long ul;
 	char *endp;
 
+	if (!arg)
+		return -1;
+
 	errno = 0;
 	ul = strtoul(arg, &endp, 8);
 	if (errno || endp == arg || *endp != ',' || (unsigned int) ul != ul)
diff --git a/t/t2107-update-index-basic.sh b/t/t2107-update-index-basic.sh
index fe2fb17..1bafb90 100755
--- a/t/t2107-update-index-basic.sh
+++ b/t/t2107-update-index-basic.sh
@@ -29,6 +29,10 @@ test_expect_success 'update-index -h with corrupt index' '
 	test_i18ngrep "[Uu]sage: git update-index" broken/usage
 '
 
+test_expect_success '--cacheinfo complains of missing arguments' '
+	test_must_fail git update-index --cacheinfo
+'
+
 test_expect_success '--cacheinfo does not accept blob null sha1' '
 	echo content >file &&
 	git add file &&
-- 
2.0.0.rc1.436.g03cb729

```

## Junio C Hamano, 2014-06-04 18:03

Subject: Re: [PATCH] update-index: fix segfault with missing --cacheinfo argument
Message-ID: <xmqqioogv9ge.fsf@gitster.dls.corp.google.com>
URL: https://gitlist.dev/e/xmqqioogv9ge.fsf%40gitster.dls.corp.google.com
In-Reply-To: <20140604071110.GA22158@sigill.intra.peff.net>

```
Jeff King <peff@peff.net> writes:

> Running "git update-index --cacheinfo" without any further
> arguments results in a segfault rather than an error
> message. Commit ec160ae (update-index: teach --cacheinfo a
> new syntax "mode,sha1,path", 2014-03-23) added code to
> examine the format of the argument, but forgot to handle the
> NULL case.
>
> Returning an error from the parser is enough, since we then
> treat it as an old-style "--cacheinfo <mode> <sha1> <path>",
> and complain that we have less than 3 arguments to read.
>
> Signed-off-by: Jeff King <peff@peff.net>
> ---

Thanks.

```
