public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] time: strftime_l: Use malloc rather than an unbounded alloca.
@ 2023-05-10 19:59 Joe Simmons-Talbott
  2023-05-16 19:28 ` Adhemerval Zanella Netto
  0 siblings, 1 reply; 7+ messages in thread
From: Joe Simmons-Talbott @ 2023-05-10 19:59 UTC (permalink / raw)
  To: libc-alpha; +Cc: Joe Simmons-Talbott

Avoid possible stack overflow by replacing alloca() with malloc().
---
 time/strftime_l.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/time/strftime_l.c b/time/strftime_l.c
index 402c6c4111..59d3e1a3b2 100644
--- a/time/strftime_l.c
+++ b/time/strftime_l.c
@@ -273,8 +273,9 @@ static const CHAR_T zeroes[16] = /* "0000000000000000" */
     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);			      \
+    ws = malloc ((l + 1) * sizeof (wchar_t));				      \
+    if (ws != NULL)							      \
+      (void) __mbsrtowcs_l (ws, &__s, l, &__st, loc);			      \
   }
 #endif
 
@@ -1346,7 +1347,10 @@ __strftime_internal (CHAR_T *s, size_t maxsize, const CHAR_T *format,
 	    wchar_t *wczone;
 	    size_t len;
 	    widen (zone, wczone, len);
+	    if (wczone == NULL)
+	      return 0;
 	    cpy (len, wczone);
+	    free (wczone);
 	  }
 #else
 	  cpy (strlen (zone), zone);
-- 
2.39.2


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2023-05-22 18:35 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-10 19:59 [PATCH] time: strftime_l: Use malloc rather than an unbounded alloca Joe Simmons-Talbott
2023-05-16 19:28 ` Adhemerval Zanella Netto
2023-05-16 19:53   ` Joe Simmons-Talbott
2023-05-16 22:00   ` Paul Eggert
2023-05-17 11:04     ` Adhemerval Zanella Netto
2023-05-17 17:40       ` Paul Eggert
2023-05-22 18:35         ` Joe Simmons-Talbott

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).