* [PATCH v2] time: strftime_l: Avoid an unbounded alloca.
@ 2023-05-23 19:41 Joe Simmons-Talbott
2023-05-23 20:00 ` Paul Eggert
0 siblings, 1 reply; 2+ messages in thread
From: Joe Simmons-Talbott @ 2023-05-23 19:41 UTC (permalink / raw)
To: libc-alpha; +Cc: Joe Simmons-Talbott, Paul Eggert
Avoid possible stack overflow by removing alloca() and converting to
wide characters within the buffer.
Suggested-by: Paul Eggert <eggert@cs.ucla.edu>
---
Changes to v1:
* Fix elf/check_localplt failure for wmemmove
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);
--
2.39.2
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH v2] time: strftime_l: Avoid an unbounded alloca.
2023-05-23 19:41 [PATCH v2] time: strftime_l: Avoid an unbounded alloca Joe Simmons-Talbott
@ 2023-05-23 20:00 ` Paul Eggert
0 siblings, 0 replies; 2+ messages in thread
From: Paul Eggert @ 2023-05-23 20:00 UTC (permalink / raw)
To: Joe Simmons-Talbott; +Cc: libc-alpha
This looks good to me, thanks.
I patched Gnulib to match that wmemmove→__wmemmove change, here:
https://git.savannah.gnu.org/cgit/gnulib.git/commit/?id=dd477fa7c7d79074b6311ac63f030cc74e3a29f3
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2023-05-23 20:00 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-23 19:41 [PATCH v2] time: strftime_l: Avoid an unbounded alloca Joe Simmons-Talbott
2023-05-23 20:00 ` Paul Eggert
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).