Skip to contentSkip to conversation

net: off by one, try #2

PATCH In review Roel Kluin 3 participants 12 messages Feb 11, 2009 – Feb 13, 2009

Subsystems (2)
lore
Share & tools
lore

Roel Kluin

author
#0 lore raw

PATCH 1/1 net: off by one, try #2

Better?

-------------------->8----------------8<----------------------- With while (x++ < n) { ... } x can reach n+1.

---
 drivers/net/3c505.c        |    4 ++--
 drivers/net/irda/mcs7780.c |    2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
drivers/net/3c505.c +2 -2
index 6124605..fb74846 100644
@@ -500,9 +500,9 @@ static bool receive_pcb(struct net_device *dev, pcb_struct * pcb)
 		pcb->data.raw[i++] = inb_command(dev->base_addr);
 		if (i > MAX_PCB_DATA)
 			INVALID_PCB_MSG(i);
-	} while ((stat & ASF_PCB_MASK) != ASF_PCB_END && j < 20000);
+	} while ((stat & ASF_PCB_MASK) != ASF_PCB_END && j <= 20000);
 	spin_unlock_irqrestore(&adapter->lock, flags);
-	if (j >= 20000) {
+	if (j > 20000) {
 		TIMEOUT_MSG(__LINE__);
 		return false;
 	}
drivers/net/irda/mcs7780.c +1 -1
index 7eafdca..b4f4f19 100644
@@ -585,7 +585,7 @@ static int mcs_speed_change(struct mcs_cb *mcs)
 		mcs_get_reg(mcs, MCS_RESV_REG, &rval);
 	} while(cnt++ < 100 && (rval & MCS_IRINTX));
 
-	if(cnt >= 100) {
+	if(cnt > 100) {
 		IRDA_ERROR("unable to change speed\n");
 		ret = -EIO;
 		goto error;
#1 lore raw
Quoted 1 line · Roel Kluin

Better?

Yes, but it looks like here is even more...

Quoted 15 lines

-------------------->8----------------8<----------------------- With while (x++ < n) { ... } x can reach n+1.

---
 drivers/net/3c505.c        |    4 ++--
 drivers/net/irda/mcs7780.c |    2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
drivers/net/3c505.c +0 -0
index 6124605..fb74846 100644

drivers/net/3c505.c static bool receive_pcb(struct net_device *dev, pcb_struct * pcb) · line 502

Quoted 3 lines
drivers/net/3c505.c +0 -0
@@ -500,9 +500,9 @@ static bool receive_pcb(struct net_device *dev, pcb_struct * pcb)
 		pcb->data.raw[i++] = inb_command(dev->base_addr);
 		if (i > MAX_PCB_DATA)
 			INVALID_PCB_MSG(i);

i is also misused here and array can be overriden, so additional break/return is needed.

Thanks,
Jarek P.

Quoted 21 lines
drivers/net/3c505.c +2 -2
-	} while ((stat & ASF_PCB_MASK) != ASF_PCB_END && j < 20000);
+	} while ((stat & ASF_PCB_MASK) != ASF_PCB_END && j <= 20000);
 	spin_unlock_irqrestore(&adapter->lock, flags);
-	if (j >= 20000) {
+	if (j > 20000) {
 		TIMEOUT_MSG(__LINE__);
 		return false;
 	}
drivers/net/irda/mcs7780.c +1 -1
index 7eafdca..b4f4f19 100644
@@ -585,7 +585,7 @@ static int mcs_speed_change(struct mcs_cb *mcs)
 		mcs_get_reg(mcs, MCS_RESV_REG, &rval);
 	} while(cnt++ < 100 && (rval & MCS_IRINTX));
 
