From: Florian Weimer <fweimer@redhat.com>
To: libc-alpha@sourceware.org
Subject: [PATCH 08/11] stdio-common: Introduce the __printf_buffer function
Date: Fri, 09 Feb 2024 16:25:44 +0100 [thread overview]
Message-ID: <f38d412ad4b7c65424502827cdfeff639858dc67.1707491940.git.fweimer@redhat.com> (raw)
In-Reply-To: <cover.1707491940.git.fweimer@redhat.com>
It can be used to write to a __print_buffer (incrementally).
---
include/printf_buffer.h | 5 ++
stdio-common/Makefile | 5 ++
stdio-common/printf_buffer.c | 30 +++++++
stdio-common/tst-printf_buffer.c | 138 +++++++++++++++++++++++++++++++
4 files changed, 178 insertions(+)
create mode 100644 stdio-common/printf_buffer.c
create mode 100644 stdio-common/tst-printf_buffer.c
diff --git a/include/printf_buffer.h b/include/printf_buffer.h
index cee5afb581..a5483e8a97 100644
--- a/include/printf_buffer.h
+++ b/include/printf_buffer.h
@@ -206,6 +206,11 @@ bool __printf_buffer_flush (struct __printf_buffer *buf) attribute_hidden;
void __vprintf_buffer (struct __printf_buffer *buf, const char *format,
va_list ap, unsigned int mode_flags) attribute_hidden;
+/* Write the argument list to BUF according to FORMAT.
+ Unconditionally enables fortification (special procesing for %n). */
+void __printf_buffer (struct __printf_buffer *buf, const char *format, ...)
+ __attribute__ ((__format__ (__printf__, 2, 3))) attribute_hidden;
+
/* Wide version of struct __printf_buffer follows. */
enum __wprintf_buffer_mode
diff --git a/stdio-common/Makefile b/stdio-common/Makefile
index 6447b6b444..d610ed67e6 100644
--- a/stdio-common/Makefile
+++ b/stdio-common/Makefile
@@ -64,6 +64,7 @@ routines := \
perror \
printf \
printf-prs \
+ printf_buffer \
printf_buffer_as_file \
printf_buffer_done \
printf_buffer_flush \
@@ -290,6 +291,10 @@ test-srcs = \
tst-unbputc \
# test-srcs
+# Test for the internal, non-exported __printf_buffer function.
+tests-internal += tst-printf_buffer
+tests-static += tst-printf_buffer
+
ifeq ($(run-built-tests),yes)
tests-special += \
$(objpfx)tst-printf-bz18872-mem.out \
diff --git a/stdio-common/printf_buffer.c b/stdio-common/printf_buffer.c
new file mode 100644
index 0000000000..fabb3b5bb4
--- /dev/null
+++ b/stdio-common/printf_buffer.c
@@ -0,0 +1,30 @@
+/* Write a format string to a buffer.
+ Copyright (C) 2024 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <https://www.gnu.org/licenses/>. */
+
+#include <printf_buffer.h>
+#include <libioP.h>
+#include <stdarg.h>
+
+void
+__printf_buffer (struct __printf_buffer *buf, const char *format, ...)
+{
+ va_list arg;
+ va_start (arg, format);
+ __vprintf_buffer (buf, format, arg, PRINTF_FORTIFY);
+ va_end (arg);
+}
diff --git a/stdio-common/tst-printf_buffer.c b/stdio-common/tst-printf_buffer.c
new file mode 100644
index 0000000000..d12da8c939
--- /dev/null
+++ b/stdio-common/tst-printf_buffer.c
@@ -0,0 +1,138 @@
+/* Basic test for the __printf_buffer function (via asprintf).
+ Copyright (C) 2024 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <https://www.gnu.org/licenses/>. */
+
+#include <printf_buffer.h>
+#include <array_length.h>
+#include <string.h>
+#include <support/check.h>
+
+static int
+do_test (void)
+{
+ {
+ struct __printf_buffer_asprintf buf;
+ __printf_buffer_asprintf_init (&buf);
+ TEST_VERIFY (!__printf_buffer_has_failed (&buf.base));
+ TEST_COMPARE (__printf_buffer_asprintf_free (&buf), -1);
+ }
+
+ {
+ struct __printf_buffer_asprintf buf;
+ __printf_buffer_asprintf_init (&buf);
+ __printf_buffer (&buf.base, "%s", "");
+ TEST_VERIFY (buf.base.write_base == buf.direct);
+ TEST_VERIFY (buf.base.write_ptr == buf.base.write_base);
+ TEST_VERIFY (!__printf_buffer_has_failed (&buf.base));
+ TEST_COMPARE (__printf_buffer_asprintf_free (&buf), -1);
+ }
+
+ {
+ struct __printf_buffer_asprintf buf;
+ __printf_buffer_asprintf_init (&buf);
+ __printf_buffer (&buf.base, "abc");
+ TEST_VERIFY (buf.base.write_base == buf.direct);
+ TEST_VERIFY (buf.base.write_ptr == buf.base.write_base + 3);
+ TEST_COMPARE_BLOB (buf.direct, 3, "abc", 3);
+ TEST_VERIFY (!__printf_buffer_has_failed (&buf.base));
+ TEST_COMPARE (__printf_buffer_asprintf_free (&buf), -1);
+ }
+
+ {
+ struct __printf_buffer_asprintf buf;
+ __printf_buffer_asprintf_init (&buf);
+ __printf_buffer (&buf.base, "abc[%d]DEF", 17);
+ TEST_VERIFY (buf.base.write_base == buf.direct);
+ TEST_VERIFY (buf.base.write_ptr == buf.base.write_base + 10);
+ TEST_COMPARE_BLOB (buf.direct, 10, "abc[17]DEF", 10);
+ TEST_VERIFY (!__printf_buffer_has_failed (&buf.base));
+ TEST_COMPARE (__printf_buffer_asprintf_free (&buf), -1);
+ }
+
+ /* Test concatenation. */
+ {
+ struct __printf_buffer_asprintf buf;
+ __printf_buffer_asprintf_init (&buf);
+ __printf_buffer (&buf.base, "abc");
+ TEST_VERIFY (buf.base.write_base == buf.direct);
+ TEST_VERIFY (buf.base.write_ptr == buf.base.write_base + 3);
+ TEST_COMPARE_BLOB (buf.direct, 3, "abc", 3);
+ __printf_buffer (&buf.base, "[%d]DEF", 17);
+ TEST_VERIFY (buf.base.write_base == buf.direct);
+ TEST_VERIFY (buf.base.write_ptr == buf.base.write_base + 10);
+ TEST_COMPARE_BLOB (buf.direct, 10, "abc[17]DEF", 10);
+ TEST_VERIFY (!__printf_buffer_has_failed (&buf.base));
+ TEST_COMPARE (__printf_buffer_asprintf_free (&buf), -1);
+ }
+
+ {
+ struct __printf_buffer_asprintf buf;
+ char large_string[500];
+ memset (large_string, 'X', array_length (large_string));
+ large_string[array_length (large_string) -1] = '\0';
+ int n;
+
+ __printf_buffer_asprintf_init (&buf);
+ __printf_buffer (&buf.base, "%s", large_string);
+ TEST_VERIFY (buf.base.write_base != buf.direct);
+ TEST_COMPARE_BLOB (buf.base.write_base,
+ buf.base.write_ptr - buf.base.write_base,
+ large_string, strlen (large_string));
+ TEST_VERIFY (!__printf_buffer_has_failed (&buf.base));
+ TEST_COMPARE (__printf_buffer_asprintf_free (&buf), -1);
+
+ __printf_buffer_asprintf_init (&buf);
+ __printf_buffer (&buf.base, "abc");
+ __printf_buffer (&buf.base, "%n", &n);
+ /* %n counts from the start of the buffer, not from within the
+ printf call. */
+ TEST_COMPARE (n, 3);
+ __printf_buffer (&buf.base, "%s", large_string);
+ TEST_VERIFY (buf.base.write_base != buf.direct);
+ TEST_COMPARE_BLOB (buf.base.write_base, 3, "abc", 3);
+ TEST_COMPARE_BLOB (buf.base.write_base + 3,
+ buf.base.write_ptr - buf.base.write_base - 3,
+ large_string, strlen (large_string));
+ TEST_VERIFY (!__printf_buffer_has_failed (&buf.base));
+ __printf_buffer (&buf.base, "%n", &n);
+ TEST_COMPARE (n, 3 + strlen (large_string));
+ TEST_COMPARE_BLOB (buf.base.write_base, 3, "abc", 3);
+ TEST_COMPARE_BLOB (buf.base.write_base + 3,
+ buf.base.write_ptr - buf.base.write_base - 3,
+ large_string, strlen (large_string));
+ TEST_VERIFY (!__printf_buffer_has_failed (&buf.base));
+ TEST_COMPARE (__printf_buffer_asprintf_free (&buf), -1);
+ }
+
+ /* Test %n with concatenation. */
+ {
+ struct __printf_buffer_asprintf buf;
+ __printf_buffer_asprintf_init (&buf);
+ __printf_buffer (&buf.base, "abc");
+ int n = -1;
+ __printf_buffer (&buf.base, "D%nEF", &n);
+ TEST_COMPARE (n, 4); /* %n counts from the start of the buffer. */
+ TEST_VERIFY (buf.base.write_base == buf.direct);
+ TEST_COMPARE_BLOB (buf.base.write_base,
+ buf.base.write_ptr - buf.base.write_base, "abcDEF", 6);
+ TEST_VERIFY (!__printf_buffer_has_failed (&buf.base));
+ TEST_COMPARE (__printf_buffer_asprintf_free (&buf), -1);
+ }
+ return 0;
+}
+
+#include <support/test-driver.c>
--
2.43.0
next prev parent reply other threads:[~2024-02-09 15:25 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-02-09 15:24 [PATCH 00/11] Build getdomainname, gethostname, syslog with fortification Florian Weimer
2024-02-09 15:24 ` [PATCH 01/11] misc: Build getdomainname " Florian Weimer
2024-02-12 17:14 ` Adhemerval Zanella Netto
2024-02-12 17:30 ` Andreas Schwab
2024-02-12 19:29 ` Florian Weimer
2024-02-13 9:12 ` Andreas Schwab
2024-02-13 9:23 ` Florian Weimer
2024-02-13 9:32 ` Andreas Schwab
2024-02-09 15:24 ` [PATCH 02/11] misc: Build gethostname " Florian Weimer
2024-02-12 17:25 ` Adhemerval Zanella Netto
2024-02-09 15:25 ` [PATCH 03/11] libio: Add fortify wrapper for internal __snprintf Florian Weimer
2024-02-12 17:34 ` Adhemerval Zanella Netto
2024-02-13 12:13 ` Florian Weimer
2024-02-13 13:16 ` Adhemerval Zanella Netto
2024-02-09 15:25 ` [PATCH 04/11] syslog: Update misc/tst-syslog to check reported %n value Florian Weimer
2024-02-13 11:59 ` Adhemerval Zanella Netto
2024-02-15 13:23 ` Florian Weimer
2024-02-09 15:25 ` [PATCH 05/11] syslog: Build with fortification Florian Weimer
2024-02-13 12:26 ` Adhemerval Zanella Netto
2024-02-09 15:25 ` [PATCH 06/11] stdio: Rename __printf_buffer to __vfprintf_buffer Florian Weimer
2024-02-16 13:40 ` Adhemerval Zanella Netto
2024-02-09 15:25 ` [PATCH 07/11] libio: Extract __printf_buffer_asprintf_init from asprintf implementation Florian Weimer
2024-02-16 14:04 ` Adhemerval Zanella Netto
2024-02-09 15:25 ` Florian Weimer [this message]
2024-02-16 14:12 ` [PATCH 08/11] stdio-common: Introduce the __printf_buffer function Adhemerval Zanella Netto
2024-02-09 15:25 ` [PATCH 09/11] stdio-common: Allow skipping initial bytes in __printf_buffer for %n Florian Weimer
2024-02-16 14:13 ` Adhemerval Zanella Netto
2024-02-09 15:25 ` [PATCH 10/11] stdio-common: Support large offsets with %lln Florian Weimer
2024-02-16 14:20 ` Adhemerval Zanella Netto
2024-02-09 15:26 ` [PATCH 11/11] syslog: Use a printf buffer directly to construct the entire packet Florian Weimer
2024-02-14 15:16 ` Adhemerval Zanella Netto
2024-02-15 13:02 ` Florian Weimer
2024-02-16 13:35 ` Adhemerval Zanella Netto
2024-02-16 15:58 ` Adhemerval Zanella Netto
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=f38d412ad4b7c65424502827cdfeff639858dc67.1707491940.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).