From: Junio C Hamano <gitster@pobox•com>
To: David Sharp <dhsharp@google•com>
Cc: git@vger•kernel.org
Subject: Re: [PATCH v2] rev-parse: Check argc before using argv[i+1]
Date: Tue, 28 Jan 2014 13:43:56 -0800 [thread overview]
Message-ID: <xmqqmwiflr37.fsf@gitster.dls.corp.google.com> (raw)
In-Reply-To: <1390944060-783-1-git-send-email-dhsharp@google.com> (David Sharp's message of "Tue, 28 Jan 2014 13:21:00 -0800")
David Sharp <dhsharp@google•com> writes:
> Without this patch, git-rev-parse --prefix, --default, or
> --resolve-git-dir, without a value argument, would result in a segfault.
> Instead, die() with a message.
When I sent the review message, I actually was on the fence between
checking i vs argc and checking the nullness myself. I realize,
after seeing the actual patch below, that we are protecting against
picking up a NULL and blindly passing it on in the codepaths that
follow, so the updated code does look a lot better, at least to me.
Thanks.
>
> Signed-off-by: David Sharp <dhsharp@google•com>
> ---
> builtin/rev-parse.c | 17 +++++++++++------
> 1 file changed, 11 insertions(+), 6 deletions(-)
>
> diff --git a/builtin/rev-parse.c b/builtin/rev-parse.c
> index aaeb611..45901df 100644
> --- a/builtin/rev-parse.c
> +++ b/builtin/rev-parse.c
> @@ -547,15 +547,17 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
> continue;
> }
> if (!strcmp(arg, "--default")) {
> - def = argv[i+1];
> - i++;
> + def = argv[++i];
> + if (!def)
> + die("--default requires an argument");
> continue;
> }
> if (!strcmp(arg, "--prefix")) {
> - prefix = argv[i+1];
> + prefix = argv[++i];
> + if (!prefix)
> + die("--prefix requires an argument");
> startup_info->prefix = prefix;
> output_prefix = 1;
> - i++;
> continue;
> }
> if (!strcmp(arg, "--revs-only")) {
> @@ -738,9 +740,12 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
> continue;
> }
> if (!strcmp(arg, "--resolve-git-dir")) {
> - const char *gitdir = resolve_gitdir(argv[i+1]);
> + const char *gitdir = argv[++i];
> if (!gitdir)
> - die("not a gitdir '%s'", argv[i+1]);
> + die("--resolve-git-dir requires an argument");
> + gitdir = resolve_gitdir(gitdir);
> + if (!gitdir)
> + die("not a gitdir '%s'", argv[i]);
> puts(gitdir);
> continue;
> }
next prev parent reply other threads:[~2014-01-28 21:44 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-01-27 23:44 [PATCH] rev-parse: Check argc before using argv[i+1] David Sharp
2014-01-28 19:12 ` Junio C Hamano
2014-01-28 21:20 ` David Sharp
2014-01-28 21:21 ` [PATCH v2] " David Sharp
2014-01-28 21:43 ` Junio C Hamano [this message]
2014-01-28 22:02 ` David Sharp
2014-01-28 22:01 ` Johannes Sixt
2014-01-28 22:03 ` David Sharp
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=xmqqmwiflr37.fsf@gitster.dls.corp.google.com \
--to=gitster@pobox$(echo .)com \
--cc=dhsharp@google$(echo .)com \
--cc=git@vger$(echo .)kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox