From patchwork Mon Dec 5 04:55:32 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guenter Roeck X-Patchwork-Id: 9460327 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 3AE446022E for ; Mon, 5 Dec 2016 06:04:13 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3210B24B5B for ; Mon, 5 Dec 2016 06:04:13 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2736F2621D; Mon, 5 Dec 2016 06:04:13 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D747D24B5B for ; Mon, 5 Dec 2016 06:04:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751134AbcLEGEM (ORCPT ); Mon, 5 Dec 2016 01:04:12 -0500 Received: from bh-25.webhostbox.net ([208.91.199.152]:51223 "EHLO bh-25.webhostbox.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751039AbcLEGEM (ORCPT ); Mon, 5 Dec 2016 01:04:12 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=roeck-us.net; s=default; h=References:In-Reply-To:Message-Id:Date:Subject: Cc:To:From; bh=G9dWinboMADmP2ZVsrRKYwg4tCnE4MM3NiNrNM6HIh0=; b=dgbpXKPQjclsfK GrzB9hlUMxql74ytKBGj/ijVkqUfPYrn9El4fvKBIxXvTpJhCkosdSQ1hWRy0qFsIfIR8vJqkosL2 lSZZk7Eq0ceAIgaCW/PcmMPvqooX5MLjqoKd/P2OPUax8VSQfVPu9ioVL7Ri/w1Ue9VdMiXAsFUgr dVvA/0yXpGGpTF6wutUo7mQJpYN3bQT0BNL4R9XqnQqvZ8lMCobzlJBWxuqb7Vs71QLiQ4FWWF2aE eKMNb3F7OokPtL3JMG3dcSYnIQd60K64HACcoymIiv5h2paRPvru9bSskPgVpYrw+x0KtUK1eu208 VsoFQj7F2E/EQWfMqHKw==; Received: from 108-223-40-66.lightspeed.sntcca.sbcglobal.net ([108.223.40.66]:49768 helo=localhost) by bh-25.webhostbox.net with esmtpa (Exim 4.86_1) (envelope-from ) id 1cDlJf-0009Fj-4q; Mon, 05 Dec 2016 04:55:51 +0000 From: Guenter Roeck To: Hardware Monitoring Cc: Jean Delvare , Guenter Roeck Subject: [PATCH 09/17] hwmon: (nct7802) Fix overflows seen when writing into limit attributes Date: Sun, 4 Dec 2016 20:55:32 -0800 Message-Id: <1480913740-5678-9-git-send-email-linux@roeck-us.net> X-Mailer: git-send-email 2.5.0 In-Reply-To: <1480913740-5678-1-git-send-email-linux@roeck-us.net> References: <1480913740-5678-1-git-send-email-linux@roeck-us.net> X-Authenticated_sender: guenter@roeck-us.net X-OutGoing-Spam-Status: No, score=-1.0 X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - bh-25.webhostbox.net X-AntiAbuse: Original Domain - vger.kernel.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - roeck-us.net X-Get-Message-Sender-Via: bh-25.webhostbox.net: authenticated_id: guenter@roeck-us.net X-Authenticated-Sender: bh-25.webhostbox.net: guenter@roeck-us.net X-Source: X-Source-Args: X-Source-Dir: Sender: linux-hwmon-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-hwmon@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Fix overflows seen when writing voltage and temperature limit attributes. The value passed to DIV_ROUND_CLOSEST() needs to be clamped. Signed-off-by: Guenter Roeck --- drivers/hwmon/nct7802.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/drivers/hwmon/nct7802.c b/drivers/hwmon/nct7802.c index 3ce33d244cc0..6fe71cfe0320 100644 --- a/drivers/hwmon/nct7802.c +++ b/drivers/hwmon/nct7802.c @@ -326,8 +326,9 @@ static int nct7802_write_voltage(struct nct7802_data *data, int nr, int index, int shift = 8 - REG_VOLTAGE_LIMIT_MSB_SHIFT[index - 1][nr]; int err; - voltage = DIV_ROUND_CLOSEST(voltage, nct7802_vmul[nr]); - voltage = clamp_val(voltage, 0, 0x3ff); + voltage = DIV_ROUND_CLOSEST(clamp_val(voltage, 0, + 0x3ff * nct7802_vmul[nr]), + nct7802_vmul[nr]); mutex_lock(&data->access_lock); err = regmap_write(data->regmap, @@ -402,7 +403,7 @@ static ssize_t store_temp(struct device *dev, struct device_attribute *attr, if (err < 0) return err; - val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), -128, 127); + val = DIV_ROUND_CLOSEST(clamp_val(val, -127000, 127000), 1000); err = regmap_write(data->regmap, nr, val & 0xff); return err ? : count;