public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] grantpt: Get rid of alloca
@ 2023-06-01 19:58 Joe Simmons-Talbott
  2023-06-05 13:29 ` Florian Weimer
  0 siblings, 1 reply; 8+ messages in thread
From: Joe Simmons-Talbott @ 2023-06-01 19:58 UTC (permalink / raw)
  To: libc-alpha; +Cc: Joe Simmons-Talbott

Replace alloca with a scratch_buffer to avoid potential stack overflows.
---
 sysdeps/unix/grantpt.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/sysdeps/unix/grantpt.c b/sysdeps/unix/grantpt.c
index 38fce52576..e5d2390bf2 100644
--- a/sysdeps/unix/grantpt.c
+++ b/sysdeps/unix/grantpt.c
@@ -20,6 +20,7 @@
 #include <fcntl.h>
 #include <grp.h>
 #include <limits.h>
+#include <scratch_buffer.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/resource.h>
@@ -147,7 +148,14 @@ grantpt (int fd)
 	/* `sysconf' does not support _SC_GETGR_R_SIZE_MAX.
 	   Try a moderate value.  */
 	grbuflen = 1024;
-      grtmpbuf = (char *) __alloca (grbuflen);
+      struct scratch_buffer sbuf;
+      scratch_buffer_init (&sbuf);
+      if (!scratch_buffer_set_array_size (&sbuf, 1, grbuflen))
+	{
+	  retval -1;
+	  goto cleanup;
+	}
+      grtmpbuf = sbuf.data;
       __getgrnam_r (TTY_GROUP, &grbuf, grtmpbuf, grbuflen, &p);
       if (p != NULL)
 	tty_gid = p->gr_gid;
@@ -255,6 +263,8 @@ grantpt (int fd)
   if (buf != _buf)
     free (buf);
 
+  scratch_buffer_free(sbuf);
+
   return retval;
 }
 libc_hidden_def (grantpt)
-- 
2.39.2


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

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

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-01 19:58 [PATCH] grantpt: Get rid of alloca Joe Simmons-Talbott
2023-06-05 13:29 ` Florian Weimer
2023-06-05 13:37   ` Joe Simmons-Talbott
2023-06-05 18:14     ` Adhemerval Zanella Netto
2023-06-05 22:02       ` Joe Simmons-Talbott
2023-06-06  6:14         ` Siddhesh Poyarekar
2023-06-07 19:05           ` Zack Weinberg
2023-06-07  0:40   ` Samuel Thibault

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