From: Bruno Haible Date: Sat, 3 Nov 2007 11:44:07 +0000 (+0000) Subject: Fix out-of-memory handling of vasnprintf. X-Git-Tag: v0.17~17 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4e118d51300a6739c1a7054fb35d1a886c1b1546;p=thirdparty%2Fgettext.git Fix out-of-memory handling of vasnprintf. --- diff --git a/gettext-runtime/intl/ChangeLog b/gettext-runtime/intl/ChangeLog index 711f08571..e77142f25 100644 --- a/gettext-runtime/intl/ChangeLog +++ b/gettext-runtime/intl/ChangeLog @@ -1,3 +1,11 @@ +2007-11-03 Bruno Haible + + Fix out-of-memory handling of vasnprintf. + * printf-parse.c: Include . + (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 * libgnuintl.h.in (LIBINTL_VERSION): Bump to 0.17. diff --git a/gettext-runtime/intl/printf-parse.c b/gettext-runtime/intl/printf-parse.c index c4e3e1c07..6ed1cc322 100644 --- a/gettext-runtime/intl/printf-parse.c +++ b/gettext-runtime/intl/printf-parse.c @@ -64,6 +64,9 @@ /* malloc(), realloc(), free(). */ #include +/* errno. */ +#include + /* 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; } diff --git a/gettext-runtime/intl/vasnprintf.c b/gettext-runtime/intl/vasnprintf.c index 3cecb0216..48f6851d2 100644 --- a/gettext-runtime/intl/vasnprintf.c +++ b/gettext-runtime/intl/vasnprintf.c @@ -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); \ diff --git a/gettext-runtime/libasprintf/ChangeLog b/gettext-runtime/libasprintf/ChangeLog index 6dc4b288a..0860c99d4 100644 --- a/gettext-runtime/libasprintf/ChangeLog +++ b/gettext-runtime/libasprintf/ChangeLog @@ -1,3 +1,11 @@ +2007-11-03 Bruno Haible + + Fix out-of-memory handling of vasnprintf. + * printf-parse.c: Include . + (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 * printf-parse.c: Don't assume exists in IN_LIBASPRINTF diff --git a/gettext-runtime/libasprintf/printf-parse.c b/gettext-runtime/libasprintf/printf-parse.c index c4e3e1c07..6ed1cc322 100644 --- a/gettext-runtime/libasprintf/printf-parse.c +++ b/gettext-runtime/libasprintf/printf-parse.c @@ -64,6 +64,9 @@ /* malloc(), realloc(), free(). */ #include +/* errno. */ +#include + /* 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; } diff --git a/gettext-runtime/libasprintf/vasnprintf.c b/gettext-runtime/libasprintf/vasnprintf.c index 3cecb0216..48f6851d2 100644 --- a/gettext-runtime/libasprintf/vasnprintf.c +++ b/gettext-runtime/libasprintf/vasnprintf.c @@ -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); \