Skip to contentSkip to conversation

ath10k: sdio: remove reduntant check in for loop

PATCH Superseded Alex Dewar 4 participants 10 messages Sep 14, 2020 – Nov 6, 2020

Subsystems (3)
lore
Share & tools
lore

Alex Dewar

author
#0 lore raw

PATCH 1/1 ath10k: sdio: remove reduntant check in for loop

The for loop checks whether cur_section is NULL on every iteration, but we know it can never be NULL as there is another check towards the bottom of the loop body. Remove this unnecessary check.

Also change i to start at 1, so that we don't need an extra +1 when we use it.

Addresses-Coverity: 1496984 ("Null pointer dereferences)

---
 drivers/net/wireless/ath/ath10k/sdio.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/wireless/ath/ath10k/sdio.c +2 -2
index 81ddaafb6721..f31ab2ec2c48 100644
@@ -2308,7 +2308,7 @@ static int ath10k_sdio_dump_memory_section(struct ath10k *ar,
 
 	count = 0;
 
-	for (i = 0; cur_section; i++) {
+	for (i = 1; ; i++) {
 		section_size = cur_section->end - cur_section->start;
 
 		if (section_size <= 0) {
@@ -2318,7 +2318,7 @@ static int ath10k_sdio_dump_memory_section(struct ath10k *ar,
 			break;
 		}
 
-		if ((i + 1) == mem_region->section_table.size) {
+		if (i == mem_region->section_table.size) {
 			/* last section */
 			next_section = NULL;
 			skip_size = 0;
Signature · 7 lines
2.28.0


_______________________________________________
ath10k mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/ath10k
#1 lore raw
Quoted 21 lines · Alex Dewar

The for loop checks whether cur_section is NULL on every iteration, but
we know it can never be NULL as there is another check towards the bottom of the loop body. Remove this unnecessary check.

Also change i to start at 1, so that we don't need an extra +1 wheno we
use it.

Addresses-Coverity: 1496984 ("Null pointer dereferences)

---
 drivers/net/wireless/ath/ath10k/sdio.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/wireless/ath/ath10k/sdio.c +0 -0
index 81ddaafb6721..f31ab2ec2c48 100644

drivers/net/wireless/ath/ath10k/sdio.c static int · line 2312

Quoted 6 lines
drivers/net/wireless/ath/ath10k/sdio.c +1 -1
@@ -2308,7 +2308,7 @@ static int
 ath10k_sdio_dump_memory_section(struct ath10k *ar,
 
 	count = 0;
 
-	for (i = 0; cur_section; i++) {
+	for (i = 1; ; i++) {

'i' is only referenced once inside the loop to check boundary,

the loop is actually iterating over cur_section, so i would make it clear in the loop statement, e.g.:
Remove the break condition and the cur_section assignment at the end of the loop and use the loop statement to do it for you

for (; cur_section; cur_section = next_section)

drivers/net/wireless/ath/ath10k/sdio.c static int · line 2322

Quoted 9 lines
drivers/net/wireless/ath/ath10k/sdio.c +0 -0
 		section_size = cur_section->end - cur_section->start;
 
drivers/net/wireless/ath/ath10k/sdio.c +0 -1
 		if (section_size <= 0) {
@@ -2318,7 +2318,7 @@ static int
 ath10k_sdio_dump_memory_section(struct ath10k *ar,
 			break;
 		}
 
-		if ((i + 1) == mem_region->section_table.size) {

And for i you can just increment it inline:
if (++i == ...)

Quoted 4 lines
drivers/net/wireless/ath/ath10k/sdio.c +1 -0
+		if (i == mem_region->section_table.size) {
 			/* last section */
 			next_section = NULL;

skip_size = 0;

author
#2 lore raw

[snip]

Quoted 18 lines

'i' is only referenced once inside the loop to check boundary,

the loop is actually iterating over cur_section, so i would make it clear in the loop statement, e.g.:
Remove the break condition and the cur_section assignment at the end of the loop and use the loop statement to do it for you

for (; cur_section; cur_section = next_section)

Quoted 8 lines
  		section_size = cur_section->end - cur_section->start;
  
  		if (section_size <= 0) {
(quoted diff) +0 -0
@@ -2318,7 +2318,7 @@ static int
 ath10k_sdio_dump_memory_section(struct ath10k *ar,
  			break;
  		}
 
Quoted 3 lines

drivers/net/wireless/ath/ath10k/sdio.c static int ath10k_sdio_dump_memory_section(struct ath10k *ar, · line 2321

Quoted 1 line
drivers/net/wireless/ath/ath10k/sdio.c +0 -1
-		if ((i + 1) == mem_region->section_table.size) {

And for i you can just increment it inline:
if (++i == ...)

Good suggestions! I've sent a v2 with these changes.

Quoted 6 lines
Quoted 6 lines
drivers/net/wireless/ath/ath10k/sdio.c +1 -0
+		if (i == mem_region->section_table.size) {
  			/* last section */
  			next_section = NULL;
  			skip_size = 0;

_______________________________________________
ath10k mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/ath10k

author
#3 lore raw

PATCH v2 1/1 ath10k: sdio: remove redundant check in for loop

In reply to Saeed Mahameed

The for loop checks whether cur_section is NULL on every iteration, but we know it can never be NULL as there is another check towards the bottom of the loop body. Refactor to avoid this unnecessary check.

Also, increment the variable i inline for clarity

Addresses-Coverity: 1496984 ("Null pointer dereferences)

---
v2: refactor in the manner suggested by Saeed

 drivers/net/wireless/ath/ath10k/sdio.c | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)
drivers/net/wireless/ath/ath10k/sdio.c +3 -9
index 81ddaafb6721..486886c74e6a 100644
@@ -2307,8 +2307,8 @@ static int ath10k_sdio_dump_memory_section(struct ath10k *ar,
 	}
 
 	count = 0;
-
-	for (i = 0; cur_section; i++) {
+	i = 0;
+	for (; cur_section; cur_section = next_section) {
 		section_size = cur_section->end - cur_section->start;
 
 		if (section_size <= 0) {
@@ -2318,7 +2318,7 @@ static int ath10k_sdio_dump_memory_section(struct ath10k *ar,
 			break;
 		}
 
-		if ((i + 1) == mem_region->section_table.size) {
+		if (++i == mem_region->section_table.size) {
 			/* last section */
 			next_section = NULL;
 			skip_size = 0;
@@ -2361,12 +2361,6 @@ static int ath10k_sdio_dump_memory_section(struct ath10k *ar,
 		}
 
 		count += skip_size;
-
-		if (!next_section)
-			/* this was the last section */
-			break;
-
-		cur_section = next_section;
 	}
 
 	return count;
Signature · 7 lines
2.28.0


_______________________________________________
ath10k mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/ath10k
#4 lore raw

Re: [PATCH v2] ath10k: sdio: remove redundant check in for loop

Hi Alex,

Quoted 6 lines · Alex Dewar

The for loop checks whether cur_section is NULL on every iteration, but we know it can never be NULL as there is another check towards the bottom of the loop body. Refactor to avoid this unnecessary check.

Also, increment the variable i inline for clarity

Comments below.

Quoted 14 lines

Addresses-Coverity: 1496984 ("Null pointer dereferences)

---
v2: refactor in the manner suggested by Saeed

 drivers/net/wireless/ath/ath10k/sdio.c | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)
drivers/net/wireless/ath/ath10k/sdio.c +0 -0
index 81ddaafb6721..486886c74e6a 100644

drivers/net/wireless/ath/ath10k/sdio.c static int ath10k_sdio_dump_memory_section(struct ath10k *ar, · line 2311

Quoted 7 lines
drivers/net/wireless/ath/ath10k/sdio.c +2 -2
@@ -2307,8 +2307,8 @@ static int ath10k_sdio_dump_memory_section(struct ath10k *ar,
        }
 
        count = 0;
-
-       for (i = 0; cur_section; i++) {
+       i = 0;
+       for (; cur_section; cur_section = next_section) {

You can have multiple statements in each section of a for() if you need to, e.g.

for (i = 1; cur_section; cur_section = next_section, i++) {

which means that the increment of i isn't hidden deep in the function body.

That said, this function is a mess. Something (approximately) like this might be more readable:

prev_end = memregion->start;

for (i = 0; i < mem_region->section_table.size; i++) {
    cur_section = &mem_region->section_table.sections[i];

    // fail if prev_end is greater than cur_section->start - message
from line 2329 and 2294
    // check section size - from line 2315

    skip_size = cur_section->start - prev_end;

    // check buffer size - from line 2339 - needs to account for the
skip size too.
    // fill in the skip size amount - from line 2358 and 2304
    // ath10k_sdio_read_mem - from line 2346

    prev_end = cur_section->end;
}

Thanks,

Signature · 9 lines
Julian Calaby

Email: [email protected]
Profile: http://www.google.com/profiles/julian.calaby/

_______________________________________________
ath10k mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/ath10k
#5 lore raw

Re: [PATCH v2] ath10k: sdio: remove redundant check in for loop

Quoted 34 lines · Julian Calaby
Quoted 6 lines · Alex Dewar

The for loop checks whether cur_section is NULL on every iteration, but we know it can never be NULL as there is another check towards the bottom of the loop body. Refactor to avoid this unnecessary check.

Also, increment the variable i inline for clarity

Comments below.

Quoted 14 lines

Addresses-Coverity: 1496984 ("Null pointer dereferences)

---
v2: refactor in the manner suggested by Saeed

 drivers/net/wireless/ath/ath10k/sdio.c | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)
drivers/net/wireless/ath/ath10k/sdio.c +0 -0
index 81ddaafb6721..486886c74e6a 100644

drivers/net/wireless/ath/ath10k/sdio.c static int ath10k_sdio_dump_memory_section(struct ath10k *ar, · line 2311

Quoted 7 lines
drivers/net/wireless/ath/ath10k/sdio.c +2 -2
@@ -2307,8 +2307,8 @@ static int ath10k_sdio_dump_memory_section(struct ath10k *ar,
        }
 
        count = 0;
-
-       for (i = 0; cur_section; i++) {
+       i = 0;
+       for (; cur_section; cur_section = next_section) {

You can have multiple statements in each section of a for() if you need to, e.g.

Quoted 3 lines

for (i = 1; cur_section; cur_section = next_section, i++) {

which means that the increment of i isn't hidden deep in the function body.

Yeah, I was thinking the same. But I'll apply this patch anyway, it's still an improvement.

Quoted 4 lines

That said, this function is a mess. Something (approximately) like this might be more readable:

prev_end = memregion->start;

Quoted 16 lines
for (i = 0; i < mem_region->section_table.size; i++) {
    cur_section = &mem_region->section_table.sections[i];

    // fail if prev_end is greater than cur_section->start - message
from line 2329 and 2294
    // check section size - from line 2315

    skip_size = cur_section->start - prev_end;

    // check buffer size - from line 2339 - needs to account for the
skip size too.
    // fill in the skip size amount - from line 2358 and 2304
    // ath10k_sdio_read_mem - from line 2346

    prev_end = cur_section->end;
}

I agree. Anyone can come up with a patch?

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

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

_______________________________________________
ath10k mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/ath10k
author
#6 lore raw

Re: [PATCH v2] ath10k: sdio: remove redundant check in for loop

Quoted 1 line

I agree. Anyone can come up with a patch?

Hi Kalle,

I was thinking of having a go at this. Have you applied the v2 of this patch yet though? I couldn't see it in wireless-drivers-next. I just don't want to have to rebase the patch if you were going to apply this v2.

Best,
Alex

Quoted 5 lines
Signature · 4 lines
-- 
https://patchwork.kernel.org/project/linux-wireless/list/

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

_______________________________________________
ath10k mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/ath10k

#7 lore raw

Re: [PATCH v2] ath10k: sdio: remove redundant check in for loop

Quoted 8 lines · Alex Dewar
Quoted 1 line

I agree. Anyone can come up with a patch?

Hi Kalle,

I was thinking of having a go at this. Have you applied the v2 of this patch yet though? I couldn't see it in wireless-drivers-next. I just don't want to have to rebase the patch if you were going to apply this v2.

I have not applied this yet. It's in my pending branch but I can easily drop it. Just let me know what you prefer.

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

https://wireless.wiki.kernel.org/en/developers/documentation/submittingpatches
#8 lore raw

Re: [PATCH v2] ath10k: sdio: remove redundant check in for loop

In reply to Alex Dewar
Quoted 10 lines · Alex Dewar

The for loop checks whether cur_section is NULL on every iteration, but we know it can never be NULL as there is another check towards the bottom of the loop body. Refactor to avoid this unnecessary check.

Also, increment the variable i inline for clarity

Addresses-Coverity: 1496984 ("Null pointer dereferences)

Patch applied to ath-next branch of ath.git, thanks.

dbeb101d28eb ath10k: sdio: remove redundant check in for loop

Signature · 3 lines
https://patchwork.kernel.org/project/linux-wireless/patch/[email protected]/

https://wireless.wiki.kernel.org/en/developers/documentation/submittingpatches
#9 lore raw

Re: [PATCH v2] ath10k: sdio: remove redundant check in for loop

In reply to Alex Dewar
Quoted 10 lines · Alex Dewar

The for loop checks whether cur_section is NULL on every iteration, but we know it can never be NULL as there is another check towards the bottom of the loop body. Refactor to avoid this unnecessary check.

Also, increment the variable i inline for clarity

Addresses-Coverity: 1496984 ("Null pointer dereferences)

Patch applied to ath-next branch of ath.git, thanks.

dbeb101d28eb ath10k: sdio: remove redundant check in for loop

Signature · 9 lines
https://patchwork.kernel.org/project/linux-wireless/patch/[email protected]/

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


_______________________________________________
ath10k mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/ath10k