From nobody Tue Apr 7 07:08:24 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 44F92ECAAD4 for ; Tue, 30 Aug 2022 00:33:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229715AbiH3AdR (ORCPT ); Mon, 29 Aug 2022 20:33:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32950 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229531AbiH3AdN (ORCPT ); Mon, 29 Aug 2022 20:33:13 -0400 Received: from out2.migadu.com (out2.migadu.com [IPv6:2001:41d0:2:aacc::]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 16BD5760D8 for ; Mon, 29 Aug 2022 17:33:12 -0700 (PDT) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1661819590; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=ga7uskWn3UNZiozr0UHy+8EXtSLpa+LxL+/sfY9E+7E=; b=oWVGcX4V2BzPw3iqrRBIh8mL/8+H1JpFqDNsfVgSY7pr+bOW9q0e8do3a0o56iaC9+7Oig x3g5u6h3iB0NCCbwCQq0XlGqwqnq/q8AXoouiel4xKEJ4ieZdl4AAvuac5FlRhCnznLENb 91OEaeY7vmreCKwd5divUfha4yhGcJo= From: Kent Overstreet To: linux-kernel@vger.kernel.org Cc: Kent Overstreet , Petr Mladek , Rasmus Villemoes , Kent Overstreet Subject: [PATCH v6.1] vsprintf: prt_u64_minwidth(), prt_u64() Date: Mon, 29 Aug 2022 20:33:02 -0400 Message-Id: <20220830003302.1794119-1-kent.overstreet@linux.dev> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Kent Overstreet This adds two new-style printbuf helpers for printing simple u64s, and converts num_to_str() to be a simple wrapper around prt_u64_minwidth(). Signed-off-by: Kent Overstreet Cc: Petr Mladek Cc: Rasmus Villemoes Signed-off-by: Kent Overstreet --- include/linux/kernel.h | 5 ++- lib/vsprintf.c | 95 ++++++++++++++++++++---------------------- 2 files changed, 50 insertions(+), 50 deletions(-) diff --git a/include/linux/kernel.h b/include/linux/kernel.h index 1906861ece..ea65d5d9ef 100644 --- a/include/linux/kernel.h +++ b/include/linux/kernel.h @@ -202,12 +202,15 @@ static inline void might_fault(void) { } =20 void do_exit(long error_code) __noreturn; =20 +struct printbuf; +extern void prt_u64_minwidth(struct printbuf *out, u64 num, + unsigned width, bool zeropad); +extern void prt_u64(struct printbuf *out, u64 num); extern int num_to_str(char *buf, int size, unsigned long long num, unsigned int width); =20 /* lib/printf utilities */ =20 -struct printbuf; extern __printf(2, 3) void prt_printf(struct printbuf *out, const char *fm= t, ...); extern __printf(2, 0) void prt_vprintf(struct printbuf *out, const char *f= mt, va_list); =20 diff --git a/lib/vsprintf.c b/lib/vsprintf.c index 87adc528c6..63aae10225 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -368,41 +368,52 @@ char *put_dec(char *buf, unsigned long long n) =20 #endif =20 -/* - * Convert passed number to decimal string. - * Returns the length of string. On buffer overflow, returns 0. - * - * If speed is not important, use snprintf(). It's easy to read the code. +/** + * prt_u64_minwidth - print a u64, in decimal, with zero padding + * @out: printbuf to output to + * @num: u64 to print + * @width: minimum width */ -int num_to_str(char *buf, int size, unsigned long long num, unsigned int w= idth) +void prt_u64_minwidth(struct printbuf *out, u64 num, unsigned width, + bool zeropad) { /* put_dec requires 2-byte alignment of the buffer. */ char tmp[sizeof(num) * 3] __aligned(2); - int idx, len; + unsigned len =3D put_dec(tmp, num) - tmp; =20 - /* put_dec() may work incorrectly for num =3D 0 (generate "", not "0") */ - if (num <=3D 9) { - tmp[0] =3D '0' + num; - len =3D 1; - } else { - len =3D put_dec(tmp, num) - tmp; - } + printbuf_make_room(out, max(len, width)); =20 - if (len > size || width > size) - return 0; + if (width > len) + __prt_chars_reserved(out, zeropad ? '0' : ' ', width - len); =20 - if (width > len) { - width =3D width - len; - for (idx =3D 0; idx < width; idx++) - buf[idx] =3D ' '; - } else { - width =3D 0; - } + while (len) + __prt_char_reserved(out, tmp[--len]); + printbuf_nul_terminate(out); +} =20 - for (idx =3D 0; idx < len; ++idx) - buf[idx + width] =3D tmp[len - idx - 1]; +/** + * prt_u64 - print a simple u64, in decimal + * @out: printbuf to output to + * @num: u64 to print + */ +void prt_u64(struct printbuf *out, u64 num) +{ + prt_u64_minwidth(out, num, 0, false); +} =20 - return len + width; +/* + * Convert passed number to decimal string. + * Returns the length of string. On buffer overflow, returns 0. + * + * Consider switching to printbufs and using prt_u64() or prt_u64_minwith() + * instead. + */ +int num_to_str(char *buf, int size, unsigned long long num, unsigned int w= idth) +{ + struct printbuf out =3D PRINTBUF_EXTERN(buf, size); + + prt_u64_minwidth(&out, num, width, false); + return out.pos; } =20 #define SIGN 1 /* unsigned/signed, must be 1 */ @@ -993,20 +1004,6 @@ static const struct printf_spec default_dec_spec =3D { .precision =3D -1, }; =20 -static const struct printf_spec default_dec02_spec =3D { - .base =3D 10, - .field_width =3D 2, - .precision =3D -1, - .flags =3D ZEROPAD, -}; - -static const struct printf_spec default_dec04_spec =3D { - .base =3D 10, - .field_width =3D 4, - .precision =3D -1, - .flags =3D ZEROPAD, -}; - static noinline_for_stack void resource_string(struct printbuf *out, struct resource *res, struct printf_spec spec, const char *fmt) @@ -1206,12 +1203,12 @@ void bitmap_list_string(struct printbuf *out, unsig= ned long *bitmap, prt_char(out, ','); first =3D false; =20 - number(out, rbot, default_dec_spec); + prt_u64(out, rbot); if (rtop =3D=3D rbot + 1) continue; =20 prt_char(out, '-'); - number(out, rtop - 1, default_dec_spec); + prt_u64(out, rtop - 1); } } =20 @@ -1753,21 +1750,21 @@ void date_str(struct printbuf *out, int year =3D tm->tm_year + (r ? 0 : 1900); int mon =3D tm->tm_mon + (r ? 0 : 1); =20 - number(out, year, default_dec04_spec); + prt_u64_minwidth(out, year, 4, true); prt_char(out, '-'); - number(out, mon, default_dec02_spec); + prt_u64_minwidth(out, mon, 2, true); prt_char(out, '-'); - number(out, tm->tm_mday, default_dec02_spec); + prt_u64_minwidth(out, tm->tm_mday, 2, true); } =20 static noinline_for_stack void time_str(struct printbuf *out, const struct rtc_time *tm, bool r) { - number(out, tm->tm_hour, default_dec02_spec); + prt_u64_minwidth(out, tm->tm_hour, 2, true); prt_char(out, ':'); - number(out, tm->tm_min, default_dec02_spec); + prt_u64_minwidth(out, tm->tm_min, 2, true); prt_char(out, ':'); - number(out, tm->tm_sec, default_dec02_spec); + prt_u64_minwidth(out, tm->tm_sec, 2, true); } =20 static noinline_for_stack @@ -2045,7 +2042,7 @@ void device_node_string(struct printbuf *out, struct = device_node *dn, str_spec.precision =3D precision; break; case 'p': /* phandle */ - number(out, (unsigned int)dn->phandle, default_dec_spec); + prt_u64(out, (unsigned int)dn->phandle); break; case 'P': /* path-spec */ p =3D fwnode_get_name(of_fwnode_handle(dn)); --=20 2.36.1