Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Pedro Alves <pedro@codesourcery.com>
To: gdb-patches@sourceware.org
Cc: "Pierre Muller" <muller@ics.u-strasbg.fr>
Subject: Re: [RFC] Ada ARI fix for sprintf
Date: Sat, 11 Apr 2009 15:19:00 -0000	[thread overview]
Message-ID: <200904111620.17103.pedro@codesourcery.com> (raw)
In-Reply-To: <000401c9b758$2c1cf4b0$8456de10$@u-strasbg.fr>

On Tuesday 07 April 2009 09:09:27, Pierre Muller wrote:
> The reason this is a RFC rather than a RFA
> is that I used another function than
> the one recommended on ARI page.

> Currently AR Index gives 153 failures for "sprint" rule.
> sprintf	153	Do not use sprintf, instead use xstrprintf
> But they seem to all have local static buffers
> whereas xstrprintf allocates memory globally.

Well, you don't have to follow those rules literally.
The important point is in not using a function that
can overflow, like sprintf.

> 	* ada-exp.y (convert_char_literal): Replace sprintf by xsnprintf.
> 	* ada-lang.c (add_angle_bracket): Ditto.
                                       ^ typo, add_angle_brackets.

> 	(ada_decode, find_old_style_renaming_symbol): Ditto.
> 	(ada_to_fixed_type_1, ada_enum_name): Ditto.
> 

> Index: ada-lang.c
> ===================================================================
> RCS file: /cvs/src/src/gdb/ada-lang.c,v
> retrieving revision 1.205
> diff -u -p -r1.205 ada-lang.c
> --- ada-lang.c	24 Mar 2009 02:07:06 -0000	1.205
> +++ ada-lang.c	7 Apr 2009 08:01:28 -0000
> @@ -335,11 +335,13 @@ static char *
>  add_angle_brackets (const char *str)
>  {
>    static char *result = NULL;
> +  int result_size;
>  
>    xfree (result);
> -  result = (char *) xmalloc ((strlen (str) + 3) * sizeof (char));
> +  result_size = (strlen (str) + 3) * sizeof (char);
> +  result = (char *) xmalloc (result_size);
>  
> -  sprintf (result, "<%s>", str);
> +  xsnprintf (result, result_size, "<%s>", str);
>    return result;
>  }

This function is already allocating the buffer from the heap, so,
if you used xstrprintf, the code would become simpler, as you
would get rid of the xmalloc, and the need to compute result_size
yourself.  Something like:

 static char *
 add_angle_brackets (const char *str)
 {
   static char *result = NULL;
 
   xfree (result);
   result = xstrprintf ("<%s>", str);
   return result;
 }

Although, this function is used in the symbol completer, and
I don't know if there's a speed difference in using
xstrprintf over xmalloc+snprintf, and if it makes a difference
in this case.  Probably not somethind to worry about
though...  Joel?

All other cases looked good to me.

-- 
Pedro Alves


  reply	other threads:[~2009-04-11 15:19 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-04-07  8:09 Pierre Muller
2009-04-11 15:19 ` Pedro Alves [this message]
2009-04-14 18:24   ` Joel Brobecker
2009-04-14 18:39     ` [RFA] Ada ARI fix for sprintf version 2 Pierre Muller
2009-04-14 18:56       ` Joel Brobecker
2009-04-14 19:22         ` Pierre Muller

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=200904111620.17103.pedro@codesourcery.com \
    --to=pedro@codesourcery.com \
    --cc=gdb-patches@sourceware.org \
    --cc=muller@ics.u-strasbg.fr \
    /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