aboutsummaryrefslogtreecommitdiff
path: root/stdio-common
diff options
context:
space:
mode:
authorJoe Simmons-Talbott <josimmon@redhat.com>2023-08-15 14:28:25 +0000
committerJoe Simmons-Talbott <josimmon@redhat.com>2023-08-15 14:28:25 +0000
commit892e125f1c92f4f77e75ba56ccb80989de63c391 (patch)
tree3ed42b9c8422afe7a7213bc896502fd66885486a /stdio-common
parentd6fe19facc61caffb25383d9c25eff86a0e115c8 (diff)
downloadglibc-892e125f1c92f4f77e75ba56ccb80989de63c391.zip
glibc-892e125f1c92f4f77e75ba56ccb80989de63c391.tar.gz
glibc-892e125f1c92f4f77e75ba56ccb80989de63c391.tar.bz2
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>
Diffstat (limited to 'stdio-common')
-rw-r--r--stdio-common/fxprintf.c14
1 files changed, 6 insertions, 8 deletions
diff --git a/stdio-common/fxprintf.c b/stdio-common/fxprintf.c
index f0ac965..88501ab 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;
}