From: Florian Weimer <fweimer@redhat.com>
To: libc-alpha@sourceware.org
Subject: [PATCH 22/26] libio: Convert __vsprintf_internal to buffers
Date: Thu, 17 Mar 2022 20:31:43 +0100 [thread overview]
Message-ID: <baf82848945a6347ec77133e6f8ae2c15b357783.1647544751.git.fweimer@redhat.com> (raw)
In-Reply-To: <cover.1647544751.git.fweimer@redhat.com>
---
include/printf_buffer.h | 2 +
libio/iovsprintf.c | 70 ++++++++----------------------
stdio-common/printf_buffer_flush.c | 4 ++
3 files changed, 24 insertions(+), 52 deletions(-)
diff --git a/include/printf_buffer.h b/include/printf_buffer.h
index 95d1b06489..c938cb6085 100644
--- a/include/printf_buffer.h
+++ b/include/printf_buffer.h
@@ -44,7 +44,9 @@
enum __printf_buffer_mode
{
__printf_buffer_mode_failed,
+ __printf_buffer_mode_sprintf,
__printf_buffer_mode_snprintf,
+ __printf_buffer_mode_sprintf_chk,
__printf_buffer_mode_to_file,
__printf_buffer_mode_strfmon,
__printf_buffer_mode_fp, /* For __printf_fp_l_buffer. */
diff --git a/libio/iovsprintf.c b/libio/iovsprintf.c
index 72c67bf27b..ba4e180a35 100644
--- a/libio/iovsprintf.c
+++ b/libio/iovsprintf.c
@@ -25,58 +25,18 @@
in files containing the exception. */
#include "libioP.h"
-#include "strfile.h"
-static int __THROW
-_IO_str_chk_overflow (FILE *fp, int c)
-{
- /* If we get here, the user-supplied buffer would be overrun by
- further output. */
- __chk_fail ();
-}
-
-static const struct _IO_jump_t _IO_str_chk_jumps libio_vtable =
-{
- JUMP_INIT_DUMMY,
- JUMP_INIT(finish, _IO_str_finish),
- JUMP_INIT(overflow, _IO_str_chk_overflow),
- JUMP_INIT(underflow, _IO_str_underflow),
- JUMP_INIT(uflow, _IO_default_uflow),
- JUMP_INIT(pbackfail, _IO_str_pbackfail),
- JUMP_INIT(xsputn, _IO_default_xsputn),
- JUMP_INIT(xsgetn, _IO_default_xsgetn),
- JUMP_INIT(seekoff, _IO_str_seekoff),
- JUMP_INIT(seekpos, _IO_default_seekpos),
- JUMP_INIT(setbuf, _IO_default_setbuf),
- JUMP_INIT(sync, _IO_default_sync),
- JUMP_INIT(doallocate, _IO_default_doallocate),
- JUMP_INIT(read, _IO_default_read),
- JUMP_INIT(write, _IO_default_write),
- JUMP_INIT(seek, _IO_default_seek),
- JUMP_INIT(close, _IO_default_close),
- JUMP_INIT(stat, _IO_default_stat),
- JUMP_INIT(showmanyc, _IO_default_showmanyc),
- JUMP_INIT(imbue, _IO_default_imbue)
-};
-
-/* This function is called by regular vsprintf with maxlen set to -1,
- and by vsprintf_chk with maxlen set to the size of the output
- string. In the former case, _IO_str_chk_overflow will never be
- called; in the latter case it will crash the program if the buffer
- overflows. */
+#include <printf.h>
+#include <stdint.h>
+#include <printf_buffer.h>
int
__vsprintf_internal (char *string, size_t maxlen,
const char *format, va_list args,
unsigned int mode_flags)
{
- _IO_strfile sf;
- int ret;
+ struct __printf_buffer buf;
-#ifdef _IO_MTSAFE_IO
- sf._sbf._f._lock = NULL;
-#endif
- _IO_no_init (&sf._sbf._f, _IO_USER_LOCK, -1, NULL, NULL);
/* When called from fortified sprintf/vsprintf, erase the destination
buffer and try to detect overflows. When called from regular
sprintf/vsprintf, do not erase the destination buffer, because
@@ -84,19 +44,25 @@ __vsprintf_internal (char *string, size_t maxlen,
by ISO C), nor try to detect overflows. */
if ((mode_flags & PRINTF_CHK) != 0)
{
- _IO_JUMPS (&sf._sbf) = &_IO_str_chk_jumps;
string[0] = '\0';
+ __printf_buffer_init (&buf, string, maxlen,
+ __printf_buffer_mode_sprintf);
}
else
- _IO_JUMPS (&sf._sbf) = &_IO_str_jumps;
- _IO_str_init_static_internal (&sf, string,
- (maxlen == -1) ? -1 : maxlen - 1,
- string);
+ {
+ __printf_buffer_init (&buf, string, 0, __printf_buffer_mode_sprintf_chk);
+ buf.write_end = (char *) ~(uintptr_t) 0; /* End of address space. */
+ }
+
+ __printf_buffer (&buf, format, args, mode_flags);
- ret = __vfprintf_internal (&sf._sbf._f, format, args, mode_flags);
+ /* Write the NUL terminator if there is room. Do not use the putc
+ operation to avoid overflowing the character write count. */
+ if ((mode_flags & PRINTF_CHK) != 0 && buf.write_ptr == buf.write_end)
+ __chk_fail ();
+ *buf.write_ptr = '\0';
- *sf._sbf._f._IO_write_ptr = '\0';
- return ret;
+ return __printf_buffer_done (&buf);
}
int
diff --git a/stdio-common/printf_buffer_flush.c b/stdio-common/printf_buffer_flush.c
index bfd1f9d733..f44c8b887f 100644
--- a/stdio-common/printf_buffer_flush.c
+++ b/stdio-common/printf_buffer_flush.c
@@ -38,10 +38,14 @@ __printf_buffer_do_flush (struct __printf_buffer *buf)
switch (buf->mode)
{
case __printf_buffer_mode_failed:
+ case __printf_buffer_mode_sprintf:
return;
case __printf_buffer_mode_snprintf:
__printf_buffer_flush_snprintf ((struct __printf_buffer_snprintf *) buf);
return;
+ case __printf_buffer_mode_sprintf_chk:
+ __chk_fail ();
+ break;
case __printf_buffer_mode_to_file:
__printf_buffer_flush_to_file ((struct __printf_buffer_to_file *) buf);
return;
--
2.35.1
next prev parent reply other threads:[~2022-03-17 19:31 UTC|newest]
Thread overview: 49+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-03-17 19:28 [PATCH 00/26] vfprintf rework to remove vtables Florian Weimer
2022-03-17 19:28 ` [PATCH 01/26] libio: Convert tst_swprintf to the test framework Florian Weimer
2022-03-18 17:40 ` Adhemerval Zanella
2022-03-17 19:28 ` [PATCH 02/26] libio: Flush-only _IO_str_overflow must not return EOF (bug 28949) Florian Weimer
2022-03-18 18:11 ` Adhemerval Zanella
2022-03-17 19:28 ` [PATCH 03/26] stdio-common: Add wide stream coverage to tst-vfprintf-user-type Florian Weimer
2022-03-18 18:30 ` Adhemerval Zanella
2022-03-18 19:19 ` Florian Weimer
2022-03-17 19:28 ` [PATCH 04/26] stdio-common: Add tst-printf-width-i18n to cover numeric field width Florian Weimer
2022-05-20 13:22 ` Adhemerval Zanella
2022-05-20 13:33 ` Adhemerval Zanella
2022-05-23 6:39 ` Florian Weimer
2022-03-17 19:28 ` [PATCH 05/26] vfprintf: Move argument processing into vfprintf-process-arg.c Florian Weimer
2022-05-20 13:28 ` Adhemerval Zanella
2022-03-17 19:28 ` [PATCH 06/26] vfprintf: Consolidate some multibyte/wide character processing Florian Weimer
2022-05-20 14:16 ` Adhemerval Zanella
2022-03-17 19:29 ` [PATCH 07/26] __printf_fphex always uses LC_NUMERIC Florian Weimer
2022-05-20 14:21 ` Adhemerval Zanella
2022-05-23 6:55 ` Florian Weimer
2022-03-17 19:29 ` [PATCH 08/26] stdio-common: Add tst-memstream-string for open_memstream overflow Florian Weimer
2022-05-20 17:44 ` Adhemerval Zanella
2022-05-23 7:03 ` Florian Weimer
2022-03-17 19:29 ` [PATCH 09/26] stdio-common: Add printf specifier registry to <printf.h> Florian Weimer
2022-05-20 17:49 ` Adhemerval Zanella
2022-03-17 19:30 ` [PATCH 10/26] stdio-common: Move union printf_arg int <printf.h> Florian Weimer
2022-05-20 17:51 ` Adhemerval Zanella
2022-03-17 19:30 ` [PATCH 11/26] stdio-common: Simplify printf_unknown interface in vfprintf-internal.c Florian Weimer
2022-05-20 18:07 ` Adhemerval Zanella
2022-03-17 19:30 ` [PATCH 12/26] locale: Call _nl_unload_locale from _nl_archive_subfreeres Florian Weimer
2022-05-20 18:09 ` Adhemerval Zanella
2022-05-23 7:14 ` Florian Weimer
2022-03-17 19:30 ` [PATCH 13/26] locale: Remove cleanup function pointer from struct __localedata Florian Weimer
2022-05-20 18:16 ` Adhemerval Zanella
2022-03-17 19:30 ` [PATCH 14/26] locale: Remove private union from struct __locale_data Florian Weimer
2022-05-20 18:22 ` Adhemerval Zanella
2022-03-17 19:30 ` [PATCH 15/26] locale: Add more cached data to LC_CTYPE Florian Weimer
2022-05-20 18:29 ` Adhemerval Zanella
2022-05-23 7:20 ` Florian Weimer
2022-03-17 19:31 ` [PATCH 16/26] locale: Implement struct grouping_iterator Florian Weimer
2022-03-17 19:31 ` [PATCH 17/26] stdio-common: Introduce buffers for implementing printf Florian Weimer
2022-03-17 19:31 ` [PATCH 18/26] stdio-common: Add __printf_function_invoke Florian Weimer
2022-03-17 19:31 ` [PATCH 19/26] stdio-common: Add __translated_number_width Florian Weimer
2022-03-17 19:31 ` [PATCH 20/26] stdio-common: Convert vfprintf and related functions to buffers Florian Weimer
2022-03-17 19:31 ` [PATCH 21/26] stdio-common: Add lock optimization to vfprintf and vfwprintf Florian Weimer
2022-03-17 19:31 ` Florian Weimer [this message]
2022-03-17 19:31 ` [PATCH 23/26] libio: Convert __vasprintf_internal to buffers Florian Weimer
2022-03-17 19:31 ` [PATCH 24/26] libio: Convert __vdprintf_internal " Florian Weimer
2022-03-17 19:32 ` [PATCH 25/26] libio: Convert __obstack_vprintf_internal to buffers (bug 27124) Florian Weimer
2022-03-17 19:32 ` [PATCH 26/26] libio: Convert __vswprintf_internal to buffers (bug 27857) Florian Weimer
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=baf82848945a6347ec77133e6f8ae2c15b357783.1647544751.git.fweimer@redhat.com \
--to=fweimer@redhat.com \
--cc=libc-alpha@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).