-	if(cnt >= 100) {
+	if(cnt > 100) {
 		IRDA_ERROR("unable to change speed\n");
 		ret = -EIO;
 		goto error;
author
#2 lore raw
Quoted 5 lines
Quoted 1 line

With while (x++ < n) { ... } x can reach n+1.

Yes, but it looks like here is even more...
i is also misused here and array can be overriden, so additional break/return is needed.

Thanks, is this how it should be? -------------------->8----------------8<-----------------------

With while (x++ < n) { ... } x can reach n+1. As Jarek Poplawski pointed out, array pcb->data.raw was not correctly used.

---

drivers/net/3c505.c +8 -5
index 6124605..4cf3050 100644
@@ -497,12 +497,15 @@ static bool receive_pcb(struct net_device *dev, pcb_struct * pcb)
 	do {
 		j = 0;
 		while (((stat = get_status(dev->base_addr)) & ACRF) == 0 && j++ < 20000);
-		pcb->data.raw[i++] = inb_command(dev->base_addr);
-		if (i > MAX_PCB_DATA)
-			INVALID_PCB_MSG(i);
-	} while ((stat & ASF_PCB_MASK) != ASF_PCB_END && j < 20000);
+		pcb->data.raw[i] = inb_command(dev->base_addr);
+	} while (++i < MAX_PCB_DATA && (stat & ASF_PCB_MASK) != ASF_PCB_END && j <= 20000);
+
 	spin_unlock_irqrestore(&adapter->lock, flags);
-	if (j >= 20000) {
+	if (i >= MAX_PCB_DATA) {
+		INVALID_PCB_MSG(i);
+		return false;
+	}
+	if (j > 20000) {
 		TIMEOUT_MSG(__LINE__);
 		return false;
 	}
#3 lore raw
Quoted 7 lines · Roel Kluin
Quoted 5 lines
Quoted 1 line

With while (x++ < n) { ... } x can reach n+1.

Yes, but it looks like here is even more...
i is also misused here and array can be overriden, so additional break/return is needed.

Thanks, is this how it should be?

It looks (almost) OK to me. :-) Except this > 80 line.

On the other hand, I wonder if it's not a good time to make it more readable; I mean the first while (): length, ";", and maybe ++j similarly to i now?

BTW, I hope you remember about irda.

Jarek P.

Quoted 32 lines

-------------------->8----------------8<-----------------------

With while (x++ < n) { ... } x can reach n+1. As Jarek Poplawski pointed out, array pcb->data.raw was not correctly used.

---

drivers/net/3c505.c +8 -5
index 6124605..4cf3050 100644
@@ -497,12 +497,15 @@ static bool receive_pcb(struct net_device *dev, pcb_struct * pcb)
 	do {
 		j = 0;
 		while (((stat = get_status(dev->base_addr)) & ACRF) == 0 && j++ < 20000);
-		pcb->data.raw[i++] = inb_command(dev->base_addr);
-		if (i > MAX_PCB_DATA)
-			INVALID_PCB_MSG(i);
-	} while ((stat & ASF_PCB_MASK) != ASF_PCB_END && j < 20000);
+		pcb->data.raw[i] = inb_command(dev->base_addr);
+	} while (++i < MAX_PCB_DATA && (stat & ASF_PCB_MASK) != ASF_PCB_END && j <= 20000);
+
 	spin_unlock_irqrestore(&adapter->lock, flags);
-	if (j >= 20000) {
+	if (i >= MAX_PCB_DATA) {
+		INVALID_PCB_MSG(i);
+		return false;
+	}
+	if (j > 20000) {
 		TIMEOUT_MSG(__LINE__);
 		return false;
 	}
author
#4 lore raw
Quoted 9 lines · Jarek Poplawski
Quoted 5 lines · Roel Kluin
Quoted 4 lines
Quoted 1 line

With while (x++ < n) { ... } x can reach n+1.

Yes, but it looks like here is even more...
i is also misused here and array can be overriden, so additional break/return is needed.

Thanks, is this how it should be?

It looks (almost) OK to me. :-) Except this > 80 line.

Quoted 7 lines

On the other hand, I wonder if it's not a good time to make it more readable; I mean the first while (): length, ";", and maybe ++j similarly to i now?

BTW, I hope you remember about irda.

Jarek P.

Thanks again for reviewing, how's this? with irda this time. -------------------->8----------------8<-----------------------

With while (x++ < n) { ... } x can reach n+1. As Jarek Poplawski pointed out, array pcb->data.raw was not correctly used.

---

drivers/net/3c505.c +14 -8
index 6124605..4ade64a 100644
@@ -493,15 +493,21 @@ static bool receive_pcb(struct net_device *dev, pcb_struct * pcb)
 	}
 	/* read the data */
 	spin_lock_irqsave(&adapter->lock, flags);
