lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID:
 <SYBPR01MB788110A77D7F0F7A27F0974FAFC3A@SYBPR01MB7881.ausprd01.prod.outlook.com>
Date: Fri,  7 Nov 2025 13:16:13 +0800
From: Junrui Luo <moonafterrain@...look.com>
To: linux-kernel@...r.kernel.org
Cc: pmladek@...e.com,
	rostedt@...dmis.org,
	andriy.shevchenko@...ux.intel.com,
	akpm@...ux-foundation.org,
	tiwai@...e.com,
	perex@...ex.cz,
	linux-sound@...r.kernel.org,
	mchehab@...nel.org,
	awalls@...metrocast.net,
	linux-media@...r.kernel.org,
	davem@...emloft.net,
	edumazet@...gle.com,
	kuba@...nel.org,
	pabeni@...hat.com,
	netdev@...r.kernel.org,
	Junrui Luo <moonafterrain@...look.com>
Subject: [PATCH 1/4] lib/sprintf: add scnprintf_append() helper function

Add a new scnprintf_append() helper function that appends formatted
strings to an existing buffer.

The function safely handles buffer bounds and returns the total length
of the string, making it suitable for chaining multiple append operations.

Signed-off-by: Junrui Luo <moonafterrain@...look.com>
---
 include/linux/sprintf.h |  1 +
 lib/vsprintf.c          | 28 ++++++++++++++++++++++++++++
 2 files changed, 29 insertions(+)

diff --git a/include/linux/sprintf.h b/include/linux/sprintf.h
index f06f7b785091..3906e17fefec 100644
--- a/include/linux/sprintf.h
+++ b/include/linux/sprintf.h
@@ -14,6 +14,7 @@ __printf(3, 4) int snprintf(char *buf, size_t size, const char *fmt, ...);
 __printf(3, 0) int vsnprintf(char *buf, size_t size, const char *fmt, va_list args);
 __printf(3, 4) int scnprintf(char *buf, size_t size, const char *fmt, ...);
 __printf(3, 0) int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
+__printf(3, 4) int scnprintf_append(char *buf, size_t size, const char *fmt, ...);
 __printf(2, 3) __malloc char *kasprintf(gfp_t gfp, const char *fmt, ...);
 __printf(2, 0) __malloc char *kvasprintf(gfp_t gfp, const char *fmt, va_list args);
 __printf(2, 0) const char *kvasprintf_const(gfp_t gfp, const char *fmt, va_list args);
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index eb0cb11d0d12..f9540de300cd 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -3048,6 +3048,34 @@ int scnprintf(char *buf, size_t size, const char *fmt, ...)
 }
 EXPORT_SYMBOL(scnprintf);
 
+/**
+ * scnprintf_append - Append a formatted string to a buffer
+ * @buf: The buffer to append to (must be null-terminated)
+ * @size: The size of the buffer
+ * @fmt: Format string
+ * @...: Arguments for the format string
+ *
+ * This function appends a formatted string to an existing null-terminated
+ * buffer. It is safe to use in a chain of calls, as it returns the total
+ * length of the string.
+ *
+ * Returns: The total length of the string in @buf
+ */
+int scnprintf_append(char *buf, size_t size, const char *fmt, ...)
+{
+	va_list args;
+	size_t len;
+
+	len = strnlen(buf, size);
+	if (len >= size)
+		return len;
+	va_start(args, fmt);
+	len += vscnprintf(buf + len, size - len, fmt, args);
+	va_end(args);
+	return len;
+}
+EXPORT_SYMBOL(scnprintf_append);
+
 /**
  * vsprintf - Format a string and place it in a buffer
  * @buf: The buffer to place the result into
-- 
2.51.1.dirty


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