public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v2] libc_fatal: Get rid of alloca
@ 2023-08-31 20:20 Joe Simmons-Talbott
  2023-09-01 14:23 ` Adhemerval Zanella Netto
  0 siblings, 1 reply; 6+ messages in thread
From: Joe Simmons-Talbott @ 2023-08-31 20:20 UTC (permalink / raw)
  To: libc-alpha; +Cc: Joe Simmons-Talbott

Use fixed size arrays in place of alloca to avoid potential stack overflow.
Limit the number of varargs to __libc_message to 10.
---
Changes to v1:
 * Use a fixed size array rather than scratch_buffers since we can only
   call async signal safe functions.

 sysdeps/posix/libc_fatal.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/sysdeps/posix/libc_fatal.c b/sysdeps/posix/libc_fatal.c
index 70edcc10c1..16929addab 100644
--- a/sysdeps/posix/libc_fatal.c
+++ b/sysdeps/posix/libc_fatal.c
@@ -45,6 +45,9 @@ writev_for_fatal (int fd, const struct iovec *iov, size_t niov, size_t total)
 }
 #endif
 
+/* The maximum number of varargs allowed in a __libc_message format string */
+#define MAX_NLIST 10
+
 struct str_list
 {
   const char *str;
@@ -58,6 +61,7 @@ __libc_message (const char *fmt, ...)
 {
   va_list ap;
   int fd = -1;
+  struct str_list _newp[MAX_NLIST];
 
   va_start (ap, fmt);
 
@@ -70,6 +74,7 @@ __libc_message (const char *fmt, ...)
 
   struct str_list *list = NULL;
   int nlist = 0;
+  struct iovec iov[MAX_NLIST];
 
   const char *cp = fmt;
   while (*cp != '\0')
@@ -100,17 +105,18 @@ __libc_message (const char *fmt, ...)
 	  cp = next;
 	}
 
-      struct str_list *newp = alloca (sizeof (struct str_list));
+      struct str_list *newp = &_newp[nlist];
       newp->str = str;
       newp->len = len;
       newp->next = list;
       list = newp;
       ++nlist;
+      if (nlist > MAX_NLIST)
+        goto fail_out;
     }
 
   if (nlist > 0)
     {
-      struct iovec *iov = alloca (nlist * sizeof (struct iovec));
       ssize_t total = 0;
 
       for (int cnt = nlist - 1; cnt >= 0; --cnt)
@@ -146,6 +152,7 @@ __libc_message (const char *fmt, ...)
 
   va_end (ap);
 
+fail_out:
   /* Kill the application.  */
   abort ();
 }
-- 
2.39.2


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

end of thread, other threads:[~2023-09-06 19:39 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-31 20:20 [PATCH v2] libc_fatal: Get rid of alloca Joe Simmons-Talbott
2023-09-01 14:23 ` Adhemerval Zanella Netto
2023-09-06 15:43   ` Joe Simmons-Talbott
2023-09-06 16:51     ` Adhemerval Zanella Netto
2023-09-06 18:45       ` Joe Simmons-Talbott
2023-09-06 19:39         ` 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).