Skip to contentSkip to conversation

util/cutils: Fix bounds check at freq_to_str()

PATCH In review Eduardo Habkost 3 participants 3 messages Oct 29, 2020 – Oct 30, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 util/cutils: Fix bounds check at freq_to_str()

Fix bounds check for idx at freq_to_str(), to actually ensure idx never goes beyond the last element of the suffixes array.

Reported-by Coverity (CID 1435957: OVERRUN)
---
 util/cutils.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
util/cutils.c +1 -1
index c395974fab..0d9261e1e5 100644
@@ -891,7 +891,7 @@ char *freq_to_str(uint64_t freq_hz)
     double freq = freq_hz;
     size_t idx = 0;
 
-    while (freq >= 1000.0 && idx < ARRAY_SIZE(suffixes)) {
+    while (freq >= 1000.0 && idx < ARRAY_SIZE(suffixes) - 1) {
         freq /= 1000.0;
         idx++;
     }
Signature · 1 line
2.28.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 15 lines · Eduardo Habkost

Fix bounds check for idx at freq_to_str(), to actually ensure idx never goes beyond the last element of the suffixes array.

Reported-by Coverity (CID 1435957: OVERRUN)
---
 util/cutils.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
util/cutils.c +0 -0
index c395974fab..0d9261e1e5 100644

util/cutils.c char *freq_to_str(uint64_t freq_hz) · line 894

Quoted 5 lines
util/cutils.c +1 -1
@@ -891,7 +891,7 @@ char *freq_to_str(uint64_t freq_hz)
     double freq = freq_hz;
     size_t idx = 0;
 
-    while (freq >= 1000.0 && idx < ARRAY_SIZE(suffixes)) {
+    while (freq >= 1000.0 && idx < ARRAY_SIZE(suffixes) - 1) {

This was the first patch I wrote, but thought this wasn't the simplest way. Probably too tired.

Thanks.

Quoted 4 lines
util/cutils.c +0 -0
         freq /= 1000.0;
         idx++;
     }
#2 lore raw
In reply to Eduardo Habkost
Quoted 6 lines · Eduardo Habkost

Fix bounds check for idx at freq_to_str(), to actually ensure idx never goes beyond the last element of the suffixes array.

Reported-by Coverity (CID 1435957: OVERRUN)

Personally I preferred the other option (remove the idx check from the while loop and assert that idx is in bounds after the loop)...

Quoted 1 line

thanks
-- PMM