ath10k: sdio: remove reduntant check in for loop
Subsystems (3)
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
@@ -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
Quoted 21 lines · Alex Dewar
The for loop checks whether
cur_sectionis 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)
Signed-off-by Alex Dewar <[email protected]>--- drivers/net/wireless/ath/ath10k/sdio.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/net/wireless/ath/ath10k/sdio.c +0
-0
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
-0section_size = cur_section->end - cur_section->start;drivers/net/wireless/ath/ath10k/sdio.c +0
-1if (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;
[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 thecur_sectionassignment at the end of the loop and use the loop statement to do it for youfor (;
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
PATCH v2 1/1 ath10k: sdio: remove redundant check in for loop
In reply to Saeed MahameedThe 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
@@ -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
Re: [PATCH v2] ath10k: sdio: remove redundant check in for loop
Hi Alex,
Quoted 6 lines · Alex Dewar
The for loop checks whether
cur_sectionis 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)
Suggested-by Saeed Mahameed <[email protected]>Signed-off-by Alex Dewar <[email protected]>---
v2: refactor in the manner suggested by Saeeddrivers/net/wireless/ath/ath10k/sdio.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-)drivers/net/wireless/ath/ath10k/sdio.c +0
-0
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
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_sectionis 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)
Suggested-by Saeed Mahameed <[email protected]>Signed-off-by Alex Dewar <[email protected]>---
v2: refactor in the manner suggested by Saeeddrivers/net/wireless/ath/ath10k/sdio.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-)drivers/net/wireless/ath/ath10k/sdio.c +0
-0drivers/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
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
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
Re: [PATCH v2] ath10k: sdio: remove redundant check in for loop
In reply to Alex DewarQuoted 10 lines · Alex Dewar
The for loop checks whether
cur_sectionis 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)
Suggested-by Saeed Mahameed <[email protected]>Signed-off-by Alex Dewar <[email protected]>Signed-off-by Kalle Valo <[email protected]>
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
Re: [PATCH v2] ath10k: sdio: remove redundant check in for loop
In reply to Alex DewarQuoted 10 lines · Alex Dewar
The for loop checks whether
cur_sectionis 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)
Suggested-by Saeed Mahameed <[email protected]>Signed-off-by Alex Dewar <[email protected]>Signed-off-by Kalle Valo <[email protected]>
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