Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp7409927rwr; Wed, 10 May 2023 07:57:51 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5SkBzlrDNXAreswSRDNt/TcaoA0r89Ztx8z6x8XNr5RNorcRnzdrVXXwALkF6pCnfwCRrr X-Received: by 2002:a17:90a:8d16:b0:250:faff:e201 with SMTP id c22-20020a17090a8d1600b00250faffe201mr2053612pjo.36.1683730671182; Wed, 10 May 2023 07:57:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683730671; cv=none; d=google.com; s=arc-20160816; b=CbeeChDdErgdAMHnxnrKE4/OBJkVv8n/LrslIINl/X6VbP3I6QVADqfgJkbw0k18o4 O3opLMfSuOVoROsKUbWB8KFSlZdmFflzvP817Ct3aeLudMoCvRaQVYvNyww5RCahwKv7 nAa4dHeVZk29n/phhwM+63aFbqaoibwTtyB0VULCq1GyDUJaBg6hMRTP6unsBlQq1xK+ 5NrWIeppYXDjurCBwKKOz3t3SwNB1d6l03b+F5tpENRhB9a8tvldGSAzZ2iNI/3FrMje mBmO0+FI8S7M1zIgKD93xBsfxmdEZPI34eaKGk062lpTIZnzgaD/9d7i9WetPX6t/ohm iF2A== 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 :message-id:date:subject:cc:to:from; bh=4mryp54DyEbb0RPmv+DFH+ShxeMpbBTmXxA/ginSD7M=; b=wzxzEr1/FrzMgBp0v1eGZD4BkNmeq8fz97GXPY/Kv8lM0K5WyP6RCUyDb++DR2+O98 D6CBL1uzUhBqBMdk6R27EKmNeP9EhIHUnW9Y7+ty4FWQZuhTtSyihwPA9IDNfxA0HsSR JIz1IVt8pBAVYLnH4geoH19b6F35StYEy0MLr1UJvW1nGhpIEVmuDCaubP46JMB1WtFK Zbd82djntv8Mjz7B7IEHdbK6BaNhE68c8xqeT11XNCDWgBt7KF6ehddDGv9ItFnxED8L jkXtk13IUNnklteWPHt89rrgV1bZeRIvK7rWsRVYEUmfi/rykKDJ7GtABYxWSP1V14Zk exSA== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id rj14-20020a17090b3e8e00b002500b162449si15609299pjb.57.2023.05.10.07.57.35; Wed, 10 May 2023 07:57:51 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237049AbjEJOgA (ORCPT + 99 others); Wed, 10 May 2023 10:36:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40034 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237479AbjEJOf4 (ORCPT ); Wed, 10 May 2023 10:35:56 -0400 Received: from exchange.fintech.ru (e10edge.fintech.ru [195.54.195.159]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0860786A2; Wed, 10 May 2023 07:35:44 -0700 (PDT) Received: from Ex16-01.fintech.ru (10.0.10.18) by exchange.fintech.ru (195.54.195.159) with Microsoft SMTP Server (TLS) id 14.3.498.0; Wed, 10 May 2023 17:35:41 +0300 Received: from localhost (10.0.253.138) by Ex16-01.fintech.ru (10.0.10.18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2242.4; Wed, 10 May 2023 17:35:41 +0300 From: Nikita Zhandarovich To: Jean Delvare CC: Nikita Zhandarovich , Guenter Roeck , =?UTF-8?q?Uwe=20Kleine-K=C3=B6nig?= , , , Subject: [PATCH] hwmon: (f71882fg) prevent possible division by zero Date: Wed, 10 May 2023 07:35:37 -0700 Message-ID: <20230510143537.145060-1-n.zhandarovich@fintech.ru> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.0.253.138] X-ClientProxiedBy: Ex16-02.fintech.ru (10.0.10.19) To Ex16-01.fintech.ru (10.0.10.18) X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,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 In the unlikely event that something goes wrong with the device and its registers, the fan_from_reg() function may return 0. This value will cause a division-by-zero error in the show_pwm() function. To prevent this, test the value of fan_from_reg(data->fan_full_speed[nr]) against 0 before performing the division. If the division-by-zero error is avoided, assign 0 to the val variable. Found by Linux Verification Center (linuxtesting.org) with static analysis tool SVACE. Fixes: df9ec2dae094 ("hwmon: (f71882fg) Reorder symbols to get rid of a few forward declarations") Signed-off-by: Nikita Zhandarovich --- drivers/hwmon/f71882fg.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/drivers/hwmon/f71882fg.c b/drivers/hwmon/f71882fg.c index 70121482a617..27207ec6f7fe 100644 --- a/drivers/hwmon/f71882fg.c +++ b/drivers/hwmon/f71882fg.c @@ -1096,8 +1096,11 @@ static ssize_t show_pwm(struct device *dev, val = data->pwm[nr]; else { /* RPM mode */ - val = 255 * fan_from_reg(data->fan_target[nr]) - / fan_from_reg(data->fan_full_speed[nr]); + if (fan_from_reg(data->fan_full_speed[nr])) + val = 255 * fan_from_reg(data->fan_target[nr]) + / fan_from_reg(data->fan_full_speed[nr]); + else + val = 0; } mutex_unlock(&data->update_lock); return sprintf(buf, "%d\n", val); -- 2.25.1