Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp3218455pxb; Mon, 18 Apr 2022 19:34:21 -0700 (PDT) X-Google-Smtp-Source: ABdhPJys6gdSGirXyJCG3nUbDx68PcwjXdG+S7jEXlTiJTpaefxQvycJNJfv4ncVKvRufkcG+ch+ X-Received: by 2002:a17:90b:1bd0:b0:1d2:a488:7aee with SMTP id oa16-20020a17090b1bd000b001d2a4887aeemr7213187pjb.31.1650335660881; Mon, 18 Apr 2022 19:34:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650335660; cv=none; d=google.com; s=arc-20160816; b=sChfMpN4QkQpAVrL4Hzf4zZ4QXJi0MENHR7lri7WdeqEt4I+gL9tZIzeUbATHnFcO9 hIbBKeHnQdWp8B8EWYxSEbnGljrMKfh4y5EH8O6GOndkUJ74SRLuDlJ6299Ngrzgtin6 sdD3Kx76P0pJH8hU3eefh0bgfQRwdjGnPmflrYIR2I2Ty6lceVKTgiajHNWW0Bng4oY8 g9gezwCN3TVAHsT2e5YsvkgwuKlT14YLthI9wqLSM+uzD4c9z8Ghlx9VWy5ydjmd3o+8 6HI5XWwy8m8ZFavIev0mPaSo4DQsVZVA40x8MCguTih91cQRofDm21F4Aes4/P617Yip 2iXQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=f4b1Z7vh+BL/y3dBKTpx/3yhX8F0Qjp7ZX1W7aNtH4w=; b=UbsDykc0sLM4D130GrqQRFmm+M9tSORtYUsYRouF35YQ9dp9r6mdaTD/4GT4WESYEQ whPbEj3mFm0swCTkfMGYNuqP6J2QnGl8zIanDahPtS5hcnxFBU1M1bMwv5sEcA8toCOd QyMPIAqfX2TcPujJB/YTUvv4rKGCkYBesz4iHcIKlpldScspcG1y/oDotvb5c5E5Y6ry bSvJU7gk2wANhiCuh0gPrbhubqdUscqbIeDpDzRhAopBF6plXmbteOx6nyQQnXJvT4si FCHwJ/ODO0lJ0PjAuYGlXqtc7v4BClEpT/pn0eeowIaV4qgV8nGMzYzvyCcsPYhF8PVK GXIA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=zMqXQrsM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f201-20020a6238d2000000b00505a42ba062si9862420pfa.287.2022.04.18.19.34.04; Mon, 18 Apr 2022 19:34:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=zMqXQrsM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238543AbiDRMZF (ORCPT + 99 others); Mon, 18 Apr 2022 08:25:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49258 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238927AbiDRMXN (ORCPT ); Mon, 18 Apr 2022 08:23:13 -0400 Received: from sin.source.kernel.org (sin.source.kernel.org [IPv6:2604:1380:40e1:4800::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EC8C71CFE1; Mon, 18 Apr 2022 05:18:32 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sin.source.kernel.org (Postfix) with ESMTPS id 2610CCE1077; Mon, 18 Apr 2022 12:18:31 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 04677C385A1; Mon, 18 Apr 2022 12:18:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1650284309; bh=tRJCLhZrLSVmS+owP3osLKVUwqsLjwoB+gXJgquW6hc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=zMqXQrsMWml3RTdjMZxIvYEm2OfM11NPi1y/E7N7/RVRgCYAy6oU4bD/x5aCg7jzF oFP7SvuiKUpewo++bRxj3S7UUQI8VBOaCxo0Y2rQ8wXb1P/Bgk5s47qV86o98OLDYd 18NrbKB862mlx+KrLm9FudQA0Rzb4AXZRrxhT37Y= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Linus Torvalds , Andy Shevchenko , Sasha Levin Subject: [PATCH 5.17 070/219] gpiolib: acpi: use correct format characters Date: Mon, 18 Apr 2022 14:10:39 +0200 Message-Id: <20220418121207.773707341@linuxfoundation.org> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20220418121203.462784814@linuxfoundation.org> References: <20220418121203.462784814@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Linus Torvalds [ Upstream commit 213d266ebfb1621aab79cfe63388facc520a1381 ] When compiling with -Wformat, clang emits the following warning: gpiolib-acpi.c:393:4: warning: format specifies type 'unsigned char' but the argument has type 'int' [-Wformat] pin); ^~~ So warning that '%hhX' is paired with an 'int' is all just completely mindless and wrong. Sadly, I can see a different bogus warning reason why people would want to use '%02hhX'. Again, the *sane* thing from a human perspective is to use '%02X. But if the compiler doesn't do any range analysis at all, it could decide that "Oh, that print format could need up to 8 bytes of space in the result". Using '%02hhX' would cut that down to two. And since we use char ev_name[5]; and currently use "_%c%02hhX" as the format string, even a compiler that doesn't notice that "pin <= 255" test that guards this all will go "OK, that's at most 4 bytes and the final NUL termination, so it's fine". While a compiler - like gcc - that only sees that the original source of the 'pin' value is a 'unsigned short' array, and then doesn't take the "pin <= 255" into account, will warn like this: gpiolib-acpi.c: In function 'acpi_gpiochip_request_interrupt': gpiolib-acpi.c:206:24: warning: '%02X' directive writing between 2 and 4 bytes into a region of size 3 [-Wformat-overflow=] sprintf(ev_name, "_%c%02X", ^~~~ gpiolib-acpi.c:206:20: note: directive argument in the range [0, 65535] because gcc isn't being very good at that argument range analysis either. In other words, the original use of 'hhx' was bogus to begin with, and due to *another* compiler warning being bad, and we had that bad code being written back in 2016 to work around _that_ compiler warning (commit e40a3ae1f794: "gpio: acpi: work around false-positive -Wstring-overflow warning"). Sadly, two different bad compiler warnings together does not make for one good one. It just makes for even more pain. End result: I think the simplest and cleanest option is simply the proposed change which undoes that '%hhX' change for gcc, and replaces it with just using a slightly bigger stack allocation. It's not like a 5-byte allocation is in any way likely to have saved any actual stack, since all the other variables in that function are 'int' or bigger. False-positive compiler warnings really do make people write worse code, and that's a problem. But on a scale of bad code, I feel that extending the buffer trivially is better than adding a pointless cast that literally makes no sense. At least in this case the end result isn't unreadable or buggy. We've had several cases of bad compiler warnings that caused changes that were actually horrendously wrong. Fixes: e40a3ae1f794 ("gpio: acpi: work around false-positive -Wstring-overflow warning") Signed-off-by: Linus Torvalds Signed-off-by: Andy Shevchenko Signed-off-by: Sasha Levin --- drivers/gpio/gpiolib-acpi.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/gpio/gpiolib-acpi.c b/drivers/gpio/gpiolib-acpi.c index a5495ad31c9c..b7c2f2af1dee 100644 --- a/drivers/gpio/gpiolib-acpi.c +++ b/drivers/gpio/gpiolib-acpi.c @@ -387,8 +387,8 @@ static acpi_status acpi_gpiochip_alloc_event(struct acpi_resource *ares, pin = agpio->pin_table[0]; if (pin <= 255) { - char ev_name[5]; - sprintf(ev_name, "_%c%02hhX", + char ev_name[8]; + sprintf(ev_name, "_%c%02X", agpio->triggering == ACPI_EDGE_SENSITIVE ? 'E' : 'L', pin); if (ACPI_SUCCESS(acpi_get_handle(handle, ev_name, &evt_handle))) -- 2.35.1