Skip to contentSkip to conversation

gpio: Fix test on unsigned in lnw_irq_type()

PATCH In review Roel Kluin 3 participants 4 messages Oct 12, 2009 – Oct 15, 2009 5 messages not in the archive GPIO

lore
Share & tools
lore

Roel Kluin

author
#0 lore raw

PATCH 1/1 gpio: Fix test on unsigned in lnw_irq_type()

The wrong test was used, gpio is unsigned.

---

drivers/gpio/langwell_gpio.c +4 -1
index 5711ce5..0d0cbc0 100644
@@ -123,8 +123,11 @@ static int lnw_irq_type(unsigned irq, unsigned type)
 	void __iomem *grer = (void __iomem *)(&lnw->reg_base->GRER[reg]);
 	void __iomem *gfer = (void __iomem *)(&lnw->reg_base->GFER[reg]);
 
-	if (gpio < 0 || gpio > lnw->chip.ngpio)
+	if (irq < lnw->irq_base || gpio > lnw->chip.ngpio ||
+			reg >= ARRAY_SIZE(lnw->reg_base->GRER)
+			reg >= ARRAY_SIZE(lnw->reg_base->GFER))
 		return -EINVAL;
+
 	spin_lock_irqsave(&lnw->lock, flags);
 	if (type & IRQ_TYPE_EDGE_RISING)
 		value = readl(grer) | BIT(gpio % 32);
#1 lore raw
Quoted 11 lines · On Mon, 12 Oct 2009 16:12:40 +0200 Roel Kluin <[email protected]> wrote:

The wrong test was used, gpio is unsigned.

---

drivers/gpio/langwell_gpio.c +0 -0
index 5711ce5..0d0cbc0 100644
@@ -123,8 +123,11 @@ static int lnw_irq_type(unsigned irq, unsigned type)
 	void __iomem *grer = (void __iomem *)(&lnw->reg_base->GRER[reg]);
 	void __iomem *gfer = (void __iomem *)(&lnw->reg_base->GFER[reg]);

drivers/gpio/langwell_gpio.c static int lnw_irq_type(unsigned irq, unsigned type) · line 133

Quoted 10 lines
drivers/gpio/langwell_gpio.c +4 -1
 
-	if (gpio < 0 || gpio > lnw->chip.ngpio)
+	if (irq < lnw->irq_base || gpio > lnw->chip.ngpio ||
+			reg >= ARRAY_SIZE(lnw->reg_base->GRER)
+			reg >= ARRAY_SIZE(lnw->reg_base->GFER))
 		return -EINVAL;
+
 	spin_lock_irqsave(&lnw->lock, flags);
 	if (type & IRQ_TYPE_EDGE_RISING)
 		value = readl(grer) | BIT(gpio % 32);

Makes the code unfortunately complex. It'd be better to make `gpio' a signed quantity, or even..

drivers/gpio/langwell_gpio.c renamed from drivers/gpio/langwell_gpio.c~gpio-fix-test-on-unsigned-in-lnw_irq_type +1 -1
@@ -123,7 +123,7 @@ static int lnw_irq_type(unsigned irq, un
 	void __iomem *grer = (void __iomem *)(&lnw->reg_base->GRER[reg]);
 	void __iomem *gfer = (void __iomem *)(&lnw->reg_base->GFER[reg]);
 
-	if (gpio < 0 || gpio > lnw->chip.ngpio)
+	if ((s32)gpio < 0 || gpio > lnw->chip.ngpio)
 		return -EINVAL;
 	spin_lock_irqsave(&lnw->lock, flags);
 	if (type & IRQ_TYPE_EDGE_RISING)

_

author
#7 lore raw
In reply to a message not in the archive

The wrong test was used, gpio is unsigned. Also lnw->chip.ngpio is set to 64, so if gpio equals that, then reg (= gpio / 32) becomes 2, an index out of bounds for GRER and GFER that have 2 elements.

---

Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

From: Andrew Morton [mailto:[email protected]]

Review

Quoted 3 lines
(quoted diff) +1 -1
-    if (gpio < 0 || gpio > lnw->chip.ngpio)
+    if (gpio > lnw->chip.ngpio)
             return -EINVAL;

Should that be >= ?

Oh, my bad. Andrew is right, should be >=...

Ok, how about this then?

drivers/gpio/langwell_gpio.c +1 -1
index 5711ce5..72af3fc 100644
@@ -123,7 +123,7 @@ static int lnw_irq_type(unsigned irq, unsigned type)
 	void __iomem *grer = (void __iomem *)(&lnw->reg_base->GRER[reg]);
 	void __iomem *gfer = (void __iomem *)(&lnw->reg_base->GFER[reg]);
 
-	if (gpio < 0 || gpio > lnw->chip.ngpio)
+	if (gpio >= lnw->chip.ngpio)
 		return -EINVAL;
 	spin_lock_irqsave(&lnw->lock, flags);
 	if (type & IRQ_TYPE_EDGE_RISING)
Acked-by
#8 lore raw
Quoted 30 lines

-----Original Message-----
From: Roel Kluin [mailto:[email protected]]
Sent: Thursday, October 15, 2009 1:17 AM
To: Du, Alek; Andrew Morton; LKML
Subject: Re: [PATCH] gpio: Fix test on unsigned in lnw_irq_type()

The wrong test was used, gpio is unsigned. Also lnw->chip.ngpio is set to 64, so if gpio equals that, then reg (= gpio / 32) becomes 2, an index out of bounds for GRER and GFER that have 2 elements.

---

Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

From: Andrew Morton [mailto:[email protected]]

drivers/gpio/langwell_gpio.c static int lnw_irq_type(unsigned irq, unsigned type) · line 127

Quoted 3 lines
drivers/gpio/langwell_gpio.c +1 -1
-    if (gpio < 0 || gpio > lnw->chip.ngpio)
+    if (gpio > lnw->chip.ngpio)
             return -EINVAL;

Should that be >= ?

Oh, my bad. Andrew is right, should be >=...

Ok, how about this then?

drivers/gpio/langwell_gpio.c +0 -0
index 5711ce5..72af3fc 100644

drivers/gpio/langwell_gpio.c static int lnw_irq_type(unsigned irq, unsigned type) · line 129

Quoted 8 lines
drivers/gpio/langwell_gpio.c +1 -1
@@ -123,7 +123,7 @@ static int lnw_irq_type(unsigned irq, unsigned type)
 	void __iomem *grer = (void __iomem *)(&lnw->reg_base->GRER[reg]);
 	void __iomem *gfer = (void __iomem *)(&lnw->reg_base->GFER[reg]);
 
-	if (gpio < 0 || gpio > lnw->chip.ngpio)
+	if (gpio >= lnw->chip.ngpio)
 		return -EINVAL;
 	spin_lock_irqsave(&lnw->lock, flags);
 	if (type & IRQ_TYPE_EDGE_RISING)