From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f47.google.com (mail-wm1-f47.google.com [209.85.128.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C5A4F391859 for ; Wed, 25 Mar 2026 10:22:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.47 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774434172; cv=none; b=q2If/Ax1nULQs0DYZdmmDPatyAcWgwzQaZ7hd6p8SLU+fJFqrjyWi+j2+5QjEzqrNLXGC6WLmSnItzFoO1kSENI5DbyroP0+W18IAni5TT+rayrOpeiwJOIP+H9bUG+D1z8tghTmuFawedqRjkDY/9t/WDA3nJqrLmxbA3F4kEU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774434172; c=relaxed/simple; bh=3jFBM8VxzHyHcLwO4jxjXYiPCeInvWOn11lscb7Ygn8=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=oKF32Gwz2RNS8iv2CJ7U0aZdpI9LNJE1Cn1LZnY/ROEvO/lSflKAf9H6NEi41Li++iQbn9VsnD8SUGnTCM1JXKZ/zu3n60kUAU28AyZjn9HOIq8TyawX7Yph76j9Zs6WKljypmjOWoC6fTTk4MapS/wcl/vYFNBPSvtPMpj+ILw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com; spf=pass smtp.mailfrom=suse.com; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.b=R3iT0xUw; arc=none smtp.client-ip=209.85.128.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.b="R3iT0xUw" Received: by mail-wm1-f47.google.com with SMTP id 5b1f17b1804b1-4852afd42ceso16965745e9.2 for ; Wed, 25 Mar 2026 03:22:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1774434169; x=1775038969; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=yjMRQ6MhCWyXMd+cuPGmCexyPNlI3S3bsRZRcS5iWzg=; b=R3iT0xUwe1TDTQ2dimM0lJfuqW+ys6QxI3+samA2CV9XrYh2A1EI95Spq6T6vyMbuG 0Gxkjg1GJ91dkw93pIEjsV78nO3pHdCdXJaqktfpFM3o6T4Bujqr2mgQL5l2rKFWibSd slBbVlz2avG9I2rc3K5HRRg6zsgb04DzXOgpSxAoluLLpW7G8cnyag2P9g9U2TAWTXTW ew/68tar8NM62IpuoI9bkxsDEcFqISdDTp/ABrrBrWiRWVTIrErjtaX3CevS6QeNERpB jLQFsPN+/jtOiugEDzaNPDaWReYjPIeX/tbyd6hc4ZomSuX7X8KQV+WsJqh7uyOwxcF9 1o1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774434169; x=1775038969; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=yjMRQ6MhCWyXMd+cuPGmCexyPNlI3S3bsRZRcS5iWzg=; b=XioJxOBua3yUg8TA+/hz0+QWqqiU/74stlyxsvwknL5OKwKuBmRXwgrr2cleHOHY8y /fb23lFSieFtVRXK8xaTprOCA54KNePasvgss/Cv5oHTZScD/blZyWMxMzf9OdwqL5UK FbWjY8KAcuu3O9vA9dI0SpwCgDX3i14ANv2sqq0ICO8z/wRYfbB22V/46mKq4SDfnQCL 1cn9b8H37eHMXezJ4Nzn54wPOjY5Kq5NB1m3TryUX5cTuT7Hy8ZU4kbatcjqG/S4DLCB NwJp9MVCpA7OJpO2eu2dJc08smQNVy7z2LvAEhSGUoDi7mStyTetIeKS/Y5kxB8dtWih HagA== X-Forwarded-Encrypted: i=1; AJvYcCWVzTGIJRCvK1K4KSuDIsUHHqzghyHnIILGX9UJmI+k32diY3rmzqAGLWNq4LpUtw/i+zq+7P3eIzVTus4=@vger.kernel.org X-Gm-Message-State: AOJu0YxvGM9wyIx+qUZ4QcwQKrMbd31rRZ6gxhJ8GXYcdpTWaNCFwemR mDaqcQBOe7Y1Asec9e/Ls+UOEBJajSw/P0FaiZa2aCVb1/BX27iTPOa4JHZCv2tHT5w= X-Gm-Gg: ATEYQzyDLXEQSKXShuFtNNFVz2kEwjWz7dIA2mZfTSkBNjjQ1ubCMQFuCHwdoWb84Jv N7CWQl08Zh6s//Kx1gX/2O2K/+0P87ouvbp/YPfJnRtfW4SOm3Hq0rio/PgFDStAPT2K5jRnK8O 3bwH5G45BahtSWARtasi5N2902kL0Hh7Z/y/1BqFw/X7xHvgPefC6MUmII74B3odOuBtzzG6U27 NY4q4SadcacbDGGM9UnL73DDBkb+8VWioXiyNCTN4XGxZX7ACq+GZHjhGqnXz8qTvqvBwdBvMEb U5H2vNaXUP8BH2nApxPHMkNLOUQTWoT1fCTLwePb/FEghgS7/YNJAJm/z8Y08RqB/aKBhiMQ9Ca RIH9BoatXycKcVNbe5tfSs4lGHaP5h7sA6DHZXK6NREz7MiRiPD/vKuX1wsWj6QO7pCTHSQYjNY r1O5lFbC94R2m8tSoOXdEpTiFGKA== X-Received: by 2002:a05:600c:4e50:b0:477:76bf:e1fb with SMTP id 5b1f17b1804b1-4871605071dmr47914635e9.16.1774434169048; Wed, 25 Mar 2026 03:22:49 -0700 (PDT) Received: from pathway.suse.cz ([176.114.240.130]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-487117077cbsm117246065e9.6.2026.03.25.03.22.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Mar 2026 03:22:48 -0700 (PDT) Date: Wed, 25 Mar 2026 11:22:47 +0100 From: Petr Mladek To: "Masami Hiramatsu (Google)" 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: References: <177440550682.147866.1854734911195480940.stgit@devnote2> <177440551685.147866.4375769344976474036.stgit@devnote2> 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-Disposition: inline In-Reply-To: <177440551685.147866.4375769344976474036.stgit@devnote2> 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. */ 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? Would it break any existing kernel caller? I would personally keep the existing behavior unless anyone checks the existing callers. > - > 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