Linux-Hwmon Archive on lore.kernel.org
 help / color / Atom feed
From: Guenter Roeck <linux@roeck-us.net>
To: Lei YU <mine260309@gmail.com>
Cc: Jean Delvare <jdelvare@suse.com>,
	Eddie James <eajames@linux.ibm.com>,
	"Gustavo A . R . Silva" <gustavo@embeddedor.com>,
	linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] hwmon (occ): Fix division by zero issue
Date: Thu, 11 Jul 2019 09:35:48 -0700
Message-ID: <20190711163548.GA19747@roeck-us.net> (raw)
In-Reply-To: <1562813088-23708-1-git-send-email-mine260309@gmail.com>

On Thu, Jul 11, 2019 at 10:44:48AM +0800, Lei YU wrote:
> The code in occ_get_powr_avg() invokes div64_u64() without checking the
> divisor. In case the divisor is zero, kernel gets an "Division by zero
> in kernel" error.
> 
> Check the divisor and make it return 0 if the divisor is 0.
> 
> Signed-off-by: Lei YU <mine260309@gmail.com>
> Reviewed-by: Eddie James <eajames@linux.ibm.com>

Applied.

Thanks,
Guenter

> ---
>  drivers/hwmon/occ/common.c | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/hwmon/occ/common.c b/drivers/hwmon/occ/common.c
> index 13a6290..f02aa40 100644
> --- a/drivers/hwmon/occ/common.c
> +++ b/drivers/hwmon/occ/common.c
> @@ -402,8 +402,10 @@ static ssize_t occ_show_power_1(struct device *dev,
>  
>  static u64 occ_get_powr_avg(u64 *accum, u32 *samples)
>  {
> -	return div64_u64(get_unaligned_be64(accum) * 1000000ULL,
> -			 get_unaligned_be32(samples));
> +	u64 divisor = get_unaligned_be32(samples);
> +
> +	return (divisor == 0) ? 0 :
> +		div64_u64(get_unaligned_be64(accum) * 1000000ULL, divisor);
>  }
>  
>  static ssize_t occ_show_power_2(struct device *dev,

      reply index

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-07-11  2:44 Lei YU
2019-07-11 16:35 ` Guenter Roeck [this message]

Reply instructions:

You may reply publically to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190711163548.GA19747@roeck-us.net \
    --to=linux@roeck-us.net \
    --cc=eajames@linux.ibm.com \
    --cc=gustavo@embeddedor.com \
    --cc=jdelvare@suse.com \
    --cc=linux-hwmon@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mine260309@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Linux-Hwmon Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-hwmon/0 linux-hwmon/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-hwmon linux-hwmon/ https://lore.kernel.org/linux-hwmon \
		linux-hwmon@vger.kernel.org linux-hwmon@archiver.kernel.org
	public-inbox-index linux-hwmon


Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-hwmon


AGPL code for this site: git clone https://public-inbox.org/ public-inbox