From nobody Tue Oct 7 10:30:59 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 52472288CC; Fri, 11 Jul 2025 01:56:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752199011; cv=none; b=j3ema35u2k6cmJ6FUqOzDEFyx6kXczHJHJ1l0Nfx62JFcw9TBdmriSUEKmohdctmiwmcuJWkNs9CD+GowyPFO5QEyv34deO9G8/gtxtuCfEWjF5psFw1Ydqpaqn/0OZ4AEUwHRiawf52IYdB7gJClDGkMt9sQ0+z9f070IBKoMQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752199011; c=relaxed/simple; bh=SDtvVojw1lKkC1ixgqUR/M7GavSPKQ3ZRDqLgxzNGuk=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=SaOwgvyOdi9cpmb4JEl7g11g6pUcOOaAsA/M9+pNF0iphDUHuLHaLC9hG5sqePbS8iRuZflx32kbppD7azC8wl7NWjZTiX9M2gqzYFxDalmd/p5wvf2BJDR2z1XpLNur7qwPi697qpUQCV4htb6d3PUVEHj6afXy+R0q3F5las4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=bYvIpIqe; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="bYvIpIqe" 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: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 =3D buf; e =3D buf + countof(buf); p =3D sprintf_end(p, e, foo); p =3D sprintf_end(p, e, bar); if (p =3D=3D NULL) goto trunc; vs len =3D 0; size =3D countof(buf); len +=3D snprintf(buf + len, size - len, foo); if (len >=3D size) goto trunc; len +=3D snprintf(buf + len, size - len, bar); if (len >=3D size) goto trunc; And also better than scnprintf() calls: len =3D 0; size =3D countof(buf); len +=3D scnprintf(buf + len, size - len, foo); len +=3D 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, cons= t 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 *f= mt, ...); +__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_li= st 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 cha= r *fmt, va_list args) } EXPORT_SYMBOL(vsprintf_trunc); =20 +/** + * 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 <=3D @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 ar= gs) +{ + int len; + + if (unlikely(p =3D=3D NULL)) + return NULL; + + len =3D 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); =20 +/** + * 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 <=3D @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 =3D 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 --=20 2.50.0