public inbox for glibc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Joe Simmons-Talbott <josepht@sourceware.org>
To: glibc-cvs@sourceware.org
Subject: [glibc] fxprintf: Get rid of alloca
Date: Tue, 15 Aug 2023 14:29:31 +0000 (GMT)	[thread overview]
Message-ID: <20230815142931.349473857357@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=892e125f1c92f4f77e75ba56ccb80989de63c391

commit 892e125f1c92f4f77e75ba56ccb80989de63c391
Author: Joe Simmons-Talbott <josimmon@redhat.com>
Date:   Tue Aug 15 14:28:25 2023 +0000

    fxprintf: Get rid of alloca
    
    Use a scratch_buffer rather than alloca/malloc to avoid potential stack
    overflow.
    
    Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>

Diff:
---
 stdio-common/fxprintf.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)

diff --git a/stdio-common/fxprintf.c b/stdio-common/fxprintf.c
index f0ac9654ab..88501ab61f 100644
--- a/stdio-common/fxprintf.c
+++ b/stdio-common/fxprintf.c
@@ -15,6 +15,7 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
+#include <scratch_buffer.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -34,20 +35,18 @@ locked_vfxprintf (FILE *fp, const char *fmt, va_list ap,
   wchar_t *wfmt;
   mbstate_t mbstate;
   int res;
-  int used_malloc = 0;
   size_t len = strlen (fmt) + 1;
+  struct scratch_buffer buf;
+  scratch_buffer_init (&buf);
 
   if (__glibc_unlikely (len > SIZE_MAX / sizeof (wchar_t)))
     {
       __set_errno (EOVERFLOW);
       return -1;
     }
-  if (__libc_use_alloca (len * sizeof (wchar_t)))
-    wfmt = alloca (len * sizeof (wchar_t));
-  else if ((wfmt = malloc (len * sizeof (wchar_t))) == NULL)
+  if (!scratch_buffer_set_array_size (&buf, sizeof (wchar_t), len))
     return -1;
-  else
-    used_malloc = 1;
+  wfmt = buf.data;
 
   memset (&mbstate, 0, sizeof mbstate);
   res = __mbsrtowcs (wfmt, &fmt, len, &mbstate);
@@ -55,8 +54,7 @@ locked_vfxprintf (FILE *fp, const char *fmt, va_list ap,
   if (res != -1)
     res = __vfwprintf_internal (fp, wfmt, ap, mode_flags);
 
-  if (used_malloc)
-    free (wfmt);
+  scratch_buffer_free (&buf);
 
   return res;
 }

                 reply	other threads:[~2023-08-15 14:29 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230815142931.349473857357@sourceware.org \
    --to=josepht@sourceware.org \
    --cc=glibc-cvs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).