public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v2] grantpt: Get rid of alloca
@ 2023-06-07 18:21 Joe Simmons-Talbott
  2023-06-07 19:05 ` Sergey Bugaev
  0 siblings, 1 reply; 3+ messages in thread
From: Joe Simmons-Talbott @ 2023-06-07 18:21 UTC (permalink / raw)
  To: Samuel Thibault, Sergey Bugaev, libc-alpha; +Cc: Joe Simmons-Talbott

Replace alloca with a scratch_buffer to avoid potential stack overflows.

Checked with build-many-glibcs.py on i686-gnu.
---
Changes to v1:
 * Move scratch_buffer_free call into same scope as the scratch_buffer
   and use the address of the scratch_buffer for scratch_buffer_free.

 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..77c8d814cc 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,10 +148,19 @@ 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;
+
+      scratch_buffer_free(&sbuf);
     }
   gid_t gid = tty_gid == -1 ? __getgid () : tty_gid;
 
-- 
2.39.2


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

* Re: [PATCH v2] grantpt: Get rid of alloca
  2023-06-07 18:21 [PATCH v2] grantpt: Get rid of alloca Joe Simmons-Talbott
@ 2023-06-07 19:05 ` Sergey Bugaev
  2023-06-13 19:18   ` Joe Simmons-Talbott
  0 siblings, 1 reply; 3+ messages in thread
From: Sergey Bugaev @ 2023-06-07 19:05 UTC (permalink / raw)
  To: Joe Simmons-Talbott; +Cc: Samuel Thibault, libc-alpha

Hello,

On Wed, Jun 7, 2023 at 9:21 PM Joe Simmons-Talbott <josimmon@redhat.com> wrote:
> +      if (!scratch_buffer_set_array_size (&sbuf, 1, grbuflen))
> +       {
> +         retval -1;
> +         goto cleanup;
> +       }

This still has the 'retval -1' :)

Sergey

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

* Re: [PATCH v2] grantpt: Get rid of alloca
  2023-06-07 19:05 ` Sergey Bugaev
@ 2023-06-13 19:18   ` Joe Simmons-Talbott
  0 siblings, 0 replies; 3+ messages in thread
From: Joe Simmons-Talbott @ 2023-06-13 19:18 UTC (permalink / raw)
  To: Sergey Bugaev; +Cc: Samuel Thibault, libc-alpha

On Wed, Jun 07, 2023 at 10:05:57PM +0300, Sergey Bugaev wrote:
> Hello,
> 
> On Wed, Jun 7, 2023 at 9:21 PM Joe Simmons-Talbott <josimmon@redhat.com> wrote:
> > +      if (!scratch_buffer_set_array_size (&sbuf, 1, grbuflen))
> > +       {
> > +         retval -1;
> > +         goto cleanup;
> > +       }
> 
> This still has the 'retval -1' :)

I completely missed what you were saying and removed that line in v3.
I've added it back in v4 with the missing '='.

Thanks,
Joe


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

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

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-07 18:21 [PATCH v2] grantpt: Get rid of alloca Joe Simmons-Talbott
2023-06-07 19:05 ` Sergey Bugaev
2023-06-13 19:18   ` 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).