-	i = 0;
-	do {
-		j = 0;
-		while (((stat = get_status(dev->base_addr)) & ACRF) == 0 && j++ < 20000);
-		pcb->data.raw[i++] = inb_command(dev->base_addr);
-		if (i > MAX_PCB_DATA)
-			INVALID_PCB_MSG(i);
-	} while ((stat & ASF_PCB_MASK) != ASF_PCB_END && j < 20000);
+	for (i = 0; i < MAX_PCB_DATA; i++) {
+		for (j = 0; j < 20000; j++) {
+			stat = get_status(dev->base_addr);
+			if (stat & ACRF)
+				break;
+		}
+		pcb->data.raw[i] = inb_command(dev->base_addr);
+		if (stat & ASF_PCB_MASK == ASF_PCB_END || j >= 20000)
+			break;
+	}
 	spin_unlock_irqrestore(&adapter->lock, flags);
+	if (i >= MAX_PCB_DATA) {
+		INVALID_PCB_MSG(i);
+		return false;
+	}
 	if (j >= 20000) {
 		TIMEOUT_MSG(__LINE__);
 		return false;
drivers/net/irda/mcs7780.c +1 -1
index 7eafdca..85e88da 100644
@@ -585,7 +585,7 @@ static int mcs_speed_change(struct mcs_cb *mcs)
 		mcs_get_reg(mcs, MCS_RESV_REG, &rval);
 	} while(cnt++ < 100 && (rval & MCS_IRINTX));
 
-	if(cnt >= 100) {
+	if (cnt > 100) {
 		IRDA_ERROR("unable to change speed\n");
 		ret = -EIO;
 		goto error;
#5 lore raw
Quoted 14 lines · Roel Kluin
Quoted 13 lines · Jarek Poplawski
Quoted 5 lines · Roel Kluin
Quoted 4 lines
Quoted 1 line

With while (x++ < n) { ... } x can reach n+1.

Yes, but it looks like here is even more...
i is also misused here and array can be overriden, so additional break/return is needed.

Thanks, is this how it should be?

It looks (almost) OK to me. :-) Except this > 80 line.

On the other hand, I wonder if it's not a good time to make it more readable; I mean the first while (): length, ";", and maybe ++j similarly to i now?

Quoted 5 lines
Quoted 3 lines

BTW, I hope you remember about irda.

Jarek P.

Thanks again for reviewing, how's this? with irda this time.

Hmm... except one bug: "stat & ..." needs "()"...

Roel, please resend once more, but separated: something could go to stable, but not necessarily together. And add more description, especially to this 3c505 (here is more changes than 'while (x++ < n)').

Anyway, I hope David will like these "off by one" patches again, especially after this, and sun3lance one. Good catches!

Cheers,
Jarek P.

Quoted 55 lines

-------------------->8----------------8<-----------------------

With while (x++ < n) { ... } x can reach n+1. As Jarek Poplawski pointed out, array pcb->data.raw was not correctly used.

---

drivers/net/3c505.c +14 -8
index 6124605..4ade64a 100644
@@ -493,15 +493,21 @@ static bool receive_pcb(struct net_device *dev, pcb_struct * pcb)
 	}
 	/* read the data */
 	spin_lock_irqsave(&adapter->lock, flags);
-	i = 0;
-	do {
-		j = 0;
-		while (((stat = get_status(dev->base_addr)) & ACRF) == 0 && j++ < 20000);
-		pcb->data.raw[i++] = inb_command(dev->base_addr);
-		if (i > MAX_PCB_DATA)
-			INVALID_PCB_MSG(i);
-	} while ((stat & ASF_PCB_MASK) != ASF_PCB_END && j < 20000);
+	for (i = 0; i < MAX_PCB_DATA; i++) {
+		for (j = 0; j < 20000; j++) {
+			stat = get_status(dev->base_addr);
+			if (stat & ACRF)
+				break;
+		}
+		pcb->data.raw[i] = inb_command(dev->base_addr);
+		if (stat & ASF_PCB_MASK == ASF_PCB_END || j >= 20000)
+			break;
+	}
 	spin_unlock_irqrestore(&adapter->lock, flags);
+	if (i >= MAX_PCB_DATA) {
+		INVALID_PCB_MSG(i);
+		return false;
+	}
 	if (j >= 20000) {
 		TIMEOUT_MSG(__LINE__);
 		return false;
drivers/net/irda/mcs7780.c +1 -1
index 7eafdca..85e88da 100644
@@ -585,7 +585,7 @@ static int mcs_speed_change(struct mcs_cb *mcs)
 		mcs_get_reg(mcs, MCS_RESV_REG, &rval);
 	} while(cnt++ < 100 && (rval & MCS_IRINTX));
 
