Skip to contentSkip to conversation

util/cutils: Fix Coverity array overrun in freq_to_str()

PATCH In review Philippe Mathieu-Daudé 4 participants 5 messages Nov 1, 2020 – Nov 12, 2020

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 util/cutils: Fix Coverity array overrun in freq_to_str()

Fix Coverity CID 1435957: Memory - illegal accesses (OVERRUN):

Quoted 1 line
Quoted 1 line
Quoted 1 line

Overrunning array "suffixes" of 7 8-byte elements at element

index 7 (byte offset 63) using index "idx" (which evaluates to 7).

Note, the biggest input value freq_to_str() can accept is UINT64_MAX, which is ~18.446 EHz, less than 1000 EHz.

---
v3: Follow Peter's suggestion

---
 util/cutils.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
util/cutils.c +2 -1
index c395974fab4..2f869a843a5 100644
@@ -891,10 +891,11 @@ 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) {
         freq /= 1000.0;
         idx++;
     }
+    assert(idx < ARRAY_SIZE(suffixes));
 
     return g_strdup_printf("%0.3g %sHz", freq, suffixes[idx]);
 }
Signature · 1 line
2.26.2
Reviewed-by on v3 1/1
#1 lore raw
Quoted 26 lines · Philippe Mathieu-Daudé

Fix Coverity CID 1435957: Memory - illegal accesses (OVERRUN):

Quoted 1 line
Quoted 1 line
Quoted 1 line

Overrunning array "suffixes" of 7 8-byte elements at element

index 7 (byte offset 63) using index "idx" (which evaluates to 7).

Note, the biggest input value freq_to_str() can accept is UINT64_MAX, which is ~18.446 EHz, less than 1000 EHz.

---
v3: Follow Peter's suggestion

---
 util/cutils.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
util/cutils.c +0 -0
index c395974fab4..2f869a843a5 100644
@@ -891,10 +891,11 @@ char *freq_to_str(uint64_t freq_hz)
     double freq = freq_hz;
     size_t idx = 0;
 

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

Quoted 10 lines
util/cutils.c +2 -2
-    while (freq >= 1000.0 && idx < ARRAY_SIZE(suffixes)) {
+    while (freq >= 1000.0) {
         freq /= 1000.0;
         idx++;
     }
+    assert(idx < ARRAY_SIZE(suffixes));
 
     return g_strdup_printf("%0.3g %sHz", freq, suffixes[idx]);
 }
--

thanks
-- PMM

Reviewed-by on v3 1/1
#2 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 11 lines · Philippe Mathieu-Daudé

Fix Coverity CID 1435957: Memory - illegal accesses (OVERRUN):

Quoted 1 line
Quoted 1 line
Quoted 1 line

Overrunning array "suffixes" of 7 8-byte elements at element

index 7 (byte offset 63) using index "idx" (which evaluates to 7).

Note, the biggest input value freq_to_str() can accept is UINT64_MAX, which is ~18.446 EHz, less than 1000 EHz.

Thanks for taking the time to fix this!

Signature · 1 line
Eduardo
Reviewed-by on v3 1/1
#3 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 11 lines · Philippe Mathieu-Daudé

Fix Coverity CID 1435957: Memory - illegal accesses (OVERRUN):

Quoted 1 line
Quoted 1 line
Quoted 1 line

Overrunning array "suffixes" of 7 8-byte elements at element

index 7 (byte offset 63) using index "idx" (which evaluates to 7).

Note, the biggest input value freq_to_str() can accept is UINT64_MAX, which is ~18.446 EHz, less than 1000 EHz.

Quoted 26 lines

---
v3: Follow Peter's suggestion

---
 util/cutils.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
util/cutils.c +2 -1
index c395974fab4..2f869a843a5 100644
@@ -891,10 +891,11 @@ 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) {
         freq /= 1000.0;
         idx++;
     }
+    assert(idx < ARRAY_SIZE(suffixes));
 
     return g_strdup_printf("%0.3g %sHz", freq, suffixes[idx]);
 }
Signature · 1 line
2.26.2
#4 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 6 lines · Philippe Mathieu-Daudé

Fix Coverity CID 1435957: Memory - illegal accesses (OVERRUN):

Quoted 1 line
Quoted 1 line
Quoted 1 line

Overrunning array "suffixes" of 7 8-byte elements at element

index 7 (byte offset 63) using index "idx" (which evaluates to 7).

Quoted 8 lines

Note, the biggest input value freq_to_str() can accept is UINT64_MAX, which is ~18.446 EHz, less than 1000 EHz.

---
v3: Follow Peter's suggestion

Since nobody else has picked this up I'll take it via target-arm.next.

thanks
-- PMM