[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20250304-nolibc-kselftest-harness-v1-26-adca7cd231e2@linutronix.de>
Date: Tue, 04 Mar 2025 08:10:56 +0100
From: Thomas Weißschuh <thomas.weissschuh@...utronix.de>
To: Shuah Khan <shuah@...nel.org>, Shuah Khan <skhan@...uxfoundation.org>,
Willy Tarreau <w@....eu>,
Thomas Weißschuh <linux@...ssschuh.net>
Cc: linux-kernel@...r.kernel.org, linux-kselftest@...r.kernel.org,
Thomas Weißschuh <thomas.weissschuh@...utronix.de>
Subject: [PATCH 26/32] tools/nolibc: allow limiting of printf destination
size
snprintf() allows limiting the output buffer, while still returning the
number of all bytes that would have been written.
Implement the limitation logic in preparation for snprintf().
Signed-off-by: Thomas Weißschuh <thomas.weissschuh@...utronix.de>
---
tools/include/nolibc/stdio.h | 16 ++++++++++------
1 file changed, 10 insertions(+), 6 deletions(-)
diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h
index 434fbaddae7a216159fecf618da85889d631dff7..b484a19466394d55d7d21248031837238b58f3ff 100644
--- a/tools/include/nolibc/stdio.h
+++ b/tools/include/nolibc/stdio.h
@@ -215,13 +215,13 @@ char *fgets(char *s, int size, FILE *stream)
*/
typedef int (*_printf_cb)(intptr_t state, const char *buf, size_t size);
-static __attribute__((unused, format(printf, 3, 0)))
-int _printf(_printf_cb cb, intptr_t state, const char *fmt, va_list args)
+static __attribute__((unused, format(printf, 4, 0)))
+int _printf(_printf_cb cb, intptr_t state, size_t n, const char *fmt, va_list args)
{
char escape, lpref, c;
unsigned long long v;
unsigned int written;
- size_t len, ofs;
+ size_t len, ofs, w;
char tmpbuf[21];
const char *outstr;
@@ -306,8 +306,12 @@ int _printf(_printf_cb cb, intptr_t state, const char *fmt, va_list args)
outstr = fmt;
len = ofs - 1;
flush_str:
- if (cb(state, outstr, len) != 0)
- break;
+ if (n) {
+ w = len < n ? len : n;
+ n -= w;
+ if (cb(state, outstr, w) != 0)
+ break;
+ }
written += len;
do_escape:
@@ -331,7 +335,7 @@ static int _fprintf_cb(intptr_t state, const char *buf, size_t size)
static __attribute__((unused, format(printf, 2, 0)))
int vfprintf(FILE *stream, const char *fmt, va_list args)
{
- return _printf(_fprintf_cb, (intptr_t)stream, fmt, args);
+ return _printf(_fprintf_cb, (intptr_t)stream, SIZE_MAX, fmt, args);
}
static __attribute__((unused, format(printf, 1, 0)))
--
2.48.1
Powered by blists - more mailing lists