public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: John Baldwin <jhb@FreeBSD.org>
To: Tom Tromey <tromey@adacore.com>, gdb-patches@sourceware.org
Subject: Re: [PATCH] Fix off-by-one in call to vector::reserve
Date: Fri, 18 Aug 2023 09:15:06 -0700	[thread overview]
Message-ID: <3c557b21-d49b-cbc8-de49-237a52f8a2fb@FreeBSD.org> (raw)
In-Reply-To: <20230818140346.1255946-1-tromey@adacore.com>

On 8/18/23 7:03 AM, Tom Tromey via Gdb-patches wrote:
> While looking at a bug, I noticed what I think is an off-by-one
> mistake in a call to vector::reserve.  This code:
> 
>        new_args.reserve (args.size ());
>        new_args.push_back
> 	(value_from_pointer (lookup_pointer_type (values_type), struct_addr));
>        new_args.insert (new_args.end (), args.begin (), args.end ());
> 
> ... reserves 'size()' entries, but then proceeds to push one extra
> one.
> 
> This shouldn't have any really bad effects, as insert will grow the
> vector.  Still, it seems better to use the correct size if we're going
> to bother calling reserve.
> 
> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30780
> ---
>   gdb/infcall.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/gdb/infcall.c b/gdb/infcall.c
> index bea5b185ddc..7e19be79a24 100644
> --- a/gdb/infcall.c
> +++ b/gdb/infcall.c
> @@ -1233,7 +1233,7 @@ call_function_by_hand_dummy (struct value *function,
>     if (return_method == return_method_hidden_param)
>       {
>         /* Add the new argument to the front of the argument list.  */
> -      new_args.reserve (args.size ());
> +      new_args.reserve (args.size () + 1);
>         new_args.push_back
>   	(value_from_pointer (lookup_pointer_type (values_type), struct_addr));
>         new_args.insert (new_args.end (), args.begin (), args.end ());

Pedantically speaking I would probably write it as '1 + args.size()' as the
extra pointer is pushed first followed by a copy of all the items in args
(that is, trying to make the expression list the sub-sizes in the same order
they are added to the vector).

Reviewed-by: John Baldwin <jhb@FreeBSD.org>

-- 
John Baldwin


  reply	other threads:[~2023-08-18 16:15 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-18 14:03 Tom Tromey
2023-08-18 16:15 ` John Baldwin [this message]
2023-08-18 19:02   ` Tom Tromey

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=3c557b21-d49b-cbc8-de49-237a52f8a2fb@FreeBSD.org \
    --to=jhb@freebsd.org \
    --cc=gdb-patches@sourceware.org \
    --cc=tromey@adacore.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 read-only IMAP folder(s) and NNTP newsgroup(s).