git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Martin Ågren" <martin.agren@gmail.com>
To: "René Scharfe" <l.s.r@web.de>
Cc: "Junio C Hamano" <gitster@pobox.com>,
	"Git Mailing List" <git@vger.kernel.org>,
	"Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
Subject: Re: [PATCH] parse-options: use COPY_ARRAY in parse_options_concat()
Date: Wed, 13 Nov 2019 19:25:48 +0100	[thread overview]
Message-ID: <20191113182548.1707-1-martin.agren@gmail.com> (raw)
In-Reply-To: <df941c25-22ea-3523-9607-8e892dea320c@web.de>

On Wed, 13 Nov 2019 at 18:19, René Scharfe <l.s.r@web.de> wrote:
>         ALLOC_ARRAY(ret, st_add3(a_len, b_len, 1));
> -       for (i = 0; i < a_len; i++)
> -               ret[i] = a[i];
> -       for (i = 0; i < b_len; i++)
> -               ret[a_len + i] = b[i];
> +       COPY_ARRAY(ret, a, a_len);
> +       COPY_ARRAY(ret + a_len, b, b_len);
>         ret[a_len + b_len] = b[b_len]; /* final OPTION_END */

Maybe include that last one in the COPY_ARRAY with something like this
on top?

-       COPY_ARRAY(ret + a_len, b, b_len);
-       ret[a_len + b_len] = b[b_len]; /* final OPTION_END */
+       /* 1 more to include the final OPTION_END */
+       COPY_ARRAY(ret + a_len, b, st_add(b_len, 1));

Or maybe even something like the below (directly on top of your patch)?

(Plus, you could drop `i` entirely, but I'm not sure that's worth
golfing on.)

Martin

diff --git a/parse-options-cb.c b/parse-options-cb.c
index 2bde78b726..11196cfb96 100644
--- a/parse-options-cb.c
+++ b/parse-options-cb.c
@@ -185,11 +185,11 @@ struct option *parse_options_concat(struct option *a, struct option *b)
 		a_len++;
 	for (i = 0; b[i].type != OPTION_END; i++)
 		b_len++;
+	b_len++; /* final OPTION_END */
 
-	ALLOC_ARRAY(ret, st_add3(a_len, b_len, 1));
+	ALLOC_ARRAY(ret, st_add(a_len, b_len));
 	COPY_ARRAY(ret, a, a_len);
 	COPY_ARRAY(ret + a_len, b, b_len);
-	ret[a_len + b_len] = b[b_len]; /* final OPTION_END */
 
 	return ret;
 }
-- 
2.24.0


  reply	other threads:[~2019-11-13 18:26 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-12 21:41 parse-options: avoid arithmetic on pointer that's potentially NULL René Scharfe
2019-11-13  2:43 ` Junio C Hamano
2019-11-13 16:48   ` René Scharfe
2019-11-13 18:25     ` Martin Ågren [this message]
2019-11-14 16:01       ` [PATCH] parse-options: use COPY_ARRAY in parse_options_concat() René Scharfe

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=20191113182548.1707-1-martin.agren@gmail.com \
    --to=martin.agren@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=l.s.r@web.de \
    --cc=pclouds@gmail.com \
    /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.
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 NNTP newsgroup(s).