All of lore.kernel.org
 help / color / mirror / Atom feed
From: tq17373059@buaa.edu.cn
To: lars@metafoo.de, Michael.Hennerich@analog.com, jic23@kernel.org
Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org,
	baijiaju@tsinghua.edu.cn, Qi Teng <tq17373059@buaa.edu.cn>,
	TOTE Robot <oslab@tsinghua.edu.cn>
Subject: [PATCH] iio: adf4350: fix a possible divided-by-zero bug in adf4350_set_freq()
Date: Fri, 21 May 2021 14:19:53 +0800	[thread overview]
Message-ID: <20210521061953.35873-1-tq17373059@buaa.edu.cn> (raw)

From: Qi Teng <tq17373059@buaa.edu.cn>

The variable st->r1_mod is checked in:
  if (st->r0_fract && st->r1_mod)

This indicates that st->r1_mod can be zero. Its value is the same as
that in:
  st->r0_fract = do_div(tmp, st->r1_mod);

However, st->r1_mod performs as a divisor in this statement, which
implies a possible divided-by-zero bug.

To fix this possible bug, st->r1_mod is checked before the division
operation. If it is zero, st->r0_fract is set to zero instead of
do_div(tmp, st->r1_mod).

Reported-by: TOTE Robot <oslab@tsinghua.edu.cn> 
Signed-off-by: Qi Teng <tq17373059@buaa.edu.cn>
---
 drivers/iio/frequency/adf4350.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/drivers/iio/frequency/adf4350.c b/drivers/iio/frequency/adf4350.c
index 99c6f260cc21..1462a6a5bc6d 100644
--- a/drivers/iio/frequency/adf4350.c
+++ b/drivers/iio/frequency/adf4350.c
@@ -182,10 +182,7 @@ static int adf4350_set_freq(struct adf4350_state *st, unsigned long long freq)
 
 		tmp = freq * (u64)st->r1_mod + (st->fpfd >> 1);
 		do_div(tmp, st->fpfd); /* Div round closest (n + d/2)/d */
-		if (st->r1_mod)
-			st->r0_fract = do_div(tmp, st->r1_mod);
-		else
-			st->r0_fract = 0;
+		st->r0_fract = do_div(tmp, st->r1_mod);
 		st->r0_int = tmp;
 	} while (mdiv > st->r0_int);
 
-- 
2.25.1


             reply	other threads:[~2021-05-21  6:26 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-21  6:19 tq17373059 [this message]
2021-05-21 17:25 ` [PATCH] iio: adf4350: fix a possible divided-by-zero bug in adf4350_set_freq() Jonathan Cameron

Reply instructions:

You may reply publicly 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=20210521061953.35873-1-tq17373059@buaa.edu.cn \
    --to=tq17373059@buaa.edu.cn \
    --cc=Michael.Hennerich@analog.com \
    --cc=baijiaju@tsinghua.edu.cn \
    --cc=jic23@kernel.org \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=oslab@tsinghua.edu.cn \
    /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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.