Skip to contentSkip to conversation

util/cutils: Silent Coverity array overrun warning in freq_to_str()

PATCH Superseded Philippe Mathieu-Daudé 3 participants 6 messages Oct 29, 2020

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 util/cutils: Silent Coverity array overrun warning in freq_to_str()

The biggest input value freq_to_str() can accept is UINT64_MAX, which is ~18.446 EHz, less than 1000 EHz.
Add an assertion to help Coverity.

This silents 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).

---
 util/cutils.c | 1 +
 1 file changed, 1 insertion(+)
util/cutils.c +1 -0
index c395974fab4..69c0ad7f888 100644
@@ -891,6 +891,7 @@ char *freq_to_str(uint64_t freq_hz)
     double freq = freq_hz;
     size_t idx = 0;
 
+    assert(freq <= UINT64_MAX); /* Max 64-bit value is less than 1000 EHz */
     while (freq >= 1000.0 && idx < ARRAY_SIZE(suffixes)) {
         freq /= 1000.0;
         idx++;
Signature · 1 line
2.26.2
#1 lore raw

Patch aiming the 5.2 release.

Quoted 31 lines · Philippe Mathieu-Daudé

The biggest input value freq_to_str() can accept is UINT64_MAX, which is ~18.446 EHz, less than 1000 EHz.
Add an assertion to help Coverity.

This silents 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).

---
 util/cutils.c | 1 +
 1 file changed, 1 insertion(+)
util/cutils.c +1 -1
index c395974fab4..69c0ad7f888 100644
@@ -891,6 +891,7 @@ char *freq_to_str(uint64_t freq_hz)
     double freq = freq_hz;
     size_t idx = 0;
 
+    assert(freq <= UINT64_MAX); /* Max 64-bit value is less than 1000 EHz */
     while (freq >= 1000.0 && idx < ARRAY_SIZE(suffixes)) {
         freq /= 1000.0;
         idx++;
--

2.26.2

#2 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 21 lines · Philippe Mathieu-Daudé

The biggest input value freq_to_str() can accept is UINT64_MAX, which is ~18.446 EHz, less than 1000 EHz.
Add an assertion to help Coverity.

This silents 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).

---
 util/cutils.c | 1 +
 1 file changed, 1 insertion(+)
util/cutils.c +0 -0
index c395974fab4..69c0ad7f888 100644

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

Quoted 7 lines
util/cutils.c +1 -0
@@ -891,6 +891,7 @@ char *freq_to_str(uint64_t freq_hz)
     double freq = freq_hz;
     size_t idx = 0;
 
+    assert(freq <= UINT64_MAX); /* Max 64-bit value is less than 1000 EHz */
     while (freq >= 1000.0 && idx < ARRAY_SIZE(suffixes)) {
         freq /= 1000.0;
         idx++;

I don't think I really agree with this as the way to silence Coverity. The reason Coverity complains is because in one part of the function you have code that says "it's possible for idx to be >= ARRAY_SIZE(suffixes)" (because you test that in the while loop condition) but then in the following part (where you dereference you have code that says "it's not possible for idx to be >= ARRAY_SIZE(suffixes)" (because you dereference suffixes[idx]).

We should either consistently assume that idx can never get to 7 (ie don't check it in the while loop condition because that test can never return true) or we should consistently guard against it happening (by switching the while loop to "<=", or by handling the idx==ARRAY_SIZE(suffixes) case specially.)

I think I would go for:
* remove the test from the while condition
* after the while loop, assert(idx < ARRAY_SIZE(suffixes));

thanks
-- PMM

#3 lore raw
Quoted 6 lines · Peter Maydell

We should either consistently assume that idx can never get to 7 (ie don't check it in the while loop condition because that test can never return true) or we should consistently guard against it happening (by switching the while loop to "<=", or by handling the idx==ARRAY_SIZE(suffixes) case specially.)

Oops; "switching to <=" isn't the right thing; you'd need to switch to "< ARRAY_SIZE(suffixes) - 1". Anyway I think we should do the other of the two options, not this one.

-- PMM

#4 lore raw
Quoted 11 lines · Peter Maydell
Quoted 6 lines · Peter Maydell

We should either consistently assume that idx can never get to 7 (ie don't check it in the while loop condition because that test can never return true) or we should consistently guard against it happening (by switching the while loop to "<=", or by handling the idx==ARRAY_SIZE(suffixes) case specially.)

Oops; "switching to <=" isn't the right thing; you'd need to switch to "< ARRAY_SIZE(suffixes) - 1". Anyway I think we should do the other of the two options, not this one.

"< ARRAY_SIZE(suffixes) - 1" patch submitted at
https://lore.kernel.org/qemu-devel/[email protected]/

Signature · 1 line
Eduardo
#5 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 20 lines · Philippe Mathieu-Daudé

The biggest input value freq_to_str() can accept is UINT64_MAX, which is ~18.446 EHz, less than 1000 EHz.
Add an assertion to help Coverity.

This silents 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).

---
 util/cutils.c | 1 +
 1 file changed, 1 insertion(+)
util/cutils.c +0 -0
index c395974fab4..69c0ad7f888 100644

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

Quoted 4 lines
util/cutils.c +1 -0
@@ -891,6 +891,7 @@ char *freq_to_str(uint64_t freq_hz)
     double freq = freq_hz;
     size_t idx = 0;
 
+    assert(freq <= UINT64_MAX); /* Max 64-bit value is less than 1000 EHz */

If Coverity is really able to conclude that this assert really ensures idx will never be out of bounds, I will be very impressed.

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

Quoted 1 line
util/cutils.c +0 -0
     while (freq >= 1000.0 && idx < ARRAY_SIZE(suffixes)) {

I don't understand why the (idx < ARRAY_SIZE(suffix)) expression above exists, if the code in this function will only work correctly if the expression never becomes false.

It sounds simpler and more obvious to fix the boundary check. In other words:

---

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
Eduardo