Skip to contentSkip to conversation

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

PATCH Superseded Philippe Mathieu-Daudé 1 participant 1 message Nov 1, 2020

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

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

Rewrite the iteration to avoid an array overrun. This fixes 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.

---
Supersedes: <[email protected]>

---
 util/cutils.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
util/cutils.c +5 -3
index c395974fab4..723051da6e8 100644
@@ -889,11 +889,13 @@ char *freq_to_str(uint64_t freq_hz)
 {
     static const char *const suffixes[] = { "", "K", "M", "G", "T", "P", "E" };
     double freq = freq_hz;
-    size_t idx = 0;
+    size_t idx;
 
-    while (freq >= 1000.0 && idx < ARRAY_SIZE(suffixes)) {
+    for (idx = 0; idx < ARRAY_SIZE(suffixes) - 1; idx++) {
+        if (freq < 1000.0) {
+            break;
+        }
         freq /= 1000.0;
-        idx++;
     }
 
     return g_strdup_printf("%0.3g %sHz", freq, suffixes[idx]);
Signature · 1 line
2.26.2