-	if(cnt >= 100) {
+	if (cnt > 100) {
 		IRDA_ERROR("unable to change speed\n");
 		ret = -EIO;
 		goto error;
#6 lore raw
Quoted 1 line · Jarek Poplawski

On Wed, Feb 11, 2009 at 08:33:25PM +0100, Roel Kluin wrote:

...

One more thing below...

Quoted 19 lines
Quoted 19 lines

---

drivers/net/3c505.c +1 -8
index 6124605..4ade64a 100644
@@ -493,15 +493,21 @@ static bool receive_pcb(struct net_device *dev, pcb_struct * pcb)
 	}
 	/* read the data */
 	spin_lock_irqsave(&adapter->lock, flags);
-	i = 0;
-	do {
-		j = 0;
-		while (((stat = get_status(dev->base_addr)) & ACRF) == 0 && j++ < 20000);
-		pcb->data.raw[i++] = inb_command(dev->base_addr);
-		if (i > MAX_PCB_DATA)
-			INVALID_PCB_MSG(i);
-	} while ((stat & ASF_PCB_MASK) != ASF_PCB_END && j < 20000);
+	for (i = 0; i < MAX_PCB_DATA; i++) {
Quoted 8 lines

drivers/net/3c505.c static bool receive_pcb(struct net_device *dev, pcb_struct * pcb) · line 504

Quoted 8 lines
drivers/net/3c505.c +8 -0
+		for (j = 0; j < 20000; j++) {
+			stat = get_status(dev->base_addr);
+			if (stat & ACRF)
+				break;
+		}
+		pcb->data.raw[i] = inb_command(dev->base_addr);
+		if (stat & ASF_PCB_MASK == ASF_PCB_END || j >= 20000)
+			break;

Here is no ++i after breaking the loop, so no need for [--i] later; please, check this more.

Jarek P.

Quoted 23 lines
Quoted 23 lines
drivers/net/3c505.c +5 -0
+	}
 	spin_unlock_irqrestore(&adapter->lock, flags);
+	if (i >= MAX_PCB_DATA) {
+		INVALID_PCB_MSG(i);
+		return false;
+	}
 	if (j >= 20000) {
 		TIMEOUT_MSG(__LINE__);
 		return false;
drivers/net/irda/mcs7780.c +1 -1
index 7eafdca..85e88da 100644
@@ -585,7 +585,7 @@ static int mcs_speed_change(struct mcs_cb *mcs)
 		mcs_get_reg(mcs, MCS_RESV_REG, &rval);
 	} while(cnt++ < 100 && (rval & MCS_IRINTX));
 
-	if(cnt >= 100) {
+	if (cnt > 100) {
 		IRDA_ERROR("unable to change speed\n");
 		ret = -EIO;
 		goto error;
author
#7 lore raw

PATCH 1/1 3c505: do not set pcb->data.raw beyond its size

Many thanks, Jarek,

Is this changelog ok? ------------------------------>8----------------8<------------------------------

Ensure that we do not set pcb->data.raw beyond its size, print an error message and return false if we attempt to. A timout message was printed one too early.

---

drivers/net/3c505.c +16 -10
index 6124605..a8107f9 100644
@@ -493,21 +493,27 @@ static bool receive_pcb(struct net_device *dev, pcb_struct * pcb)
 	}
 	/* read the data */
 	spin_lock_irqsave(&adapter->lock, flags);
-	i = 0;
-	do {
-		j = 0;
-		while (((stat = get_status(dev->base_addr)) & ACRF) == 0 && j++ < 20000);
-		pcb->data.raw[i++] = inb_command(dev->base_addr);
-		if (i > MAX_PCB_DATA)
-			INVALID_PCB_MSG(i);
-	} while ((stat & ASF_PCB_MASK) != ASF_PCB_END && j < 20000);
+	for (i = 0; i < MAX_PCB_DATA; i++) {
+		for (j = 0; j < 20000; j++) {
+			stat = get_status(dev->base_addr);
+			if (stat & ACRF)
+				break;
+		}
+		pcb->data.raw[i] = inb_command(dev->base_addr);
+		if ((stat & ASF_PCB_MASK) == ASF_PCB_END || j >= 20000)
+			break;
+	}
 	spin_unlock_irqrestore(&adapter->lock, flags);
+	if (i >= MAX_PCB_DATA) {
+		INVALID_PCB_MSG(i);
+		return false;
+	}
 	if (j >= 20000) {
 		TIMEOUT_MSG(__LINE__);
 		return false;
 	}
-	/* woops, the last "data" byte was really the length! */
-	total_length = pcb->data.raw[--i];
+	/* the last "data" byte was really the length! */
+	total_length = pcb->data.raw[i];
 
 	/* safety check total length vs data length */
 	if (total_length != (pcb->length + 2)) {
#8 lore raw

Re: [PATCH] 3c505: do not set pcb->data.raw beyond its size

Quoted 3 lines · Roel Kluin

Many thanks, Jarek,

Is this changelog ok?

Yes, the changelog and patch look OK to me.

Thanks,
Jarek P.

Quoted 49 lines

------------------------------>8----------------8<------------------------------

Ensure that we do not set pcb->data.raw beyond its size, print an error message and return false if we attempt to. A timout message was printed one too early.

---

drivers/net/3c505.c +16 -10
index 6124605..a8107f9 100644
@@ -493,21 +493,27 @@ static bool receive_pcb(struct net_device *dev, pcb_struct * pcb)
 	}
 	/* read the data */
 	spin_lock_irqsave(&adapter->lock, flags);
-	i = 0;
-	do {
-		j = 0;
-		while (((stat = get_status(dev->base_addr)) & ACRF) == 0 && j++ < 20000);
-		pcb->data.raw[i++] = inb_command(dev->base_addr);
-		if (i > MAX_PCB_DATA)
-			INVALID_PCB_MSG(i);
-	} while ((stat & ASF_PCB_MASK) != ASF_PCB_END && j < 20000);
+	for (i = 0; i < MAX_PCB_DATA; i++) {
+		for (j = 0; j < 20000; j++) {
+			stat = get_status(dev->base_addr);
+			if (stat & ACRF)
+				break;
+		}
+		pcb->data.raw[i] = inb_command(dev->base_addr);
+		if ((stat & ASF_PCB_MASK) == ASF_PCB_END || j >= 20000)
+			break;
+	}
 	spin_unlock_irqrestore(&adapter->lock, flags);
+	if (i >= MAX_PCB_DATA) {
+		INVALID_PCB_MSG(i);
+		return false;
+	}
 	if (j >= 20000) {
 		TIMEOUT_MSG(__LINE__);
 		return false;
 	}
-	/* woops, the last "data" byte was really the length! */
-	total_length = pcb->data.raw[--i];
+	/* the last "data" byte was really the length! */
+	total_length = pcb->data.raw[i];
 
 	/* safety check total length vs data length */
 	if (total_length != (pcb->length + 2)) {
maintainer Applied
#9 lore raw

Re: [PATCH] 3c505: do not set pcb->data.raw beyond its size

From: Jarek Poplawski <[email protected]>
Date: Thu, 12 Feb 2009 06:37:15 +0000

Quoted 6 lines · Roel Kluin
Quoted 3 lines

Many thanks, Jarek,

Is this changelog ok?

Yes, the changelog and patch look OK to me.

Applied, thanks everyone.

author
#10 lore raw

PATCH 1/1 IRDA: cnt is off by 1

In reply to Jarek Poplawski
Quoted 2 lines · Jarek Poplawski

Roel, please resend once more, but separated: something could go to stable, but not necessarily together.

Ok, Here's for irda:

------------------------------>8----------------8<------------------------------ If no prior break occurs, cnt reaches 101 after the loop, so we are still able to change speed when cnt has become 100.

drivers/net/irda/mcs7780.c +1 -1
index 7eafdca..85e88da 100644
@@ -585,7 +585,7 @@ static int mcs_speed_change(struct mcs_cb *mcs)
 		mcs_get_reg(mcs, MCS_RESV_REG, &rval);
 	} while(cnt++ < 100 && (rval & MCS_IRINTX));
 
-	if(cnt >= 100) {
+	if (cnt > 100) {
 		IRDA_ERROR("unable to change speed\n");
 		ret = -EIO;
 		goto error;
maintainer Applied
#11 lore raw

Re: [PATCH] IRDA: cnt is off by 1

From: Roel Kluin <[email protected]>
Date: Wed, 11 Feb 2009 22:22:21 +0100

Quoted 4 lines

If no prior break occurs, cnt reaches 101 after the loop, so we are still able to change speed when cnt has become 100.

Applied, thanks Roel.