LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
From: Lee Jones <lee.jones@linaro.org>
To: Dan Carpenter <dan.carpenter@oracle.com>
Cc: linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org
Subject: Re: [PATCH v2] mfd: tps65911-comparator: Fix an off by one bug
Date: Mon, 23 Apr 2018 07:44:17 +0100 [thread overview]
Message-ID: <20180423064417.2e2ng3su23w4i7hx@dell> (raw)
In-Reply-To: <20180420090910.GA26071@mwanda>
On Fri, 20 Apr 2018, Dan Carpenter wrote:
> The COMP1 and COMP2 elements are in 0 and 1 respectively so this code is
> accessing the wrong elements and one space beyond the end of the array.
> We should be using "id - 1" instead.
>
> The "id" variable is never COMP (0) so that code can be removed.
>
> Fixes: 6851ad3ab346 ("TPS65911: Comparator: Add comparator driver")
> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> ---
> v2: we can fix the bug and save memory.
Looks like it's as we feared:
http://www.ti.com/lit/ds/symlink/tps65911.pdf (page 52)
There are only 2 comparators 1 and 2.
> diff --git a/drivers/mfd/tps65911-comparator.c b/drivers/mfd/tps65911-comparator.c
> index c0789f81a1c5..887409c3938d 100644
> --- a/drivers/mfd/tps65911-comparator.c
> +++ b/drivers/mfd/tps65911-comparator.c
> @@ -22,7 +22,6 @@
> #include <linux/gpio.h>
> #include <linux/mfd/tps65910.h>
>
> -#define COMP 0
> #define COMP1 1
> #define COMP2 2
Sorry, but I think these defines should describe the indexes into the
supplied struct directly, especially as in this case it is their only
reason for being.
I completely agree with you that defining something that is named '1'
as '0' is not ideal, but IMHO it's better than jumping through hoops
using arithmetic in array indexes.
> @@ -58,14 +57,11 @@ static struct comparator tps_comparators[] = {
>
> static int comp_threshold_set(struct tps65910 *tps65910, int id, int voltage)
> {
> - struct comparator tps_comp = tps_comparators[id];
> + struct comparator tps_comp = tps_comparators[id - 1];
> int curr_voltage = 0;
> int ret;
> u8 index = 0, val;
>
> - if (id == COMP)
> - return 0;
> -
> while (curr_voltage < tps_comp.uV_max) {
> curr_voltage = tps_comp.vsel_table[index];
> if (curr_voltage >= voltage)
> @@ -85,13 +81,10 @@ static int comp_threshold_set(struct tps65910 *tps65910, int id, int voltage)
>
> static int comp_threshold_get(struct tps65910 *tps65910, int id)
> {
> - struct comparator tps_comp = tps_comparators[id];
> + struct comparator tps_comp = tps_comparators[id - 1];
> int ret;
> u8 val;
>
> - if (id == COMP)
> - return 0;
> -
> ret = tps65910->read(tps65910, tps_comp.reg, 1, &val);
> if (ret < 0)
> return ret;
--
Lee Jones [李琼斯]
Linaro Services Technical Lead
Linaro.org │ Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog
next prev parent reply other threads:[~2018-04-23 6:44 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-04-19 13:46 [PATCH] " Dan Carpenter
2018-04-20 8:09 ` Lee Jones
2018-04-20 8:21 ` Dan Carpenter
2018-04-20 8:24 ` Dan Carpenter
2018-04-20 8:39 ` Lee Jones
2018-04-20 9:01 ` Dan Carpenter
2018-04-23 6:26 ` Lee Jones
2018-04-20 9:09 ` [PATCH v2] " Dan Carpenter
2018-04-23 6:44 ` Lee Jones [this message]
2018-04-23 10:27 ` Dan Carpenter
2018-04-24 5:45 ` Lee Jones
2018-04-20 8:59 ` [PATCH] " Dan Carpenter
2018-04-20 9:21 ` [PATCH] mfd: tps65911-comparator: Fix a build error Dan Carpenter
2018-04-23 18:13 ` Rhyland Klein
2018-04-24 5:46 ` Lee Jones
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=20180423064417.2e2ng3su23w4i7hx@dell \
--to=lee.jones@linaro.org \
--cc=dan.carpenter@oracle.com \
--cc=kernel-janitors@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--subject='Re: [PATCH v2] mfd: tps65911-comparator: Fix an off by one bug' \
/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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).