]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
Fix out-of-memory handling of vasnprintf.
authorBruno Haible <bruno@clisp.org>
Sat, 3 Nov 2007 11:44:07 +0000 (11:44 +0000)
committerBruno Haible <bruno@clisp.org>
Tue, 23 Jun 2009 10:15:29 +0000 (12:15 +0200)
gettext-runtime/intl/ChangeLog
gettext-runtime/intl/printf-parse.c
gettext-runtime/intl/vasnprintf.c
gettext-runtime/libasprintf/ChangeLog
gettext-runtime/libasprintf/printf-parse.c
gettext-runtime/libasprintf/vasnprintf.c

index 711f085712bf9e589188ee89decd58405be8f437..e77142f254ad6eb2ccc7efd9e1161f830819d852 100644 (file)
@@ -1,3 +1,11 @@
+2007-11-03  Bruno Haible  <bruno@clisp.org>
+
+       Fix out-of-memory handling of vasnprintf.
+       * printf-parse.c: Include <errno.h>.
+       (PRINTF_PARSE): When failing, set errno to EINVAL or ENOMEM.
+       * vasnprintf.c (VASNPRINTF): When PRINTF_PARSE fails, assume errno is
+       already set.
+
 2007-11-01  Bruno Haible  <bruno@clisp.org>
 
        * libgnuintl.h.in (LIBINTL_VERSION): Bump to 0.17.
index c4e3e1c078ec809952ac1b5486409c34b74e47d4..6ed1cc32276153ab939144abc8eef6124d957c7f 100644 (file)
@@ -64,6 +64,9 @@
 /* malloc(), realloc(), free().  */
 #include <stdlib.h>
 
+/* errno.  */
+#include <errno.h>
+
 /* Checked size_t computations.  */
 #include "xsize.h"
 
@@ -90,7 +93,7 @@ PRINTF_PARSE (const CHAR_T *format, DIRECTIVES *d, arguments *a)
   d->dir = (DIRECTIVE *) malloc (d_allocated * sizeof (DIRECTIVE));
   if (d->dir == NULL)
     /* Out of memory.  */
-    return -1;
+    goto out_of_memory_1;
 
   a->count = 0;
   a_allocated = 0;
@@ -110,13 +113,13 @@ PRINTF_PARSE (const CHAR_T *format, DIRECTIVES *d, arguments *a)
        memory_size = xtimes (a_allocated, sizeof (argument));          \
        if (size_overflow_p (memory_size))                              \
          /* Overflow, would lead to out of memory.  */                 \
-         goto error;                                                   \
+         goto out_of_memory;                                           \
        memory = (argument *) (a->arg                                   \
                               ? realloc (a->arg, memory_size)          \
                               : malloc (memory_size));                 \
        if (memory == NULL)                                             \
          /* Out of memory.  */                                         \
-         goto error;                                                   \
+         goto out_of_memory;                                           \
        a->arg = memory;                                                \
       }                                                                        \
     while (a->count <= n)                                              \
@@ -540,11 +543,11 @@ PRINTF_PARSE (const CHAR_T *format, DIRECTIVES *d, arguments *a)
              memory_size = xtimes (d_allocated, sizeof (DIRECTIVE));
              if (size_overflow_p (memory_size))
                /* Overflow, would lead to out of memory.  */
-               goto error;
+               goto out_of_memory;
              memory = (DIRECTIVE *) realloc (d->dir, memory_size);
              if (memory == NULL)
                /* Out of memory.  */
-               goto error;
+               goto out_of_memory;
              d->dir = memory;
            }
        }
@@ -567,6 +570,16 @@ error:
     free (a->arg);
   if (d->dir)
     free (d->dir);
+  errno = EINVAL;
+  return -1;
+
+out_of_memory:
+  if (a->arg)
+    free (a->arg);
+  if (d->dir)
+    free (d->dir);
+out_of_memory_1:
+  errno = ENOMEM;
   return -1;
 }
 
index 3cecb021631e4c78e20ca0ffc79b6b0239a69964..48f6851d2fd6abf5bf71366e3f940842b83396ff 100644 (file)
@@ -1143,10 +1143,8 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
   arguments a;
 
   if (PRINTF_PARSE (format, &d, &a) < 0)
