Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp4377872pxb; Wed, 20 Apr 2022 01:44:55 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxGPcyCH+6Ku8SCJNhXyfVu/6WHeYlfNKXRtZ9b0Xpty29LrZWMeo8dgZlGnydeeaxwaOjh X-Received: by 2002:a17:907:161f:b0:6f0:653:d95e with SMTP id hb31-20020a170907161f00b006f00653d95emr1356482ejc.237.1650444295659; Wed, 20 Apr 2022 01:44:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650444295; cv=none; d=google.com; s=arc-20160816; b=XXrBscKEb8by7uHYT9wE3TCu1mq9SK4ALHXL9b9EeqP6VFBUJvePGy4HXEby9sVMRC 4w9rSrOI7HVCGj0YdZx6GDcbQuCM1y+Am3MVav9EuV0YHML2yc2MTt3dDv85Vu32Lsa7 cQF+zGH5YPpKoDFdyCGHiYGGF1Ovf5tCe/mvkqFB/wh0Pq6Hm+ehWrgKFViO7rkNswB2 GvkNxlAkzvRP584MYnGh5oMbH3qeHGqonbmWd3J5xeYZDX7grMXsXNQS/F/7jco/jIlg ZvRVdfy9B6eyG0C9K7myTRe+tapBi6eELYPJSlyPKH9W1LJWoxrzy/PVRpdxA+JJPmLw ryGg== 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=WHJy2f9+7fhq4x/zu24DmAICzGrvJ78WRPTLJ52pMLw=; b=1KtjoPp0AkCpG+39gBp7Rdi6oNDXziwcp/yZArKb27zGfWNhS8ORLn8h9m8ZIZ7Mxh VGcfR5s2osmnXFI0EQwuYB3nOC7+LOaPbGfCmzOlWKrPD085L6FmymId0Mxfb5/A/+p1 qWkeE+2e1p+kZ0ROi76B087mjrfS6X62t+BZaSCRG69uxE2SsPXQO9mBrguPEUUvRGZr cPEWGEe9dEy5r/oRgsr26F7FZ+AZVly04sA+6r3t6Y0LsVi5ElyF3Y7fj28M2LQINKVn uZSMfUs6SG2QmXWZFN6KGyUEjIbdJ64R8R9h0dPZzryCJDAotp7mZAfY+/Fg2OnB53kO NyzA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=VhQwA0Fz; 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 t21-20020a056402525500b0041d78a39a7asi1156038edd.443.2022.04.20.01.44.32; Wed, 20 Apr 2022 01:44:55 -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=VhQwA0Fz; 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 S1343693AbiDROIC (ORCPT + 99 others); Mon, 18 Apr 2022 10:08:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60510 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244448AbiDRNva (ORCPT ); Mon, 18 Apr 2022 09:51:30 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7F88345045; Mon, 18 Apr 2022 06:02:16 -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 ams.source.kernel.org (Postfix) with ESMTPS id CEF88B80E44; Mon, 18 Apr 2022 13:02:14 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2025EC385A1; Mon, 18 Apr 2022 13:02:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1650286933; bh=owh/FpiM4SspLRdCCDMt5NcNvWYcFXL5FfK3I7YiHak=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VhQwA0FzGFxu9XGJNJA/iyX//AruT5OTtCxXiFGEBqKyniH/uha8oIHUibrqlnoK6 owpucMjI1DO0RhdzGtvSt7AZxFTa504hSSPPy+pKhRvm9RloYFqTiOLn3JvrHrEhzX uOtOyj3EyUgAMcJYVHQFMb7SJZcxaLcKE3k5VJKE= 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 4.14 263/284] gpiolib: acpi: use correct format characters Date: Mon, 18 Apr 2022 14:14:04 +0200 Message-Id: <20220418121220.120412769@linuxfoundation.org> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20220418121210.689577360@linuxfoundation.org> References: <20220418121210.689577360@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 60e394da9709..713dc43024c9 100644 --- a/drivers/gpio/gpiolib-acpi.c +++ b/drivers/gpio/gpiolib-acpi.c @@ -335,8 +335,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