From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7932) id 349473857357; Tue, 15 Aug 2023 14:29:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 349473857357 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1692109771; bh=DD0Dm8g+9spSscIeUtQ7OK2LpwFJ0qVi9mZjm7XVkM0=; h=From:To:Subject:Date:From; b=CU7F9SWyZ4gz/Q7evDSz0QQanLZl1q4Tz2tC95XaePqbkyI688l2/7C6IRef111n9 KyzRCH3iyM/peQdznJIMyzfUs0lkpOLOo6t7IvenfHF/Zzs2snCWdLbCiWDV2J3FFb 0YhZJKOMZwQyhiU/qOyJVPhh1DZ+2/nn4ydComz8= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Joe Simmons-Talbott To: glibc-cvs@sourceware.org Subject: [glibc] fxprintf: Get rid of alloca X-Act-Checkin: glibc X-Git-Author: Joe Simmons-Talbott X-Git-Refname: refs/heads/master X-Git-Oldrev: d6fe19facc61caffb25383d9c25eff86a0e115c8 X-Git-Newrev: 892e125f1c92f4f77e75ba56ccb80989de63c391 Message-Id: <20230815142931.349473857357@sourceware.org> Date: Tue, 15 Aug 2023 14:29:31 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=892e125f1c92f4f77e75ba56ccb80989de63c391 commit 892e125f1c92f4f77e75ba56ccb80989de63c391 Author: Joe Simmons-Talbott 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 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 . */ +#include #include #include #include @@ -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; }