Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp3340025pxb; Mon, 18 Apr 2022 23:33:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxCdEj4d9cth81x56aB0Ot7djbQ2mi5d/r8zK4pCyDgoa9Gx/zhrSVWzf7P9xsQh/ZADko0 X-Received: by 2002:a05:6a00:124f:b0:50a:72ed:c924 with SMTP id u15-20020a056a00124f00b0050a72edc924mr9823744pfi.31.1650350036963; Mon, 18 Apr 2022 23:33:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650350036; cv=none; d=google.com; s=arc-20160816; b=r7TNerb8ulchyV4rHqYPbsvk1i3Y6UIcUlhAEfXE+SP47b3q0GXb6hxK5e6RfNWJWD MSugoGDdk5Y3O+CJojVfhI8daWYZZp2GDm7NXdSkf7GG/AH4+8EuVenMiJRWolAKIGWq EyXrcTjRrPVgxkIysfaKIAv05TFMv7WJS+pKiHFK1mbAB20r1o2nuPZT5h9/kjAKCy2N /7kI60HVXGNQmUyCaS782cWvklOzX68zKUYxQMHK9VZFScSgcUlOAQkUEsCaOOSizuW2 YS77pss8KHgibkReavQUI4K0LdFKUsQbgw83tHVv9ahbMe0Ncm0SPptyuwTGx6KYzUlj RgFA== 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=QapuoL7TzPvj74eNBOxcjV/JGPSVBfyn2q1jEzpn/B4=; b=gKID2oYDTGoxt2mYY3n/vx7pe+bf9Byvs7ZhEscdEFkLikUiWgG4G6mtV/VfvHi0EL bWCIdNFjw3z9V1Cjrq8mypp7wvOvi8rTA9t/D/dz8cC8J+Z0Pdc8Qd8n5d9KiqzOrMY0 drY0WGvPgBbtzeZ+GsZMrjX4J0QDmY6KtiHdkuGAslcfGZlpDgxE84YBAIMcUmaJV3a3 VDZNzih6jPcrNG7hW+29qLSRvO8OMzgxdNpYCvL2DJjQjObTWI8B1LxPXzj1/7tvyEsZ UJtJrkGj2guQzp41cNb1+66mCFyI5zn/YLueK/m2KDKoDcMyftecm+NqdgaNC7mbkr/0 lSgA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=OGC9Mfqn; 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 d17-20020a170903209100b00158d44e6e27si9515104plc.142.2022.04.18.23.33.40; Mon, 18 Apr 2022 23:33:56 -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=OGC9Mfqn; 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 S241305AbiDRNHf (ORCPT + 99 others); Mon, 18 Apr 2022 09:07:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51104 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240289AbiDRMzf (ORCPT ); Mon, 18 Apr 2022 08:55:35 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 944A5C08; Mon, 18 Apr 2022 05:37:05 -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 29D8DB80EDD; Mon, 18 Apr 2022 12:37:04 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 63AF3C385A7; Mon, 18 Apr 2022 12:37:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1650285422; bh=ayTuUEaLywRE6bf1Sw9c4Yv0rsafKHY50S9my7OaabA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OGC9Mfqnk0dsSY/a2jHzsBJ95Aqz5G+mgffyaiA5iJDy3P7ho4D/nkirQ9Qo/koct c5CQoKYukPjn22XtQO+ubLuBDz67j/liebVszaJEhND2CX3WGYQsVh2A5JVTRdvE5/ lVFuAEwK9WsMJjqWnh8es9XS0WMK8tgUAzt14rSM= 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.10 014/105] gpiolib: acpi: use correct format characters Date: Mon, 18 Apr 2022 14:12:16 +0200 Message-Id: <20220418121146.252530654@linuxfoundation.org> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20220418121145.140991388@linuxfoundation.org> References: <20220418121145.140991388@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 55e4f402ec8b..44ee319da1b3 100644 --- a/drivers/gpio/gpiolib-acpi.c +++ b/drivers/gpio/gpiolib-acpi.c @@ -276,8 +276,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