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 05486347529 for ; Wed, 25 Mar 2026 10:00:14 +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=1774432816; cv=none; b=UAWcn++ngpfktFyWjt68JyYaNxaibMruBK/JKS6LgaTuaOtIglCbqyBo1SESvXg6n30fDQRSUSHlZCoeOoMkX5JaqGxRPF+/ZqMOATiv96cokC3g+dHSk3rOky0seA5Kjde5txJc1t1Yce6YEdh779O0LQ55CTn3jrMWcWNVBPQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774432816; c=relaxed/simple; bh=hvBzNZyYXAqwis29w/YN7a2/0vSFCZIE2DOLtEqJrpc=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=LgYo4P4CDlLqEu7rezIfIkiFJn7WzHgTZKp7WrkQFTHCTrp3o6N2o+a85g3ERUuMavEsCRumfcqKIgpVvkb5xa6BaPkW9EpdwwXu2vFP+QJv3LykHdsle68/1EHEktuM351AuxueqvymdWi3ejGliYjbr7ZUQV/W5gBNkn7nxAs= 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=qEjBnoQJ; 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="qEjBnoQJ" Received: by mail-wm1-f45.google.com with SMTP id 5b1f17b1804b1-4853e1ce427so52466145e9.3 for ; Wed, 25 Mar 2026 03:00:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1774432813; x=1775037613; 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=/6hGFLKRM0DmEy8Kmyk+4OqxYjtgyRQQdUqi/W223SM=; b=qEjBnoQJkhMo6JLOn68MdCk93DspYHezGJeddVvuslPWia37+vih5/icwvRW2wcHRv 487XI7mKFidqeON+6/k/0UXwWpUFaXarTvUbeZ7aj4Ii7LIPMeADhii36NORseT0KQk4 L3XtCK2A3zX8xYK4zl3p8qFnWID2oxEbMNyxmyAuaSpG4TVy2hj31/HlsfLU0QnNAqM9 +V213/1exEm4H2iunZTRrcQhTynHrVClq03M3cbNX2bK9JeppBlKOwdZRuYh4ySTz8Xb uLjFkR+ZdxNCvSsjkVLUPjBkjeeqZw5ozuVgq4+3BvMpBF2zd56vd94kzLyAH66jrevK CIGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774432813; x=1775037613; 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=/6hGFLKRM0DmEy8Kmyk+4OqxYjtgyRQQdUqi/W223SM=; b=emgYPoP4O/a1UWC5fJVRhH3rdzk6Eo2tYVRrBz5sv3oD6Keb7P4p69j7ufZoa6cxzW eTJvy6lqrHHa+ND+2Sp8Tmmm0qR2yJ59yxGjoIbj2QdZ5XdU0/qkhE/L1iWcMYVA8xyK 8OS/v3aB0Vwfq5y6+o1uzbT7OFLhZiz3XnSzm+BiWRpasbTFwgIrqVxv2JYozeWGOYUa 5G7Tg6imn5Dgk88hq2v4jcUHTygIKWvGkFloHqnvEgrdXQkM9Crd6d1vLGkELMy+Lz39 IxpywAAjroJp0eX8Fl57zUr9fcQl+IO4e1khc/iak7MiRSzrJ1HlhoDqTyo3DhQHaJ/a 6EzA== X-Forwarded-Encrypted: i=1; AJvYcCVq5iDQNUpksS72tVohuQfzkrAEI/DsShXqiMYN1DwaM7ik+2wDHE2BcnCJp56chZe974pMh4yuPjpBNZA=@vger.kernel.org X-Gm-Message-State: AOJu0YyKqx0aXinMBeSj3gUBhNfru9b1E5bPwsZrXPFU1nTcC+/qNDny 1Oiz9/fxEC29vOwzYqvlhdJt7H9Y+wedX7C87YU98jfzFRaUsUrSpVgA X-Gm-Gg: ATEYQzwWloDbyd6S6+h/xzDhYf2iTfQ9SNKBBrViLfPVObWf71nB8TbuEQWzH5tgBB8 DCxgQj7zMutt8hCg99HSfjRsySqJ559T/+cxOxKk2xiO518YPnwYkIvjjZZIedIygJuB92YWZkd 7TmnxRoPB6QgIDPvc23ye5zBbcnqeQvKYlUjEyhbphIf8Sgkpr16N0SBA14BdzIhjvbA/ic1sGN fltVV9/mMryQ8KD/fZ040JSf2OsAwAQTI9YkWTXLehU09n04vGkZXjATUhE/eQzZm80OS9j3ByE NXa/kK5kZLkKVJV89/rufBqt9xPm+GiCrRsCBeYaixD7wiR2dNfGdLR6guyE278y6+D5j268X2J ZLmaGgj9WfEHEKOo8b0C4bN3HNYNWtVkxHxGRK7X7gdvDqRQgAXkFFDbZ1QBhCHGIr3p3fRnvuA BtCl84+6O0s72oh0gqSwdtBvIRsW/GL/NUAKSW3MqbNUfWMZPtPoZ7xMcy7chimiYT X-Received: by 2002:a05:600c:8b88:b0:487:1c2:6a4c with SMTP id 5b1f17b1804b1-48715fbf6a6mr45413995e9.4.1774432812905; Wed, 25 Mar 2026 03:00:12 -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-4871b905f7dsm3567165e9.6.2026.03.25.03.00.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Mar 2026 03:00:12 -0700 (PDT) Date: Wed, 25 Mar 2026 10:00:09 +0000 From: David Laight To: "Masami Hiramatsu (Google)" Cc: Petr Mladek , 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: <20260325100009.42871082@pumpkin> In-Reply-To: <177440551685.147866.4375769344976474036.stgit@devnote2> 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:25:16 +0900 "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. > > Fixes: 938df695e98d ("vsprintf: associate the format state with the format pointer") > Reported-by: David Laight > Closes: https://lore.kernel.org/all/20260318151250.40fef0ab@pumpkin/ > Signed-off-by: Masami Hiramatsu (Google) I've just read the code - my god is it complicated and confusing. But I think it looks ok, so: Reviewed-by: David Laight Maybe I'll find to time (haha) to rewrite it based on the nolibc version. It'll be a lot smaller and a lot faster. I also suspect that corner cases like ("%#.0o", 0) (where the '#' needs to add a leading zero to the empty string) aren't right. But that is different from this fix. David > --- > Changes in v4: > - Do clamp() first. > - Accept negative precision (this means no precision) . > - Change the warning message for width. > Changes in v3: > - Check and update width and precision before assigning to spec. > Changes in v2: > - Fix to use logical split. > --- > lib/vsprintf.c | 17 ++++++----------- > 1 file changed, 6 insertions(+), 11 deletions(-) > > diff --git a/lib/vsprintf.c b/lib/vsprintf.c > index 800b8ac49f53..5fa8f69030be 100644 > --- 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; > - > 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); > + WARN_ONCE(spec->precision < prec, "precision %d too large", prec); > } > > /* >