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 02D64C83F17 for ; Fri, 11 Jul 2025 01:56:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8B0976B009B; Thu, 10 Jul 2025 21:56:54 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 867B46B009E; Thu, 10 Jul 2025 21:56:54 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 74F8B6B00A0; Thu, 10 Jul 2025 21:56:54 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 60B676B009B for ; Thu, 10 Jul 2025 21:56:54 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id D2B9D1412D6 for ; Fri, 11 Jul 2025 01:56:53 +0000 (UTC) X-FDA: 83650320306.25.7FB1FAB Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf16.hostedemail.com (Postfix) with ESMTP id 28870180003 for ; Fri, 11 Jul 2025 01:56:51 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=bYvIpIqe; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf16.hostedemail.com: domain of alx@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=alx@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1752199012; 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=WlDJiD+9QPljnXwhcj9rR0QJGJGIBllO08R2pS4QmG0=; b=L7TwY8C9Jh6KGCiF1Jo3RMGSd5wvnnSFZgdvzHc++v95MRVUxk4SFyGKIzdb1Uv5hhBfrO HHKvKvXsT1WrxFWS4GDOJLLOHxGulDmTqyIStupxX1hTn50gI5bkMOdB3CFRBo+7OFIbvC TfxMsPhestltCJ53sNcgNnNwPcHfyKg= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1752199012; a=rsa-sha256; cv=none; b=Xj+yuris5gDFwUBBqWdt1muZeDuAJpeUtI6U5ViSlgqRb9ISbzjfwFwqy4eMB1B6KmiUjm HNHGEDLZJENmGrLgWWdbBDglZeG5Jj40U9k8EC7ULt1HCeFtGc5fSp4TkNtkYfCebELOIc U/T4LGFEXOs3ZqEnL7PokFQlMsWqbJU= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=bYvIpIqe; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf16.hostedemail.com: domain of alx@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=alx@kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 36334A54B2E; Fri, 11 Jul 2025 01:56:51 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 52A27C4CEF5; Fri, 11 Jul 2025 01:56:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752199010; bh=SDtvVojw1lKkC1ixgqUR/M7GavSPKQ3ZRDqLgxzNGuk=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=bYvIpIqeRtL/++EgjV6eCZYSY5E07NzGsJ+Z4rbB41rkdxvNUGIwYD6aE58aadV0z fs7AQROuNsZRt+2ezIahVjGCHSH8YoCqBUpSfUcW0eiY6VxyWIG11P65G4YhxMrF/K aYeaJgZia5Zo2qh+2dmOCRWHRvB3kL1XloZSuGsSyfLbpYBFRlkHBJeH8HmizIy1IZ R2FzvZoQtSEV+kKsjGVuLrNbFfukR9rsdSlFpaDwp+4JOjySSEe0d10SVNlOfkoz0U l0WIXQaRyA0B7Pcj5+Pvw1xuv3Nem2HJA0oZt10hNpTNgSxQjPa7cdWchV5X8b/EBz J0sTuQkEgIUWg== Date: Fri, 11 Jul 2025 03:56:44 +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 , Andrew Clayton , Rasmus Villemoes , Michal Hocko , Linus Torvalds , Al Viro , Martin Uecker , Sam James , Andrew Pinski Subject: [RFC v6 2/8] vsprintf: Add [v]sprintf_end() Message-ID: 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-Rspam-User: X-Rspamd-Queue-Id: 28870180003 X-Rspamd-Server: rspam03 X-Stat-Signature: fcpo1xoatgbjt7kohucdjd37jfgs48ip X-HE-Tag: 1752199011-9254 X-HE-Meta: U2FsdGVkX19RRcgSTxaWvISGMVWr7tjvquEZAQmZ0gPOYyM9wCTG/A52iOhtcgzYMrmavucAlFfHkBTgyO7xdHizJSSBeC+rq4TzJV+D+tvFcQmdqmWorlHaFpFcnEgDtua8Bse9rvnhti46P43DbPDWfHOIbzMUG7DnGlD0j5LeKOdWW0d8KLJyj7g9E0FgGt5W9QLm+Qx06ST6lnNUBs42MHosxJDb7FsEHL0FUx0I4nucbwTEfaM8GZHjTKX/44o+qpS0S2nG9oLYjMM0qc87qxkqPsy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cjaNOmXvTQ1EKIf0Ptl5al2qNU9C/DxG12K2/RUye6vpGnMa0xGvfWFuhSj+VaU2+0P8sqs7q/zJL6mzo1SmK1drymaJ2wroqicQpSgkaxfEvfoMJ9Us0BK1ZgcDamOWYUFVyU4eoKTje50PNyY0/nINN63Ki3N8eWEB4Vk4to3XgiWKGR93IeFD7dXI+l27/c5btn+z2V1W0Xfh35Z1WHY2N+578eIAsA3DQ2lq4LyV61bxw0/Mz5cBBL9t198mWgfVEjHpmo= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: sprintf_end() 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 sprintf_end() calls. This results in only needing to report errors once after a chain of sprintf_end() calls, unlike snprintf(3), which requires checking after every call. p = buf; e = buf + countof(buf); p = sprintf_end(p, e, foo); p = sprintf_end(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); // No ability to check. It seems aparent that it's a more elegant approach to string catenation. These functions will soon be proposed for standardization as [v]seprintf() into C2y, and they exist in Plan9 as seprint(2) --but the Plan9 implementation has important bugs--. Link: Cc: Kees Cook Cc: Christopher Bazley Cc: Rasmus Villemoes Cc: Marco Elver Cc: Michal Hocko Cc: Linus Torvalds Cc: Al Viro Signed-off-by: Alejandro Colomar --- include/linux/sprintf.h | 2 ++ lib/vsprintf.c | 54 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/include/linux/sprintf.h b/include/linux/sprintf.h index 5ea6ec9c2e59..8dfc37713747 100644 --- a/include/linux/sprintf.h +++ b/include/linux/sprintf.h @@ -15,6 +15,8 @@ __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 sprintf_trunc(char *buf, size_t size, const char *fmt, ...); __printf(3, 0) int vsprintf_trunc(char *buf, size_t size, const char *fmt, va_list args); +__printf(3, 4) char *sprintf_end(char *p, const char end[0], const char *fmt, ...); +__printf(3, 0) char *vsprintf_end(char *p, const char end[0], const char *fmt, va_list args); __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 15e780942c56..5d0c5a0d60fd 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -2951,6 +2951,35 @@ int vsprintf_trunc(char *buf, size_t size, const char *fmt, va_list args) } EXPORT_SYMBOL(vsprintf_trunc); +/** + * vsprintf_end - va_list string end-delimited print formatted + * @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 @end <= @p, the function returns NULL. + * + * See the vsnprintf() documentation for format string extensions over C99. + */ +char *vsprintf_end(char *p, const char end[0], const char *fmt, va_list args) +{ + int len; + + if (unlikely(p == NULL)) + return NULL; + + len = vsprintf_trunc(p, end - p, fmt, args); + if (unlikely(len < 0)) + return NULL; + + return p + len; +} +EXPORT_SYMBOL(vsprintf_end); + /** * snprintf - Format a string and place it in a buffer * @buf: The buffer to place the result into @@ -3027,6 +3056,31 @@ int sprintf_trunc(char *buf, size_t size, const char *fmt, ...) } EXPORT_SYMBOL(sprintf_trunc); +/** + * sprintf_end - string end-delimited print formatted + * @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. + * If @end <= @p, the function returns NULL. + */ + +char *sprintf_end(char *p, const char end[0], const char *fmt, ...) +{ + va_list args; + + va_start(args, fmt); + p = vsprintf_end(p, end, fmt, args); + va_end(args); + + return p; +} +EXPORT_SYMBOL(sprintf_end); + /** * vsprintf - Format a string and place it in a buffer * @buf: The buffer to place the result into -- 2.50.0