From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9AA6FC8303C for ; Sat, 5 Jul 2025 20:33:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2B6836B8075; Sat, 5 Jul 2025 16:33:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 28E1B6B8067; Sat, 5 Jul 2025 16:33:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 17D2C6B8075; Sat, 5 Jul 2025 16:33:55 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 018296B8067 for ; Sat, 5 Jul 2025 16:33:54 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id BF0935C7DB for ; Sat, 5 Jul 2025 20:33:54 +0000 (UTC) X-FDA: 83631362388.05.CF364F5 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf12.hostedemail.com (Postfix) with ESMTP id 0FDFC4000D for ; Sat, 5 Jul 2025 20:33:52 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=O5hbx9w9; spf=pass (imf12.hostedemail.com: domain of alx@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=alx@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1751747633; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=j2yop7tjrpnvs6mdWsoDaUXdWC6LBa9pXyqPT6BSFm4=; b=5mjhlL/Ly+CP04gEQLpkqu1MIV+q5gDRZJEXu92pXzrC7A4GlwwgfwDpsuBJv0W1jc3YAY Ha/hvGCB7ln5txPN7/7qd8IRuwiYPpbJPpcxFBvwDOHWwH7sQWTLsYBRwwaXcA0UYnfJrK yPRIjobholHC2bnmIbgh0drFssIFDG4= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=O5hbx9w9; spf=pass (imf12.hostedemail.com: domain of alx@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=alx@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1751747633; a=rsa-sha256; cv=none; b=aM42nymcv4dSBbySnIrtY/NCG1IniWkt75Tg9CHlBHHx6T3U13+paaySeqv63KFB3l7z51 VZjSONvQsRVnAJzEArm4RQvxQvCqA7vhTvOcJClDb+njeb2ID56CDxVi346efGqSkTqnA3 aM9lve6s8Y4qMGiZkb77Ugl95n5znLc= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id CEBA35C5525; Sat, 5 Jul 2025 20:33:51 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 91627C4CEED; Sat, 5 Jul 2025 20:33:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1751747631; bh=c7Bo2eDkD0F/AkM6BOw7KB8MLG0DrouzGLLc8l13biQ=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=O5hbx9w97+zyL6YBKFfr9nF1GPnuY/5ukMqCyyRIsPrABCtwHhVPAN/DnV2u6c+32 xW0bHiikiwyJy6yvZefvbyxFRSAlii1ZZ5DcZ7TvOFWMiNYWFs3YWfFTs9ctbxrLQ4 i2zJ6myUejCOyNYQokLo05iwY3fC4MG64DZVHzQfhDMNIzHds+LfSkTvWbZocOTwfZ Cwlfe3KkuQbIYiKAqFHQBk6H8OzM+HYxCfBugJn6+Cu534Kh+7NumcgA4GDuom7cHt UV3jYy5PerDxlbSeUx35OvGQM9tVypdyOEQlp76wRC4BI3oLy4nxOIxrdp5ct+ybWG Rk9LjLniHg0rA== Date: Sat, 5 Jul 2025 22:33:49 +0200 From: Alejandro Colomar To: linux-mm@kvack.org, linux-hardening@vger.kernel.org Cc: Alejandro Colomar , Kees Cook , Christopher Bazley , shadow <~hallyn/shadow@lists.sr.ht>, linux-kernel@vger.kernel.org, Andrew Morton , kasan-dev@googlegroups.com, Dmitry Vyukov , Alexander Potapenko , Marco Elver , Christoph Lameter , David Rientjes , Vlastimil Babka , Roman Gushchin , Harry Yoo Subject: [RFC v1 1/3] vsprintf: Add [v]seprintf(), [v]stprintf() Message-ID: <2d20eaf1752efefcc23d0e7c5c2311dd5ae252af.1751747518.git.alx@kernel.org> X-Mailer: git-send-email 2.50.0 References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: X-Rspamd-Queue-Id: 0FDFC4000D X-Stat-Signature: inqahf9h1za97i6f6uxdfs5m5mbbfnfc X-Rspam-User: X-Rspamd-Server: rspam05 X-HE-Tag: 1751747632-34671 X-HE-Meta: U2FsdGVkX19JKfzrdMMizgL4Y/21zcp8kINw9yTQe+TjITQqkdf+vVIhcTj+HvahPJ4c8I85ClbDGU/egxRbaNuPP8TOlAi2hx4DeUWiGB4TG2UlpE+nFKoYxCkoPmHXUCx7w2DUxytWbYMIA8aP7hFd4xKic3KHwwopUucjgVLivYpjG5phcbTkF/MPx7SW3/A/gZyPnceVjaWweWSt2mc7eApn7oYIKgViUF0gHkiiKDcDvSgFaamz+z4ZcjeQmwoJBnrfCSZti4LxpBESeDGmpIKtciSvI+h5haSRWOvskoH8YBuKAC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Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: seprintf() ========== seprintf() is a function similar to stpcpy(3) in the sense that it returns a pointer that is suitable for chaining to other copy operations. It takes a pointer to the end of the buffer as a sentinel for when to truncate, which unlike a size, doesn't need to be updated after every call. This makes it much more ergonomic, avoiding manually calculating the size after each copy, which is error prone. It also makes error handling much easier, by reporting truncation with a null pointer, which is accepted and transparently passed down by subsequent seprintf() calls. This results in only needing to report errors once after a chain of seprintf() calls, unlike snprintf(3), which requires checking after every call. p = buf; e = buf + countof(buf); p = seprintf(p, e, foo); p = seprintf(p, e, bar); if (p == NULL) goto trunc; vs len = 0; size = countof(buf); len += snprintf(buf + len, size - len, foo); if (len >= size) goto trunc; len += snprintf(buf + len, size - len, bar); if (len >= size) goto trunc; And also better than scnprintf() calls: len = 0; size = countof(buf); len += scnprintf(buf + len, size - len, foo); len += scnprintf(buf + len, size - len, bar); if (len >= size) goto trunc; It seems aparent that it's a more elegant approach to string catenation. stprintf() ========== stprintf() is a helper that is needed for implementing seprintf() --although it could be open-coded within vseprintf(), of course--, but it's also useful by itself. It has the same interface properties as strscpy(): that is, it copies with truncation, and reports truncation with -E2BIG. It would be useful to replace some calls to snprintf(3) and scnprintf() which don't need chaining, and where it's simpler to pass a size. It is better than plain snprintf(3), because it results in simpler error detection (it doesn't need a check >=countof(buf), but rather <0). Cc: Kees Cook Cc: Christopher Bazley Signed-off-by: Alejandro Colomar --- lib/vsprintf.c | 109 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) diff --git a/lib/vsprintf.c b/lib/vsprintf.c index 01699852f30c..a3efacadb5e5 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -2892,6 +2892,37 @@ int vsnprintf(char *buf, size_t size, const char *fmt_str, va_list args) } EXPORT_SYMBOL(vsnprintf); +/** + * vstprintf - Format a string and place it in a buffer + * @buf: The buffer to place the result into + * @size: The size of the buffer, including the trailing null space + * @fmt: The format string to use + * @args: Arguments for the format string + * + * The return value is the length of the new string. + * If the string is truncated, the function returns -E2BIG. + * + * If you're not already dealing with a va_list consider using stprintf(). + * + * See the vsnprintf() documentation for format string extensions over C99. + */ +int vstprintf(char *buf, size_t size, const char *fmt, va_list args) +{ + int len; + + len = vsnprintf(buf, size, fmt, args); + + // It seems the kernel's vsnprintf() doesn't fail? + //if (unlikely(len < 0)) + // return -E2BIG; + + if (unlikely(len >= size)) + return -E2BIG; + + return len; +} +EXPORT_SYMBOL(vstprintf); + /** * vscnprintf - Format a string and place it in a buffer * @buf: The buffer to place the result into @@ -2923,6 +2954,36 @@ int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) } EXPORT_SYMBOL(vscnprintf); +/** + * vseprintf - Format a string and place it in a buffer + * @p: The buffer to place the result into + * @end: A pointer to one past the last character in the buffer + * @fmt: The format string to use + * @args: Arguments for the format string + * + * The return value is a pointer to the trailing '\0'. + * If @p is NULL, the function returns NULL. + * If the string is truncated, the function returns NULL. + * + * If you're not already dealing with a va_list consider using seprintf(). + * + * See the vsnprintf() documentation for format string extensions over C99. + */ +char *vseprintf(char *p, const char end[0], const char *fmt, va_list args) +{ + int len; + + if (unlikely(p == NULL)) + return NULL; + + len = vstprintf(p, end - p, fmt, args); + if (unlikely(len < 0)) + return NULL; + + return p + len; +} +EXPORT_SYMBOL(vseprintf); + /** * snprintf - Format a string and place it in a buffer * @buf: The buffer to place the result into @@ -2950,6 +3011,30 @@ int snprintf(char *buf, size_t size, const char *fmt, ...) } EXPORT_SYMBOL(snprintf); +/** + * stprintf - Format a string and place it in a buffer + * @buf: The buffer to place the result into + * @size: The size of the buffer, including the trailing null space + * @fmt: The format string to use + * @...: Arguments for the format string + * + * The return value is the length of the new string. + * If the string is truncated, the function returns -E2BIG. + */ + +int stprintf(char *buf, size_t size, const char *fmt, ...) +{ + va_list args; + int len; + + va_start(args, fmt); + len = vstprintf(buf, size, fmt, args); + va_end(args); + + return len; +} +EXPORT_SYMBOL(stprintf); + /** * scnprintf - Format a string and place it in a buffer * @buf: The buffer to place the result into @@ -2974,6 +3059,30 @@ int scnprintf(char *buf, size_t size, const char *fmt, ...) } EXPORT_SYMBOL(scnprintf); +/** + * seprintf - Format a string and place it in a buffer + * @p: The buffer to place the result into + * @end: A pointer to one past the last character in the buffer + * @fmt: The format string to use + * @...: Arguments for the format string + * + * The return value is a pointer to the trailing '\0'. + * If @buf is NULL, the function returns NULL. + * If the string is truncated, the function returns NULL. + */ + +char *seprintf(char *p, const char end[0], const char *fmt, ...) +{ + va_list args; + + va_start(args, fmt); + p = vseprintf(p, end, fmt, args); + va_end(args); + + return p; +} +EXPORT_SYMBOL(seprintf); + /** * vsprintf - Format a string and place it in a buffer * @buf: The buffer to place the result into -- 2.50.0