Re: [PATCH] setup_revisions(): take pathspec from command line and --stdin correctly

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, May 11, 2011 at 6:44 PM, Junio C Hamano <gitster@xxxxxxxxx> wrote:
> When the command line has "--" disambiguator, we take the remainder of
> argv[] as "prune_data", but when --stdin is given at the same time, we
> need to append to the existing prune_data and end up attempting to
> realloc(3) it, which would not work and can lead to a segfault or worse.
>
> Fix it by consistently using append_prune_data() throughout the input
> processing. ÂAlso avoid counting the number of existing paths in the
> function over and over again.
>
> Signed-off-by: Junio C Hamano <gitster@xxxxxxxxx>
> ---
>
> Â* This is relative to 1.6.6; the bug originates back to 60da8b1 (Make
> Â --stdin option to "log" family read also pathspecs, 2009-11-20).
>
> Ârevision.c        Â|  80 ++++++++++++++++-----------------------------
> Ât/t6017-rev-list-stdin.sh | Â 17 +++++++++
> Â2 files changed, 45 insertions(+), 52 deletions(-)
>
> diff --git a/revision.c b/revision.c
> index c92ffc2..58b5651 100644
> --- a/revision.c
> +++ b/revision.c
> @@ -956,35 +956,34 @@ int handle_revision_arg(const char *arg, struct rev_info *revs,
> Â Â Â Âreturn 0;
> Â}
>
> -static void read_pathspec_from_stdin(struct rev_info *revs, struct strbuf *sb, const char ***prune_data)
> -{
> - Â Â Â const char **prune = *prune_data;
> - Â Â Â int prune_nr;
> - Â Â Â int prune_alloc;
> +struct cmdline_pathspec {
Maybe prune_data_pathspec?

> + Â Â Â int alloc;
> + Â Â Â int nr;
> + Â Â Â const char **path;
> +};
>
> - Â Â Â /* count existing ones */
> - Â Â Â if (!prune)
> - Â Â Â Â Â Â Â prune_nr = 0;
> - Â Â Â else
> - Â Â Â Â Â Â Â for (prune_nr = 0; prune[prune_nr]; prune_nr++)
> - Â Â Â Â Â Â Â Â Â Â Â ;
> - Â Â Â prune_alloc = prune_nr; /* not really, but we do not know */
> +static void append_prune_data(struct cmdline_pathspec *prune, const char **av)
av is the same thing as argv? If so, is it worth naming it argv? I
know the old was av, so maybe not though.

> +{
> + Â Â Â while (*av) {
> + Â Â Â Â Â Â Â ALLOC_GROW(prune->path, prune->nr+1, prune->alloc);
I'd add spaces between prune->nr + 1. But I think is fine as is too.

> + Â Â Â Â Â Â Â prune->path[prune->nr++] = *(av++);
> + Â Â Â }
> +}
>
> +static void read_pathspec_from_stdin(struct rev_info *revs, struct strbuf *sb,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âstruct cmdline_pathspec *prune)
> +{
> Â Â Â Âwhile (strbuf_getwholeline(sb, stdin, '\n') != EOF) {
> Â Â Â Â Â Â Â Âint len = sb->len;
> Â Â Â Â Â Â Â Âif (len && sb->buf[len - 1] == '\n')
> Â Â Â Â Â Â Â Â Â Â Â Âsb->buf[--len] = '\0';
> - Â Â Â Â Â Â Â ALLOC_GROW(prune, prune_nr+1, prune_alloc);
> - Â Â Â Â Â Â Â prune[prune_nr++] = xstrdup(sb->buf);
> + Â Â Â Â Â Â Â ALLOC_GROW(prune->path, prune->nr+1, prune->alloc);
> + Â Â Â Â Â Â Â prune->path[prune->nr++] = xstrdup(sb->buf);
> Â Â Â Â}
> - Â Â Â if (prune) {
> - Â Â Â Â Â Â Â ALLOC_GROW(prune, prune_nr+1, prune_alloc);
> - Â Â Â Â Â Â Â prune[prune_nr] = NULL;
> - Â Â Â }
> - Â Â Â *prune_data = prune;
> Â}
>
> -static void read_revisions_from_stdin(struct rev_info *revs, const char ***prune)
> +static void read_revisions_from_stdin(struct rev_info *revs,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â struct cmdline_pathspec *prune)
> Â{
> Â Â Â Âstruct strbuf sb;
> Â Â Â Âint seen_dashdash = 0;
> @@ -1279,34 +1278,6 @@ static int for_each_good_bisect_ref(each_ref_fn fn, void *cb_data)
> Â Â Â Âreturn for_each_ref_in("refs/bisect/good", fn, cb_data);
> Â}
>
> -static void append_prune_data(const char ***prune_data, const char **av)
> -{
> - Â Â Â const char **prune = *prune_data;
> - Â Â Â int prune_nr;
> - Â Â Â int prune_alloc;
> -
> - Â Â Â if (!prune) {
> - Â Â Â Â Â Â Â *prune_data = av;
> - Â Â Â Â Â Â Â return;
> - Â Â Â }
> -
> - Â Â Â /* count existing ones */
> - Â Â Â for (prune_nr = 0; prune[prune_nr]; prune_nr++)
> - Â Â Â Â Â Â Â ;
> - Â Â Â prune_alloc = prune_nr; /* not really, but we do not know */
> -
> - Â Â Â while (*av) {
> - Â Â Â Â Â Â Â ALLOC_GROW(prune, prune_nr+1, prune_alloc);
> - Â Â Â Â Â Â Â prune[prune_nr++] = *av;
> - Â Â Â Â Â Â Â av++;
> - Â Â Â }
> - Â Â Â if (prune) {
> - Â Â Â Â Â Â Â ALLOC_GROW(prune, prune_nr+1, prune_alloc);
> - Â Â Â Â Â Â Â prune[prune_nr] = NULL;
> - Â Â Â }
> - Â Â Â *prune_data = prune;
> -}
> -
> Â/*
> Â* Parse revision information, filling in the "rev_info" structure,
> Â* and removing the used arguments from the argument list.
> @@ -1317,7 +1288,9 @@ static void append_prune_data(const char ***prune_data, const char **av)
> Âint setup_revisions(int argc, const char **argv, struct rev_info *revs, const char *def)
> Â{
> Â Â Â Âint i, flags, left, seen_dashdash, read_from_stdin;
> - Â Â Â const char **prune_data = NULL;
> + Â Â Â struct cmdline_pathspec prune_data;
> +
> + Â Â Â memset(&prune_data, 0, sizeof(prune_data));
>
> Â Â Â Â/* First, search for "--" */
> Â Â Â Âseen_dashdash = 0;
> @@ -1328,7 +1301,7 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, const ch
> Â Â Â Â Â Â Â Âargv[i] = NULL;
> Â Â Â Â Â Â Â Âargc = i;
> Â Â Â Â Â Â Â Âif (argv[i + 1])
> - Â Â Â Â Â Â Â Â Â Â Â prune_data = argv + i + 1;
> + Â Â Â Â Â Â Â Â Â Â Â append_prune_data(&prune_data, argv + i + 1);
> Â Â Â Â Â Â Â Âseen_dashdash = 1;
> Â Â Â Â Â Â Â Âbreak;
> Â Â Â Â}
> @@ -1420,8 +1393,11 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, const ch
> Â Â Â Â Â Â Â Â}
> Â Â Â Â}
>
> - Â Â Â if (prune_data)
> - Â Â Â Â Â Â Â revs->prune_data = get_pathspec(revs->prefix, prune_data);
> + Â Â Â if (prune_data.nr) {
> + Â Â Â Â Â Â Â ALLOC_GROW(prune_data.path, prune_data.nr+1, prune_data.alloc);
> + Â Â Â Â Â Â Â prune_data.path[prune_data.nr++] = NULL;
> + Â Â Â Â Â Â Â revs->prune_data = get_pathspec(revs->prefix, prune_data.path);
> + Â Â Â }
>
> Â Â Â Âif (revs->def == NULL)
> Â Â Â Â Â Â Â Ârevs->def = def;
> diff --git a/t/t6017-rev-list-stdin.sh b/t/t6017-rev-list-stdin.sh
> index f1c32db..667b375 100755
> --- a/t/t6017-rev-list-stdin.sh
> +++ b/t/t6017-rev-list-stdin.sh
> @@ -58,4 +58,21 @@ check side-3 ^side-4 -- file-3
> Âcheck side-3 ^side-2
> Âcheck side-3 ^side-2 -- file-1
>
> +test_expect_success 'not only --stdin' '
> + Â Â Â cat >expect <<-EOF &&
> + Â Â Â 7
> +
> + Â Â Â file-1
> + Â Â Â file-2
> + Â Â Â EOF
> + Â Â Â cat >input <<-EOF &&
> + Â Â Â ^master^
> + Â Â Â --
> + Â Â Â file-2
> + Â Â Â EOF
> + Â Â Â git log --pretty=tformat:%s --name-only --stdin master -- file-1 \
> + Â Â Â Â Â Â Â <input >actual &&
> + Â Â Â test_cmp expect actual
> +'
> +
> Âtest_done
> --
> 1.7.5.1.315.ge7efa
>
> --
> To unsubscribe from this list: send the line "unsubscribe git" in
> the body of a message to majordomo@xxxxxxxxxxxxxxx
> More majordomo info at Âhttp://vger.kernel.org/majordomo-info.html
>
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]