From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2206) id 00F893857437; Mon, 29 May 2023 13:19:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 00F893857437 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1685366373; bh=X87htHuDgfqeponKWpRvsEO5yckTeM66PfvLwqmh6sM=; h=From:To:Subject:Date:From; b=qdAMikRbHG1grLdBfvqefDj6vFwZPSIy4v6whnsDEkJta9iEabv8Y8xRqC2Sp4ttx ci+TnuNCZyu7nvGMsN9xxfPWBssWt9iBwqobAzx1lFn4x0cGXLZIO0v+TxpVSBZw0V YYjbcaa/eHhG1RKqjB3nI/1SeEMSfF+TQOrAV9iY= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Siddhesh Poyarekar To: glibc-cvs@sourceware.org Subject: [glibc] time: strftime_l: Avoid an unbounded alloca. X-Act-Checkin: glibc X-Git-Author: Joe Simmons-Talbott X-Git-Refname: refs/heads/master X-Git-Oldrev: ed2f9dc9420c4c61436328778a70459d0a35556a X-Git-Newrev: 79b2667d1eb06c6503c22f2f323c1c574ac5917b Message-Id: <20230529131933.00F893857437@sourceware.org> Date: Mon, 29 May 2023 13:19:33 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=79b2667d1eb06c6503c22f2f323c1c574ac5917b commit 79b2667d1eb06c6503c22f2f323c1c574ac5917b Author: Joe Simmons-Talbott Date: Tue May 23 15:41:29 2023 -0400 time: strftime_l: Avoid an unbounded alloca. Avoid possible stack overflow by removing alloca() and converting to wide characters within the buffer. Suggested-by: Paul Eggert Diff: --- time/strftime_l.c | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/time/strftime_l.c b/time/strftime_l.c index 402c6c4111..3901bd79da 100644 --- a/time/strftime_l.c +++ b/time/strftime_l.c @@ -267,15 +267,6 @@ static const CHAR_T zeroes[16] = /* "0000000000000000" */ # undef __mbsrtowcs_l # define __mbsrtowcs_l(d, s, l, st, loc) __mbsrtowcs (d, s, l, st) # endif -# define widen(os, ws, l) \ - { \ - mbstate_t __st; \ - const char *__s = os; \ - memset (&__st, '\0', sizeof (__st)); \ - l = __mbsrtowcs_l (NULL, &__s, 0, &__st, loc); \ - ws = alloca ((l + 1) * sizeof (wchar_t)); \ - (void) __mbsrtowcs_l (ws, &__s, l, &__st, loc); \ - } #endif @@ -1342,11 +1333,31 @@ __strftime_internal (CHAR_T *s, size_t maxsize, const CHAR_T *format, #ifdef COMPILE_WIDE { /* The zone string is always given in multibyte form. We have - to transform it first. */ - wchar_t *wczone; - size_t len; - widen (zone, wczone, len); - cpy (len, wczone); + to convert it to wide character. */ + size_t w = pad == L_('-') || width < 0 ? 0 : width; + char const *z = zone; + mbstate_t st = {0}; + size_t len = __mbsrtowcs_l (p, &z, maxsize - i, &st, loc); + if (len == (size_t) -1) + return 0; + size_t incr = len < w ? w : len; + if (incr >= maxsize - i) + { + errno = ERANGE; + return 0; + } + if (p) + { + if (len < w) + { + size_t delta = w - len; + __wmemmove (p + delta, p, len); + wchar_t wc = pad == L_('0') || pad == L_('+') ? L'0' : L' '; + wmemset (p, wc, delta); + } + p += incr; + } + i += incr; } #else cpy (strlen (zone), zone);