X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Ferror.c;h=1149235a101ed20bb37781cc27a78d39875c990d;hb=ecc43f7ce6220952bac7d8e751ca102c60f9f812;hp=e249942121ff5f83af7bf3a6173a3c6ab39de8d5;hpb=2fee8e226132f10fc89e92d8cf7d5e7a366775a7;p=gnulib.git diff --git a/lib/error.c b/lib/error.c index e24994212..1149235a1 100644 --- a/lib/error.c +++ b/lib/error.c @@ -90,6 +90,10 @@ extern void __error_at_line (int status, int errnum, const char *file_name, char *strerror_r (); # endif +# ifndef SIZE_MAX +# define SIZE_MAX ((size_t) -1) +# endif + /* The calling program should define program_name and set it to the name of the executing program. */ extern char *program_name; @@ -142,40 +146,26 @@ error_tail (int status, int errnum, const char *message, va_list args) { # define ALLOCA_LIMIT 2000 size_t len = strlen (message) + 1; - wchar_t *wmessage = NULL; - mbstate_t st; - size_t res; - const char *tmp; - - do + const wchar_t *wmessage = L"out of memory"; + wchar_t *wbuf = (len < ALLOCA_LIMIT + ? alloca (len * sizeof *wbuf) + : len <= SIZE_MAX / sizeof *wbuf + ? malloc (len * sizeof *wbuf) + : NULL); + + if (wbuf) { - if (len < ALLOCA_LIMIT) - wmessage = (wchar_t *) alloca (len * sizeof (wchar_t)); - else - { - if (wmessage != NULL && len / 2 < ALLOCA_LIMIT) - wmessage = NULL; - - wmessage = (wchar_t *) realloc (wmessage, - len * sizeof (wchar_t)); - - if (wmessage == NULL) - { - fputws_unlocked (L"out of memory\n", stderr); - return; - } - } - + size_t res; + mbstate_t st; + const char *tmp = message; memset (&st, '\0', sizeof (st)); - tmp =message; + res = mbsrtowcs (wbuf, &tmp, len, &st); + wmessage = res == (size_t) -1 ? L"???" : wbuf; } - while ((res = mbsrtowcs (wmessage, &tmp, len, &st)) == len); - - if (res == (size_t) -1) - /* The string cannot be converted. */ - wmessage = (wchar_t *) L"???"; __vfwprintf (stderr, wmessage, args); + if (! (len < ALLOCA_LIMIT)) + free (wbuf); } else #endif