From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f45.google.com (mail-wm1-f45.google.com [209.85.128.45]) (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 BEC8A3CAE79 for ; Wed, 25 Mar 2026 11:29:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.45 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774438167; cv=none; b=tXAZ+SuULSlq2x2UEiLMeLbpS0GNoYC7ii+hfLDkbooQmHPlgAGXASUI6jzmcPwYl8w7QLv4yFCzvMPozc48y3eESBi5Zw0sDROcLuQ+glL5RiID+K6hccMILdmsPaLCufqDtZEgRtW1gU+T4Jr1Qh/gI7fZ4cibeuMOyc3sWJs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774438167; c=relaxed/simple; bh=6oDIgZDaG9nETqOycuutbJnKWTIoI9wAcSTTkOyEhww=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=izkGxj3YwOSfBZqUzxjaHSAs48IwKyYZ0S1av88LiyVvxWDkSyfLmq9XN1KQf+3OrZ8dMO0GP4cObj4HrfI4IQzBhTfSW6SGd1CsUdznlmz5RkLQP8/qphEZ/D6RT+vdLbdtEgJOA1ePxPmtDn3NaqmLcbsudy7Q8GgpZm32huM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=fKsEz+/g; arc=none smtp.client-ip=209.85.128.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="fKsEz+/g" Received: by mail-wm1-f45.google.com with SMTP id 5b1f17b1804b1-48374014a77so65454865e9.3 for ; Wed, 25 Mar 2026 04:29:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1774438164; x=1775042964; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=69cq4KijEbdHbgxtXPBhr7Ku+DZ4bvgpTiZn+obBDaE=; b=fKsEz+/gN8DzTWOulVuxAypCBBNM9KDs6ZionaHjiazLBmKDwq0H4JpEXZ+njp6yMJ TjSuR8V0/tfzL8tqp6kANpja68YbocJqSumzXZyGaPiLytD5Qe6V3yey17bwS+5n1oJ0 v9k5Fl40DvNF1RnfOKLXNo+T7t6YxyT+khvLxU5qHqIMFwbG6sNMNhrf0tPabAy1ZtLj 4TDBfJaYpT/tgRQqa+zPgi93fk4g7s3lfDxv763+PwX07njGnM9rLjXsfwwDJu6J1gdN sa7dnGuL7WrxqOgxty4spbGOq/sz5E8QCygcUQGyuBikik9uoJaNhvcXeSMPmWC88W42 IetA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774438164; x=1775042964; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=69cq4KijEbdHbgxtXPBhr7Ku+DZ4bvgpTiZn+obBDaE=; b=EJq+q/o1Ehsreo6zrJD4FPlA6CHPWrztr3cUGH3tl5gPRVaeLFWdttSedj2ecwldNK E7NcaDF6B8E7sGwnUPlHZ8QH/vvYj1Bp4ayR3KybbIXgZDGIeR9NqGrUx8dG4i47NI92 FwPgd5TyMNJm7BbiESBTUUQ6AoVhUeMe10MzcYm02w88X6nfvNNrR+KVtKn4A2RlTZ79 xMYn2f545dv2J0K8xfolJFZfC9EG/dNxRViIhhMQtXySPROn0lBoSI9Ns7p2HgeIMdYr AyCEPEPmwtQnWFM9rHbf3mepGots5Orf1ib6OTVJXqAiZchiZz34A2jKglakJOkV6s6M EEIw== X-Forwarded-Encrypted: i=1; AJvYcCXzONGpnJASV+ynzHfCUeO2/Qs13nWRsZPk+XNfYXEeBSSWoOkziXGnFRAiK1PV5snq/FCxhVZRYrbCF0Y=@vger.kernel.org X-Gm-Message-State: AOJu0YyrBiEqwD32RzCCJTD9Ic13KEGH9FPELzDFcjSYXoNDovE9pnJw g/n49bX7eQ4AKIQOKQUs4ggGUMZ/Fyw+gzW3Bnx2s3+hG03gRjRty81m X-Gm-Gg: ATEYQzyEoDftnas1cwKLj7qDvS7aE9ecrfKBcYh7Lj2H9wW4dwQWpUqiyftg6NX3Woz /NmvyKKbyF6N7n943KUew9d52nv5cnIGpQ2H8o4rYfjs2j2i5lu0lvWXpvhAZdFDC/JujtnRePj Qx3Qwj5SrxbxRb9KX3HxOcGqR+QTThbMbpWSEIUJDLW7qS6LwpZ+Dmh7+rcUCQPgCK3EJZMg1uR 22L1Khjwj6heQT5JKSZGulw8XexMxWqiIjBZV7UvapHhRctIFWJ7WeIXzsZRQgTDMVZyyv+dn8J e32K2GcUYbK6pUIEzudXvwAwrcUOYSx23KM/XXD5L7duoKnNY68iHPnSLHHoEhQ9MPcmKxgUYyh q9dmyI8uXGeH1taAboaahd2LLUsp52JotSvu9VP+6rVgkj973Oypwk8opfAAntHfTyncICiEMW7 WRDtUwWogLWKVRdHB/7XGZmsLkDDMW4/6rGvLxciwHMyq/H7je3G7q79+Ljirkdj1v X-Received: by 2002:a05:600c:1f8e:b0:485:4006:960c with SMTP id 5b1f17b1804b1-4871605aa53mr46681135e9.16.1774438163883; Wed, 25 Mar 2026 04:29:23 -0700 (PDT) Received: from pumpkin (82-69-66-36.dsl.in-addr.zen.co.uk. [82.69.66.36]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4871661ec65sm33726085e9.3.2026.03.25.04.29.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Mar 2026 04:29:23 -0700 (PDT) Date: Wed, 25 Mar 2026 11:29:22 +0000 From: David Laight To: Petr Mladek Cc: "Masami Hiramatsu (Google)" , Steven Rostedt , Andy Shevchenko , Rasmus Villemoes , Sergey Senozhatsky , Andrew Morton , linux-kernel@vger.kernel.org Subject: Re: [PATCH v4 1/2] lib/vsprintf: Fix to check field_width and precision Message-ID: <20260325112922.429c4ee4@pumpkin> In-Reply-To: References: <177440550682.147866.1854734911195480940.stgit@devnote2> <177440551685.147866.4375769344976474036.stgit@devnote2> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; arm-unknown-linux-gnueabihf) 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. Ugg... David > * > * 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