Skip to contentSkip to conversation

brcmfmac: fix a loop exit condition

PATCH In review Dan Carpenter 5 participants 10 messages Apr 23, 2021 – Jun 15, 2021 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 brcmfmac: fix a loop exit condition

This code is supposed to loop over the whole board_type[] string. The current code kind of works just because ascii values start 97 and the string is likely shorter than that so it will break when we hit the NUL terminator. But really the condition should be "i < len" instead of "i < board_type[i]".

Fixes 29e354ebeeec ("brcmfmac: Transform compatible string for FW loading")
---
 drivers/net/wireless/broadcom/brcm80211/brcmfmac/of.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/wireless/broadcom/brcm80211/brcmfmac/of.c +1 -1
index a7554265f95f..9b75e396fc50 100644
@@ -34,7 +34,7 @@ void brcmf_of_probe(struct device *dev, enum brcmf_bus_type bus_type,
 		len = strlen(tmp) + 1;
 		board_type = devm_kzalloc(dev, len, GFP_KERNEL);
 		strscpy(board_type, tmp, len);
-		for (i = 0; i < board_type[i]; i++) {
+		for (i = 0; i < len; i++) {
 			if (board_type[i] == '/')
 				board_type[i] = '-';
 		}
Signature · 1 line
2.30.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Dan Carpenter

This code is supposed to loop over the whole board_type[] string. The current code kind of works just because ascii values start 97 and the string is likely shorter than that so it will break when we hit the NUL terminator. But really the condition should be "i < len" instead of "i < board_type[i]".

Fixes 29e354ebeeec ("brcmfmac: Transform compatible string for FW loading")

Good catch, I actually have serious doubts about whatever I was thinking when writing that line of code.

Quoted 18 lines
---
 drivers/net/wireless/broadcom/brcm80211/brcmfmac/of.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/wireless/broadcom/brcm80211/brcmfmac/of.c +1 -1
index a7554265f95f..9b75e396fc50 100644
@@ -34,7 +34,7 @@ void brcmf_of_probe(struct device *dev, enum brcmf_bus_type bus_type,
 		len = strlen(tmp) + 1;
 		board_type = devm_kzalloc(dev, len, GFP_KERNEL);
 		strscpy(board_type, tmp, len);
-		for (i = 0; i < board_type[i]; i++) {
+		for (i = 0; i < len; i++) {
 			if (board_type[i] == '/')
 				board_type[i] = '-';
 		}
#2 lore raw
In reply to Dan Carpenter
Quoted 17 lines · Dan Carpenter

This code is supposed to loop over the whole board_type[] string. The current code kind of works just because ascii values start 97 and the string is likely shorter than that so it will break when we hit the NUL terminator. But really the condition should be "i < len" instead of "i < board_type[i]".

Fixes 29e354ebeeec ("brcmfmac: Transform compatible string for FW loading")

---
 drivers/net/wireless/broadcom/brcm80211/brcmfmac/of.c | 2 +-

 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/wireless/broadcom/brcm80211/brcmfmac/of.c +0 -0
index a7554265f95f..9b75e396fc50 100644

drivers/net/wireless/broadcom/brcm80211/brcmfmac/of.c void brcmf_of_probe(struct device *dev, enum brcmf_bus_type bus_type, · line 40

Quoted 8 lines
drivers/net/wireless/broadcom/brcm80211/brcmfmac/of.c +1 -1
@@ -34,7 +34,7 @@ void brcmf_of_probe(struct device *dev, enum brcmf_bus_type bus_type,
  		len = strlen(tmp) + 1;
  		board_type = devm_kzalloc(dev, len, GFP_KERNEL);
  		strscpy(board_type, tmp, len);
-		for (i = 0; i < board_type[i]; i++) {
+		for (i = 0; i < len; i++) {
  			if (board_type[i] == '/')
  				board_type[i] = '-';
  		}

It should probably just use strreplace() though :)

johannes

#3 lore raw
Quoted 13 lines · Johannes Berg
Quoted 12 lines · Dan Carpenter

This code is supposed to loop over the whole board_type[] string. The current code kind of works just because ascii values start 97 and the string is likely shorter than that so it will break when we hit the NUL terminator. But really the condition should be "i < len" instead of "i < board_type[i]".

Fixes 29e354ebeeec ("brcmfmac: Transform compatible string for FW loading")

---
 drivers/net/wireless/broadcom/brcm80211/brcmfmac/of.c | 2 +-

 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 15 lines

drivers/net/wireless/broadcom/brcm80211/brcmfmac/of.c void brcmf_of_probe(struct device *dev, enum brcmf_bus_type bus_type, · line 40

Quoted 13 lines
drivers/net/wireless/broadcom/brcm80211/brcmfmac/of.c +1 -1
index a7554265f95f..9b75e396fc50 100644
@@ -34,7 +34,7 @@ void brcmf_of_probe(struct device *dev, enum brcmf_bus_type bus_type,
  		len = strlen(tmp) + 1;
  		board_type = devm_kzalloc(dev, len, GFP_KERNEL);
  		strscpy(board_type, tmp, len);
-		for (i = 0; i < board_type[i]; i++) {
+		for (i = 0; i < len; i++) {
  			if (board_type[i] == '/')
  				board_type[i] = '-';
  		}

It should probably just use strreplace() though :)

Good point. I'll send a v2.

regards,
dan carpenter

#4 lore raw

Le 23/04/2021 à 14:11, Dan Carpenter a écrit :

Quoted 30 lines · Johannes Berg
Quoted 13 lines · Dan Carpenter
Quoted 12 lines

This code is supposed to loop over the whole board_type[] string. The current code kind of works just because ascii values start 97 and the string is likely shorter than that so it will break when we hit the NUL terminator. But really the condition should be "i < len" instead of "i < board_type[i]".

Fixes 29e354ebeeec ("brcmfmac: Transform compatible string for FW loading")
---
  drivers/net/wireless/broadcom/brcm80211/brcmfmac/of.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 15 lines

drivers/net/wireless/broadcom/brcm80211/brcmfmac/of.c void brcmf_of_probe(struct device *dev, enum brcmf_bus_type bus_type, · line 40

Quoted 13 lines
drivers/net/wireless/broadcom/brcm80211/brcmfmac/of.c +1 -1
index a7554265f95f..9b75e396fc50 100644
@@ -34,7 +34,7 @@ void brcmf_of_probe(struct device *dev, enum brcmf_bus_type bus_type,
  		len = strlen(tmp) + 1;
  		board_type = devm_kzalloc(dev, len, GFP_KERNEL);
  		strscpy(board_type, tmp, len);
-		for (i = 0; i < board_type[i]; i++) {
+		for (i = 0; i < len; i++) {
  			if (board_type[i] == '/')
  				board_type[i] = '-';
  		}

It should probably just use strreplace() though :)

Quoted 2 lines

Good point. I'll send a v2.

and the 2 lines above look like a devm_kstrdup.

The (unlikely) malloc failure test is also missing.

CJ

Quoted 4 lines

regards,
dan carpenter

#5 lore raw
Quoted 14 lines · Christophe JAILLET
Quoted 14 lines
Quoted 13 lines

drivers/net/wireless/broadcom/brcm80211/brcmfmac/of.c void brcmf_of_probe(struct device *dev, enum brcmf_bus_type bus_type, · line 40

Quoted 11 lines

+++ b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/of.c

drivers/net/wireless/broadcom/brcm80211/brcmfmac/of.c +1 -1
@@ -34,7 +34,7 @@ void brcmf_of_probe(struct device *dev, enum brcmf_bus_type bus_type,
   		len = strlen(tmp) + 1;
   		board_type = devm_kzalloc(dev, len, GFP_KERNEL);
   		strscpy(board_type, tmp, len);
-		for (i = 0; i < board_type[i]; i++) {
+		for (i = 0; i < len; i++) {
   			if (board_type[i] == '/')
   				board_type[i] = '-';
   		}

It should probably just use strreplace() though :)

Quoted 6 lines
Quoted 2 lines

Good point. I'll send a v2.

and the 2 lines above look like a devm_kstrdup.

The (unlikely) malloc failure test is also missing.

How many issues can you have in 6 lines of code ;-)

johannes

#6 lore raw
In reply to Christophe JAILLET
Quoted 31 lines · Christophe JAILLET

Le 23/04/2021 à 14:11, Dan Carpenter a écrit :

Quoted 30 lines · Johannes Berg
Quoted 28 lines · Dan Carpenter
Quoted 17 lines

This code is supposed to loop over the whole board_type[] string. The current code kind of works just because ascii values start 97 and the string is likely shorter than that so it will break when we hit the NUL terminator. But really the condition should be "i < len" instead of "i < board_type[i]".

Fixes 29e354ebeeec ("brcmfmac: Transform compatible string for FW loading")
---
  drivers/net/wireless/broadcom/brcm80211/brcmfmac/of.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/wireless/broadcom/brcm80211/brcmfmac/of.c +0 -0
index a7554265f95f..9b75e396fc50 100644

drivers/net/wireless/broadcom/brcm80211/brcmfmac/of.c void brcmf_of_probe(struct device *dev, enum brcmf_bus_type bus_type, · line 40

Quoted 8 lines
drivers/net/wireless/broadcom/brcm80211/brcmfmac/of.c +1 -1
@@ -34,7 +34,7 @@ void brcmf_of_probe(struct device *dev, enum brcmf_bus_type bus_type,
  		len = strlen(tmp) + 1;
  		board_type = devm_kzalloc(dev, len, GFP_KERNEL);
  		strscpy(board_type, tmp, len);
-		for (i = 0; i < board_type[i]; i++) {
+		for (i = 0; i < len; i++) {
  			if (board_type[i] == '/')
  				board_type[i] = '-';
  		}

It should probably just use strreplace() though :)

Quoted 6 lines
Quoted 2 lines

Good point. I'll send a v2.

and the 2 lines above look like a devm_kstrdup.

The (unlikely) malloc failure test is also missing.

It turns out that Smatch checks for allocation failure were really ancient and really crap... I need to add all devm_ functions. Probably should re-write all that code.

Also originally GFP_NOFAIL was 0x800 and now it is 0x8000. Smatch was out of sync. So the functions that were supposed to be checked were all disabled... Need to figure out a better way to do that as well.

regards,
dan carpenter

#7 lore raw
In reply to Dan Carpenter
Quoted 9 lines · Dan Carpenter

This code is supposed to loop over the whole board_type[] string. The current code kind of works just because ascii values start 97 and the string is likely shorter than that so it will break when we hit the NUL terminator. But really the condition should be "i < len" instead of "i < board_type[i]".

Fixes 29e354ebeeec ("brcmfmac: Transform compatible string for FW loading")

There was talk about v2, but I don't see it in the patchwork.

Patch set to Changes Requested.

Signature · 3 lines
https://patchwork.kernel.org/project/linux-wireless/patch/YIKzmoMiTdToaIyP@mwanda/

https://wireless.wiki.kernel.org/en/developers/documentation/submittingpatches

A message that is not in the archive (sent off-list or not yet received): [email protected]

#9 lore raw
Quoted 8 lines · Kalle Valo
Quoted 6 lines · Dan Carpenter

This code is supposed to loop over the whole board_type[] string. The current code kind of works just because ascii values start 97 and the string is likely shorter than that so it will break when we hit the NUL terminator. But really the condition should be "i < len" instead of "i < board_type[i]".

Quoted 6 lines
Quoted 3 lines
Fixes 29e354ebeeec ("brcmfmac: Transform compatible string for FW loading")

There was talk about v2, but I don't see it in the patchwork.

Ah, crap. I started to debug Smatch to find out why it wasn't warning about some of these bugs and I got a bit carried away writing Smatch code and forgot to come back to this.

I will send it tomorrow.

regards,
dan carpenter

#10 lore raw
Quoted 13 lines · Dan Carpenter
Quoted 12 lines · Kalle Valo
Quoted 9 lines · Dan Carpenter

This code is supposed to loop over the whole board_type[] string. The current code kind of works just because ascii values start 97 and the string is likely shorter than that so it will break when we hit the NUL terminator. But really the condition should be "i < len" instead of "i < board_type[i]".

Fixes 29e354ebeeec ("brcmfmac: Transform compatible string for FW loading")
Quoted 7 lines
Quoted 1 line

There was talk about v2, but I don't see it in the patchwork.

Ah, crap. I started to debug Smatch to find out why it wasn't warning about some of these bugs and I got a bit carried away writing Smatch code and forgot to come back to this.

I will send it tomorrow.

No worries, take your time :) I just wanted to remind about this, or see if patchwork or the mailing list have lost patches again (which has happened in the past).

Signature · 3 lines
https://patchwork.kernel.org/project/linux-wireless/list/

https://wireless.wiki.kernel.org/en/developers/documentation/submittingpatches