-    {
-      errno = EINVAL;
-      return NULL;
-    }
+    /* errno is already set.  */
+    return NULL;
 
 #define CLEANUP() \
   free (d.dir);                                                                \
index 6dc4b288a61042a1f94455ab7c119ceb86edb4ef..0860c99d44fb38ebefa0cb8b10578fb9179bbc73 100644 (file)
@@ -1,3 +1,11 @@
+2007-11-03  Bruno Haible  <bruno@clisp.org>
+
+       Fix out-of-memory handling of vasnprintf.
+       * printf-parse.c: Include <errno.h>.
+       (PRINTF_PARSE): When failing, set errno to EINVAL or ENOMEM.
+       * vasnprintf.c (VASNPRINTF): When PRINTF_PARSE fails, assume errno is
+       already set.
+
 2007-10-21  Bruno Haible  <bruno@clisp.org>
 
        * printf-parse.c: Don't assume <stdint.h> exists in IN_LIBASPRINTF
index c4e3e1c078ec809952ac1b5486409c34b74e47d4..6ed1cc32276153ab939144abc8eef6124d957c7f 100644 (file)
@@ -64,6 +64,9 @@
 /* malloc(), realloc(), free().  */
 #include <stdlib.h>
 
+/* errno.  */
+#include <errno.h>
+
 /* Checked size_t computations.  */
 #include "xsize.h"
 
@@ -90,7 +93,7 @@ PRINTF_PARSE (const CHAR_T *format, DIRECTIVES *d, arguments *a)
   d->dir = (DIRECTIVE *) malloc (d_allocated * sizeof (DIRECTIVE));
   if (d->dir == NULL)
     /* Out of memory.  */
-    return -1;
+    goto out_of_memory_1;
 
   a->count = 0;
   a_allocated = 0;
@@ -110,13 +113,13 @@ PRINTF_PARSE (const CHAR_T *format, DIRECTIVES *d, arguments *a)
        memory_size = xtimes (a_allocated, sizeof (argument));          \
        if (size_overflow_p (memory_size))                              \
          /* Overflow, would lead to out of memory.  */                 \
-         goto error;                                                   \
+         goto out_of_memory;                                           \
        memory = (argument *) (a->arg                                   \
                               ? realloc (a->arg, memory_size)          \
                               : malloc (memory_size));                 \
        if (memory == NULL)                                             \
          /* Out of memory.  */                                         \
-         goto error;                                                   \
+         goto out_of_memory;                                           \
        a->arg = memory;                                                \
       }                                                                        \
     while (a->count <= n)                                              \
@@ -540,11 +543,11 @@ PRINTF_PARSE (const CHAR_T *format, DIRECTIVES *d, arguments *a)
              memory_size = xtimes (d_allocated, sizeof (DIRECTIVE));
              if (size_overflow_p (memory_size))
                /* Overflow, would lead to out of memory.  */
-               goto error;
+               goto out_of_memory;
              memory = (DIRECTIVE *) realloc (d->dir, memory_size);
              if (memory == NULL)
                /* Out of memory.  */
-               goto error;
+               goto out_of_memory;
              d->dir = memory;
            }
        }
@@ -567,6 +570,16 @@ error:
     free (a->arg);
   if (d->dir)
     free (d->dir);
+  errno = EINVAL;
+  return -1;
+
+out_of_memory:
+  if (a->arg)
+    free (a->arg);
+  if (d->dir)
+    free (d->dir);
+out_of_memory_1:
+  errno = ENOMEM;
   return -1;
 }
 
index 3cecb021631e4c78e20ca0ffc79b6b0239a69964..48f6851d2fd6abf5bf71366e3f940842b83396ff 100644 (file)
@@ -1143,10 +1143,8 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
   arguments a;
 
   if (PRINTF_PARSE (format, &d, &a) < 0)
-    {
-      errno = EINVAL;
-      return NULL;
-    }
+    /* errno is already set.  */
+    return NULL;
 
 #define CLEANUP() \
   free (d.dir);                                                                \