From mboxrd@z Thu Jan 1 00:00:00 1970 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 E7890224AF9 for ; Wed, 25 Mar 2026 13:30:36 +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=1774445437; cv=none; b=KprqopFP900Q+sIgt22eUxH+f5VpPpPB/jq+POr73WbWJW4pRy5ln7PP5iSHNfOugvl2aImP7+l59eC6sCmbEn+GeIpCxtd4eHXOHKKGv5iHpjhbeYT/3JTUUAfEyaAZ1Zs2XMMetd60JvNn8nJa0gt1kG8Wzag6JufliPKY02k= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774445437; c=relaxed/simple; bh=2mNZeTxWz6mISD7uLyMMIHTF6Kx/frbZBBJ7lvNNMqY=; h=Date:From:To:Cc:Subject:Message-Id:In-Reply-To:References: Mime-Version:Content-Type; b=hjFoFoANFnAKCEYwU8xnoO8TXSlnE/c+5MGVUxWzb0cn1xykWfxM38OQG4aU7Haz+NZEC1/TGMRrWuXOpc7aRTDKeKxitnPmOo/RK4RdrZzddeO3K0n9mNLwkQr7Uvm9OFmNacaWUb65Vuic2ZoDof9uMCn05rlJZQ28nAX1PX4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=t2X8qaj/; 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="t2X8qaj/" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 50D55C2BCB0; Wed, 25 Mar 2026 13:30:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1774445436; bh=2mNZeTxWz6mISD7uLyMMIHTF6Kx/frbZBBJ7lvNNMqY=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=t2X8qaj/22NJ6h1+WM2V2tlo+GiQs2kTqV7jULcQ3gZjw3uLGbBsRbhtuRT7i1HqG Z14U+fsky3Hxu3yVrIZpg+onVgTbI31qRKJ9ta/FS/DgvWz+lpo6fen+i3jyLoQp3N AilJL2JPxaU2okaCiLBMiFdcC2Rq+3mOJT13oqf7erS1LHsbA1IrLiHve6f89Cqpt4 3f6g1esbdhZYboXqqWFbyVOlyv3MITHuEYtLvk8PyXWWwbsEJtNCTTzmBkn+N2LHGv xbE03zAQPCXMxSYDfwjAHUrJlH3gAQlm1RaVs6kIaTE3fLB5p0rOjsaTQIZ3fENozd 0BEQOe2/x9pHQ== Date: Wed, 25 Mar 2026 22:30:31 +0900 From: Masami Hiramatsu (Google) To: Petr Mladek Cc: Steven Rostedt , Andy Shevchenko , Rasmus Villemoes , Sergey Senozhatsky , Andrew Morton , David Laight , linux-kernel@vger.kernel.org Subject: Re: [PATCH v4 1/2] lib/vsprintf: Fix to check field_width and precision Message-Id: <20260325223031.a466d740b31437836f2a8d4e@kernel.org> In-Reply-To: References: <177440550682.147866.1854734911195480940.stgit@devnote2> <177440551685.147866.4375769344976474036.stgit@devnote2> X-Mailer: Sylpheed 3.8.0beta1 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Wed, 25 Mar 2026 11:22:47 +0100 Petr Mladek wrote: > On Wed 2026-03-25 11:25:16, Masami Hiramatsu (Google) wrote: > > From: Masami Hiramatsu (Google) > > > > Check the field_width and presition correctly. Previously it depends > > on the bitfield conversion from int to check out-of-range error. > > However, commit 938df695e98d ("vsprintf: associate the format state > > with the format pointer") changed those fields to int. > > We need to check the out-of-range correctly without bitfield > > conversion. > > > > --- a/lib/vsprintf.c > > +++ b/lib/vsprintf.c > > @@ -2679,9 +2679,6 @@ struct fmt format_decode(struct fmt fmt, struct printf_spec *spec) > > > > /* we finished early by reading the precision */ > > if (unlikely(fmt.state == FORMAT_STATE_PRECISION)) { > > - if (spec->precision < 0) > > - spec->precision = 0; > > This changes the existing kernel behavior and breaks the existing > KUnit test in lib/tests/printf_kunit.c: > > static void > test_string(struct kunit *kunittest) > { > [...] > /* > * POSIX and C99 say that a negative precision (which is only > * possible to pass via a * argument) should be treated as if > * the precision wasn't present, and that if the precision is > * omitted (as in %.s), the precision should be taken to be > * 0. However, the kernel's printf behave exactly opposite, > * treating a negative precision as 0 and treating an omitted > * precision specifier as if no precision was given. > * > * These test cases document the current behaviour; should > * anyone ever feel the need to follow the standards more > * closely, this can be revisited. > */ Yeah, I also found this comment. So v5 drops the negative precision support. > test(" ", "%4.*s", -5, "123456"); > [...] > } > > The output is: > > [ 86.234405] # test_string: EXPECTATION FAILED at lib/tests/printf_kunit.c:56 > lib/tests/printf_kunit.c:208: vsnprintf(buf, 256, "%4.*s", ...) returned 6, expected 4 > [ 86.237524] # test_string: EXPECTATION FAILED at lib/tests/printf_kunit.c:56 > lib/tests/printf_kunit.c:208: vsnprintf(buf, 2, "%4.*s", ...) returned 6, expected 4 > [ 86.237542] # test_string: EXPECTATION FAILED at lib/tests/printf_kunit.c:56 > lib/tests/printf_kunit.c:208: vsnprintf(buf, 0, "%4.*s", ...) returned 6, expected 4 > [ 86.237559] # test_string: EXPECTATION FAILED at lib/tests/printf_kunit.c:141 > lib/tests/printf_kunit.c:208: kvasprintf(..., "%4.*s", ...) returned '123456', expected ' ' > > Do we really want to change the existing behavior? Of course no. > Would it break any existing kernel caller? it is possible. Anyway to update the behavior, we also need to update the test case. > > I would personally keep the existing behavior unless anyone checks > the existing callers. OK. Thanks, > > > - > > fmt.state = FORMAT_STATE_NONE; > > goto qualifier; > > } > > @@ -2802,19 +2799,17 @@ struct fmt format_decode(struct fmt fmt, struct printf_spec *spec) > > static void > > set_field_width(struct printf_spec *spec, int width) > > { > > - spec->field_width = width; > > - if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) { > > - spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX); > > - } > > + spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX); > > + WARN_ONCE(spec->field_width != width, "field width %d out of range", > > + width); > > } > > > > static void > > set_precision(struct printf_spec *spec, int prec) > > { > > - spec->precision = prec; > > - if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) { > > - spec->precision = clamp(prec, 0, PRECISION_MAX); > > - } > > + /* We allow negative precision, but treat it as if there was no precision. */ > > + spec->precision = clamp(prec, -1, PRECISION_MAX); > > And I would keep clamp(prec, 0, PRECISION_MAX) unless anyone checks > that changing the existing behavior does not break existing > callers. > > > + WARN_ONCE(spec->precision < prec, "precision %d too large", prec); > > } > > Best Regards, > Petr -- Masami Hiramatsu (Google)