I: [PATCH] few asprintf error handling fixes

Dmitry V. Levin ldv@altlinux.org
Fri May 7 16:05:00 GMT 2004


Hi,

There are still few non-critical places where result of asprintf(3)
call is not checked.  Proposed patch is attached.


-- 
ldv
-------------- next part --------------
2004-05-07  Dmitry V. Levin  <ldv@altlinux.org>

	* argp/argp-help.c (__argp_error, __argp_failure): Check result
	of __asprintf call and don't use string if it failed.
	* stdio-common/psignal.c (psignal): Likewise.
	* locale/programs/localedef.c (more_help): Likewise.
	* resolv/res_hconf.c (arg_service_list, arg_trimdomain_list,
	arg_bool, parse_line): Check result of __asprintf calls and
	don't use string if they failed.
	* sunrpc/svc_simple.c (registerrpc, universal): Likewise.
	* elf/ldconfig.c (parse_conf_include): Check result of __asprintf
	call and exit if it failed.

diff -uprk.orig glibc-2.3.3-200405070000.orig/argp/argp-help.c glibc-2.3.3-200405070000/argp/argp-help.c
--- glibc-2.3.3-200405070000.orig/argp/argp-help.c	2004-04-14 17:39:05 +0000
+++ glibc-2.3.3-200405070000/argp/argp-help.c	2004-05-07 13:59:14 +0000
@@ -1768,7 +1768,8 @@ __argp_error (const struct argp_state *s
 	    {
 	      char *buf;
 
-	      __asprintf (&buf, fmt, ap);
+	      if (__asprintf (&buf, fmt, ap) < 0)
+		buf = NULL;
 
 	      __fwprintf (stream, L"%s: %s\n",
 			  state ? state->name : __argp_short_program_name (),
@@ -1846,7 +1847,8 @@ __argp_failure (const struct argp_state 
 		{
 		  char *buf;
 
-		  __asprintf (&buf, fmt, ap);
+		  if (__asprintf (&buf, fmt, ap) < 0)
+		    buf = NULL;
 
 		  __fwprintf (stream, L": %s", buf);
 
diff -uprk.orig glibc-2.3.3-200405070000.orig/stdio-common/psignal.c glibc-2.3.3-200405070000/stdio-common/psignal.c
--- glibc-2.3.3-200405070000.orig/stdio-common/psignal.c	2004-04-14 17:39:52 +0000
+++ glibc-2.3.3-200405070000/stdio-common/psignal.c	2004-05-07 14:39:14 +0000
@@ -57,7 +57,13 @@ psignal (int sig, const char *s)
     {
       char *buf;
 
-      (void) __asprintf (&buf, _("%s%sUnknown signal %d\n"), s, colon, sig);
+      if (__asprintf (&buf, _("%s%sUnknown signal %d\n"), s, colon, sig) < 0)
+	{
+	  if (_IO_fwide (stderr, 0) > 0)
+	    (void) __fwprintf (stderr, L"%s%s%s\n", s, colon, _("Unknown signal"));
+	  else
+	    (void) fprintf (stderr, "%s%s%s\n", s, colon, _("Unknown signal"));
+	}
 
       if (_IO_fwide (stderr, 0) > 0)
 	(void) __fwprintf (stderr, L"%s",  buf);
diff -uprk.orig glibc-2.3.3-200405070000.orig/elf/ldconfig.c glibc-2.3.3-200405070000/elf/ldconfig.c
--- glibc-2.3.3-200405070000.orig/elf/ldconfig.c	2004-04-14 17:39:15 +0000
+++ glibc-2.3.3-200405070000/elf/ldconfig.c	2004-05-07 14:51:23 +0000
@@ -1034,7 +1034,9 @@ parse_conf_include (const char *config_f
   char *copy = NULL;
   if (pattern[0] != '/' && strchr (config_file, '/') != NULL)
     {
-      asprintf (&copy, "%s/%s", dirname (strdupa (config_file)), pattern);
+      if (asprintf (&copy, "%s/%s", dirname (strdupa (config_file)),
+		    pattern) < 0)
+	error (EXIT_FAILURE, 0, _("memory exhausted"));
       pattern = copy;
     }
 
diff -uprk.orig glibc-2.3.3-200405070000.orig/locale/programs/localedef.c glibc-2.3.3-200405070000/locale/programs/localedef.c
--- glibc-2.3.3-200405070000.orig/locale/programs/localedef.c	2004-04-14 17:39:29 +0000
+++ glibc-2.3.3-200405070000/locale/programs/localedef.c	2004-05-07 14:04:40 +0000
@@ -364,13 +364,14 @@ more_help (int key, const char *text, vo
     {
     case ARGP_KEY_HELP_EXTRA:
       /* We print some extra information.  */
-      asprintf (&cp, gettext ("\
+      if (asprintf (&cp, gettext ("\
 System's directory for character maps : %s\n\
                        repertoire maps: %s\n\
                        locale path    : %s\n\
 %s"),
 		CHARMAP_PATH, REPERTOIREMAP_PATH, LOCALE_PATH, gettext ("\
-Report bugs using the `glibcbug' script to <bugs@gnu.org>.\n"));
+Report bugs using the `glibcbug' script to <bugs@gnu.org>.\n")) < 0)
+	cp = NULL;
       return cp;
     default:
       break;
diff -uprk.orig glibc-2.3.3-200405070000.orig/resolv/res_hconf.c glibc-2.3.3-200405070000/resolv/res_hconf.c
--- glibc-2.3.3-200405070000.orig/resolv/res_hconf.c	2004-05-07 12:20:29 +0000
+++ glibc-2.3.3-200405070000/resolv/res_hconf.c	2004-05-07 13:59:14 +0000
@@ -145,8 +145,9 @@ arg_service_list (const char *fname, int
 	{
 	  char *buf;
 
-	  __asprintf (&buf, _("%s: line %d: expected service, found `%s'\n"),
-		      fname, line_num, start);
+	  if (__asprintf (&buf, _("%s: line %d: expected service, found `%s'\n"),
+		      fname, line_num, start) < 0)
+	    return 0;
 
 #ifdef USE_IN_LIBIO
 	  if (_IO_fwide (stderr, 0) > 0)
@@ -162,9 +163,10 @@ arg_service_list (const char *fname, int
 	{
 	  char *buf;
 
-	  __asprintf (&buf,
+	  if (__asprintf (&buf,
 		      _("%s: line %d: cannot specify more than %d services"),
-		      fname, line_num, SERVICE_MAX);
+		      fname, line_num, SERVICE_MAX) < 0)
+	    return 0;
 
 #ifdef USE_IN_LIBIO
 	  if (_IO_fwide (stderr, 0) > 0)
@@ -189,9 +191,10 @@ arg_service_list (const char *fname, int
 	    {
 	      char *buf;
 
-	      __asprintf (&buf, _("\
+	      if (__asprintf (&buf, _("\
 %s: line %d: list delimiter not followed by keyword"),
-			  fname, line_num);
+			  fname, line_num) < 0)
+		return 0;
 
 #ifdef USE_IN_LIBIO
 	      if (_IO_fwide (stderr, 0) > 0)
@@ -229,9 +232,10 @@ arg_trimdomain_list (const char *fname, 
 	{
 	  char *buf;
 
-	  __asprintf (&buf, _("\
+	  if (__asprintf (&buf, _("\
 %s: line %d: cannot specify more than %d trim domains"),
-		      fname, line_num, TRIMDOMAINS_MAX);
+		      fname, line_num, TRIMDOMAINS_MAX) < 0)
+	    return 0;
 
 #ifdef USE_IN_LIBIO
 	      if (_IO_fwide (stderr, 0) > 0)
@@ -254,9 +258,10 @@ arg_trimdomain_list (const char *fname, 
 	    {
 	      char *buf;
 
-	      __asprintf (&buf, _("\
+	      if (__asprintf (&buf, _("\
 %s: line %d: list delimiter not followed by domain"),
-			  fname, line_num);
+			  fname, line_num) < 0)
+		return 0;
 
 #ifdef USE_IN_LIBIO
 	      if (_IO_fwide (stderr, 0) > 0)
@@ -316,9 +321,10 @@ arg_bool (const char *fname, int line_nu
     {
       char *buf;
 
-      __asprintf (&buf,
+      if (__asprintf (&buf,
 		  _("%s: line %d: expected `on' or `off', found `%s'\n"),
-		  fname, line_num, args);
+		  fname, line_num, args) < 0)
+	return 0;
 
 #ifdef USE_IN_LIBIO
       if (_IO_fwide (stderr, 0) > 0)
@@ -364,8 +370,9 @@ parse_line (const char *fname, int line_
     {
       char *buf;
 
-      __asprintf (&buf, _("%s: line %d: bad command `%s'\n"),
-		  fname, line_num, start);
+      if (__asprintf (&buf, _("%s: line %d: bad command `%s'\n"),
+		  fname, line_num, start) < 0)
+	return;
 
 #ifdef USE_IN_LIBIO
       if (_IO_fwide (stderr, 0) > 0)
@@ -392,9 +399,10 @@ parse_line (const char *fname, int line_
 	  {
 	    char *buf;
 
-	    __asprintf (&buf,
+	    if (__asprintf (&buf,
 			_("%s: line %d: ignoring trailing garbage `%s'\n"),
-			fname, line_num, str);
+			fname, line_num, str) < 0)
+	      break;
 
 #ifdef USE_IN_LIBIO
 	    if (_IO_fwide (stderr, 0) > 0)
diff -uprk.orig glibc-2.3.3-200405070000.orig/sunrpc/svc_simple.c glibc-2.3.3-200405070000/sunrpc/svc_simple.c
--- glibc-2.3.3-200405070000.orig/sunrpc/svc_simple.c	2004-02-10 08:32:01 +0000
+++ glibc-2.3.3-200405070000/sunrpc/svc_simple.c	2004-05-07 14:40:40 +0000
@@ -84,8 +84,9 @@ registerrpc (u_long prognum, u_long vers
   if (procnum == NULLPROC)
     {
 
-      (void) __asprintf (&buf, _("can't reassign procedure number %ld\n"),
-			 NULLPROC);
+      if (__asprintf (&buf, _("can't reassign procedure number %ld\n"),
+			 NULLPROC) < 0)
+	buf = NULL;
       goto err_out;
     }
   if (transp == 0)
@@ -101,8 +102,9 @@ registerrpc (u_long prognum, u_long vers
   if (!svc_register (transp, (u_long) prognum, (u_long) versnum,
 		     universal, IPPROTO_UDP))
     {
-      (void) __asprintf (&buf, _("couldn't register prog %ld vers %ld\n"),
-			 prognum, versnum);
+      if (__asprintf (&buf, _("couldn't register prog %ld vers %ld\n"),
+			 prognum, versnum) < 0)
+	buf = NULL;
       goto err_out;
     }
   pl = (struct proglst_ *) malloc (sizeof (struct proglst_));
@@ -121,6 +123,8 @@ registerrpc (u_long prognum, u_long vers
   return 0;
 
  err_out:
+  if (buf == NULL)
+    return -1;
 #ifdef USE_IN_LIBIO
   if (_IO_fwide (stderr, 0) > 0)
     (void) __fwprintf (stderr, L"%s", buf);
@@ -171,16 +175,21 @@ universal (struct svc_req *rqstp, SVCXPR
 	  return;
 	if (!INTUSE(svc_sendreply) (transp_l, pl->p_outproc, outdata))
 	  {
-	    (void) __asprintf (&buf,
+	    if (__asprintf (&buf,
 			       _("trouble replying to prog %d\n"),
-			       pl->p_prognum);
-	    exit (1);
+			       pl->p_prognum) < 0)
+	      buf = NULL;
+	    goto err_out2;
 	  }
 	/* free the decoded arguments */
 	(void) svc_freeargs (transp_l, pl->p_inproc, xdrbuf);
 	return;
       }
-  (void) __asprintf (&buf, _("never registered prog %d\n"), prog);
+  if (__asprintf (&buf, _("never registered prog %d\n"), prog) < 0)
+    buf = NULL;
+ err_out2:
+  if (buf == NULL)
+    exit (1);
 #ifdef USE_IN_LIBIO
   if (_IO_fwide (stderr, 0) > 0)
     __fwprintf (stderr, L"%s", buf);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: <http://sourceware.org/pipermail/libc-alpha/attachments/20040507/fe8ec4d9/attachment.sig>


More information about the Libc-alpha mailing list