From b2a34e6f87ef6a0d5c913fb956b29f44f72cb198 Mon Sep 17 00:00:00 2001 From: tjr Date: Sun, 15 Sep 2002 08:06:17 +0000 Subject: Use the heap instead of the stack to store temporary multibyte string buffers; this is slower but safer for threaded programs where threads often have relatively low stack size limits. --- lib/libc/locale/wcsftime.c | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) (limited to 'lib/libc/locale/wcsftime.c') diff --git a/lib/libc/locale/wcsftime.c b/lib/libc/locale/wcsftime.c index 31a1c4c..1ed5b07 100644 --- a/lib/libc/locale/wcsftime.c +++ b/lib/libc/locale/wcsftime.c @@ -27,6 +27,7 @@ #include __FBSDID("$FreeBSD$"); +#include #include #include #include @@ -51,8 +52,11 @@ wcsftime(wchar_t * __restrict wcs, size_t maxsize, { static const mbstate_t initial; mbstate_t state; - char *dst, *sformat; + char *dst, *dstp, *sformat; size_t n, sflen; + int sverrno; + + sformat = dst = NULL; /* * Convert the supplied format string to a multibyte representation @@ -61,8 +65,9 @@ wcsftime(wchar_t * __restrict wcs, size_t maxsize, state = initial; sflen = wcsrtombs(NULL, &format, 0, &state); if (sflen == (size_t)-1) - return (0); - sformat = alloca(sflen + 1); + goto error; + if ((sformat = malloc(sflen + 1)) == NULL) + goto error; state = initial; wcsrtombs(sformat, &format, sflen + 1, &state); @@ -72,16 +77,29 @@ wcsftime(wchar_t * __restrict wcs, size_t maxsize, * Then, copy and convert the result back into wide characters in * the caller's buffer. */ - if (SIZE_T_MAX / MB_CUR_MAX <= maxsize) + if (SIZE_T_MAX / MB_CUR_MAX <= maxsize) { /* maxsize is prepostorously large - avoid int. overflow. */ - return (0); - dst = alloca(maxsize * MB_CUR_MAX); + errno = EINVAL; + goto error; + } + if ((dst = malloc(maxsize * MB_CUR_MAX)) == NULL) + goto error; if (strftime(dst, maxsize, sformat, timeptr) == 0) - return (0); + goto error; state = initial; - n = mbsrtowcs(wcs, (const char **)&dst, maxsize, &state); - if (n == (size_t)-2 || n == (size_t)-1 || dst != NULL) - return (0); + dstp = dst; + n = mbsrtowcs(wcs, (const char **)&dstp, maxsize, &state); + if (n == (size_t)-2 || n == (size_t)-1 || dstp != NULL) + goto error; + free(sformat); + free(dst); return (n); + +error: + sverrno = errno; + free(sformat); + free(dst); + errno = sverrno; + return (0); } -- cgit v1.1