From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 15296 invoked by alias); 11 Apr 2009 15:19:29 -0000 Received: (qmail 15286 invoked by uid 22791); 11 Apr 2009 15:19:28 -0000 X-SWARE-Spam-Status: No, hits=-2.4 required=5.0 tests=AWL,BAYES_00,SPF_PASS X-Spam-Check-By: sourceware.org Received: from mail.codesourcery.com (HELO mail.codesourcery.com) (65.74.133.4) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Sat, 11 Apr 2009 15:19:24 +0000 Received: (qmail 15159 invoked from network); 11 Apr 2009 15:19:22 -0000 Received: from unknown (HELO orlando.local) (pedro@127.0.0.2) by mail.codesourcery.com with ESMTPA; 11 Apr 2009 15:19:22 -0000 From: Pedro Alves To: gdb-patches@sourceware.org Subject: Re: [RFC] Ada ARI fix for sprintf Date: Sat, 11 Apr 2009 15:19:00 -0000 User-Agent: KMail/1.9.10 Cc: "Pierre Muller" References: <000401c9b758$2c1cf4b0$8456de10$@u-strasbg.fr> In-Reply-To: <000401c9b758$2c1cf4b0$8456de10$@u-strasbg.fr> MIME-Version: 1.0 Content-Disposition: inline Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Message-Id: <200904111620.17103.pedro@codesourcery.com> X-IsSubscribed: yes Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org X-SW-Source: 2009-04/txt/msg00208.txt.bz2 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