Received: from mailman by lists.gnu.org with archive (Exim 4.43) id 1Mieyt-0003wn-NJ for mharc-grub-devel@gnu.org; Tue, 01 Sep 2009 21:49:51 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1Mieyr-0003ub-I2 for grub-devel@gnu.org; Tue, 01 Sep 2009 21:49:49 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1Mieym-0003oZ-WF for grub-devel@gnu.org; Tue, 01 Sep 2009 21:49:48 -0400 Received: from [199.232.76.173] (port=60582 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Mieym-0003oO-Ok for grub-devel@gnu.org; Tue, 01 Sep 2009 21:49:44 -0400 Received: from smarthost01.mail.zen.net.uk ([212.23.3.140]:42755) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1Mieym-0005ei-9S for grub-devel@gnu.org; Tue, 01 Sep 2009 21:49:44 -0400 Received: from [82.69.40.219] (helo=riva.pelham.vpn.ucam.org) by smarthost01.mail.zen.net.uk with esmtp (Exim 4.63) (envelope-from ) id 1Mieyi-0000M2-8t for grub-devel@gnu.org; Wed, 02 Sep 2009 01:49:40 +0000 Received: from cjwatson by riva.pelham.vpn.ucam.org with local (Exim 3.36 #1 (Debian)) for grub-devel@gnu.org id 1Mieyh-0005m5-00; Wed, 02 Sep 2009 02:49:39 +0100 Date: Wed, 2 Sep 2009 02:49:39 +0100 From: Colin Watson To: grub-devel@gnu.org Message-ID: <20090902014939.GM13423@riva.ucam.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit User-Agent: Mutt/1.5.18 (2008-05-17) X-Originating-Smarthost01-IP: [82.69.40.219] X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 3) Subject: [PATCH] Introduce xasprintf X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: The development of GRUB 2 List-Id: The development of GRUB 2 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 02 Sep 2009 01:49:50 -0000 Ubuntu builds with -D_FORTIFY_SOURCE=2 by default, and thus produces a bunch of warnings like this: util/grub-mkfont.c: In function ‘write_font’: util/grub-mkfont.c:366: warning: ignoring return value of ‘asprintf’, declared with attribute warn_unused_result I came across the idea of an xasprintf function in Gnulib a while back. Using this both fixes the above class of warnings by always checking the return value of asprintf, and also provides an IMO more intuitive interface to formatting an allocated string. Do people like the following patch? The warnings are minor so there's no problem with this being post-1.97. Index: configure.ac =================================================================== --- configure.ac (revision 2557) +++ configure.ac (working copy) @@ -172,7 +172,7 @@ fi # Check for functions. -AC_CHECK_FUNCS(posix_memalign memalign asprintf) +AC_CHECK_FUNCS(posix_memalign memalign asprintf vasprintf) # # Check for target programs. Index: include/grub/util/misc.h =================================================================== --- include/grub/util/misc.h (revision 2557) +++ include/grub/util/misc.h (working copy) @@ -21,6 +21,7 @@ #include #include +#include #include #include @@ -57,12 +58,20 @@ void grub_util_write_image_at (const void *img, size_t size, off_t offset, FILE *out); +#ifndef HAVE_VASPRINTF + +int vasprintf (char **buf, const char *fmt, va_list ap); + +#endif + #ifndef HAVE_ASPRINTF int asprintf (char **buf, const char *fmt, ...); #endif +char *xasprintf (const char *fmt, ...); + #ifdef __MINGW32__ #define fseeko fseeko64 Index: util/grub-probe.c =================================================================== --- util/grub-probe.c (revision 2558) +++ util/grub-probe.c (working copy) @@ -248,7 +248,7 @@ filebuf_via_sys = grub_util_read_image (path); grub_util_info ("reading %s via GRUB facilities", path); - asprintf (&grub_path, "(%s)%s", drive_name, path); + grub_path = xasprintf ("(%s)%s", drive_name, path); file = grub_file_open (grub_path); filebuf_via_grub = xmalloc (file->size); grub_file_read (file, filebuf_via_grub, file->size); Index: util/getroot.c =================================================================== --- util/getroot.c (revision 2557) +++ util/getroot.c (working copy) @@ -454,7 +454,7 @@ if (q) *q = ','; - asprintf (&grub_dev, "md%s", p); + grub_dev = xasprintf ("md%s", p); free (p); } else if (os_dev[7] == '/' && os_dev[8] == 'd') @@ -469,7 +469,7 @@ if (q) *q = ','; - asprintf (&grub_dev, "md%s", p); + grub_dev = xasprintf ("md%s", p); free (p); } else if (os_dev[7] >= '0' && os_dev[7] <= '9') @@ -482,7 +482,7 @@ if (q) *q = ','; - asprintf (&grub_dev, "md%s", p); + grub_dev = xasprintf ("md%s", p); free (p); } else if (os_dev[7] == '/' && os_dev[8] >= '0' && os_dev[8] <= '9') @@ -495,7 +495,7 @@ if (q) *q = ','; - asprintf (&grub_dev, "md%s", p); + grub_dev = xasprintf ("md%s", p); free (p); } else Index: util/grub-mkfont.c =================================================================== --- util/grub-mkfont.c (revision 2557) +++ util/grub-mkfont.c (working copy) @@ -363,8 +363,8 @@ if (! style_name[0]) strcpy (style_name, " Regular"); - asprintf (&font_name, "%s %s %d", font_info->name, &style_name[1], - font_info->size); + font_name = xasprintf ("%s %s %d", font_info->name, &style_name[1], + font_info->size); write_string_section ("NAME", font_name, &offset, file); write_string_section ("FAMI", font_info->name, &offset, file); Index: util/misc.c =================================================================== --- util/misc.c (revision 2557) +++ util/misc.c (working copy) @@ -28,6 +28,7 @@ #include #include #include +#include #include #include @@ -369,6 +370,19 @@ { } +#ifndef HAVE_VASPRINTF + +int +vasprintf (char **buf, const char *fmt, va_list ap) +{ + /* Should be large enough. */ + *buf = xmalloc (512); + + return vsprintf (*buf, fmt, ap); +} + +#endif + #ifndef HAVE_ASPRINTF int @@ -377,11 +391,8 @@ int status; va_list ap; - /* Should be large enough. */ - *buf = xmalloc (512); - va_start (ap, fmt); - status = vsprintf (*buf, fmt, ap); + status = vasprintf (*buf, fmt, ap); va_end (ap); return status; @@ -389,6 +400,23 @@ #endif +char * +xasprintf (const char *fmt, ...) +{ + va_list ap; + char *result; + + va_start (ap, fmt); + if (vasprintf (&result, fmt, ap) < 0) + { + if (errno == ENOMEM) + grub_util_error ("out of memory"); + return NULL; + } + + return result; +} + #ifdef __MINGW32__ void sync (void) Thanks, -- Colin Watson [cjwatson@ubuntu.com]