diff mbox

gpio: Prevent an integer overflow in the pca953x driver

Message ID 1432254912-17153-1-git-send-email-joshua.scott@alliedtelesis.co.nz
State New
Headers show

Commit Message

Joshua Scott May 22, 2015, 12:35 a.m. UTC
Interrupts were missed if an 8-bit integer overflow occurred. This was
observed when bank0,pin7 and bank1,pin7 changed simultaniously.

As the 8-bit totals were only checked against zero, replace them with
booleans. Name the booleans so that their purpose is clear.

Signed-off-by: Joshua Scott <joshua.scott@alliedtelesis.co.nz>
---
 drivers/gpio/gpio-pca953x.c | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)

Comments

Alexandre Courbot May 23, 2015, 6:22 a.m. UTC | #1
On Fri, May 22, 2015 at 9:35 AM, Joshua Scott
<joshua.scott@alliedtelesis.co.nz> wrote:
> Interrupts were missed if an 8-bit integer overflow occurred. This was
> observed when bank0,pin7 and bank1,pin7 changed simultaniously.
>
> As the 8-bit totals were only checked against zero, replace them with
> booleans. Name the booleans so that their purpose is clear.
>
> Signed-off-by: Joshua Scott <joshua.scott@alliedtelesis.co.nz>

Looks good, I wonder why += was used instead of |= ? Or why a bool was
not used in the first place since per-pin information is never used
anyway.

Reviewed-by: Alexandre Courbot <acourbot@nvidia.com>
--
To unsubscribe from this list: send the line "unsubscribe linux-gpio" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Linus Walleij June 1, 2015, 1:52 p.m. UTC | #2
On Fri, May 22, 2015 at 2:35 AM, Joshua Scott
<joshua.scott@alliedtelesis.co.nz> wrote:

> Interrupts were missed if an 8-bit integer overflow occurred. This was
> observed when bank0,pin7 and bank1,pin7 changed simultaniously.
>
> As the 8-bit totals were only checked against zero, replace them with
> booleans. Name the booleans so that their purpose is clear.
>
> Signed-off-by: Joshua Scott <joshua.scott@alliedtelesis.co.nz>

Patch applied with Alex' review tag.

Yours,
Linus Walleij
--
To unsubscribe from this list: send the line "unsubscribe linux-gpio" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/drivers/gpio/gpio-pca953x.c b/drivers/gpio/gpio-pca953x.c
index e2da64a..5059db5 100644
--- a/drivers/gpio/gpio-pca953x.c
+++ b/drivers/gpio/gpio-pca953x.c
@@ -443,12 +443,13 @@  static struct irq_chip pca953x_irq_chip = {
 	.irq_set_type		= pca953x_irq_set_type,
 };
 
-static u8 pca953x_irq_pending(struct pca953x_chip *chip, u8 *pending)
+static bool pca953x_irq_pending(struct pca953x_chip *chip, u8 *pending)
 {
 	u8 cur_stat[MAX_BANK];
 	u8 old_stat[MAX_BANK];
-	u8 pendings = 0;
-	u8 trigger[MAX_BANK], triggers = 0;
+	bool pending_seen = false;
+	bool trigger_seen = false;
+	u8 trigger[MAX_BANK];
 	int ret, i, offset = 0;
 
 	switch (chip->chip_type) {
@@ -461,7 +462,7 @@  static u8 pca953x_irq_pending(struct pca953x_chip *chip, u8 *pending)
 	}
 	ret = pca953x_read_regs(chip, offset, cur_stat);
 	if (ret)
-		return 0;
+		return false;
 
 	/* Remove output pins from the equation */
 	for (i = 0; i < NBANK(chip); i++)
@@ -471,11 +472,12 @@  static u8 pca953x_irq_pending(struct pca953x_chip *chip, u8 *pending)
 
 	for (i = 0; i < NBANK(chip); i++) {
 		trigger[i] = (cur_stat[i] ^ old_stat[i]) & chip->irq_mask[i];
-		triggers += trigger[i];
+		if (trigger[i])
+			trigger_seen = true;
 	}
 
-	if (!triggers)
-		return 0;
+	if (!trigger_seen)
+		return false;
 
 	memcpy(chip->irq_stat, cur_stat, NBANK(chip));
 
@@ -483,10 +485,11 @@  static u8 pca953x_irq_pending(struct pca953x_chip *chip, u8 *pending)
 		pending[i] = (old_stat[i] & chip->irq_trig_fall[i]) |
 			(cur_stat[i] & chip->irq_trig_raise[i]);
 		pending[i] &= trigger[i];
-		pendings += pending[i];
+		if (pending[i])
+			pending_seen = true;
 	}
 
-	return pendings;
+	return pending_seen;
 }
 
 static irqreturn_t pca953x_irq_handler(int irq, void *devid)