util/cutils: Fix bounds check at freq_to_str()
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)
Suggested-by Peter Maydell <[email protected]>
Signed-off-by Eduardo Habkost <[email protected]>
--- util/cutils.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
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 ) { freq /=1000.0 ; idx++; }
Signature · 1 line
2.28.0
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)Suggested-by Peter Maydell <[email protected]>Signed-off-by Eduardo Habkost <[email protected]>--- util/cutils.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)util/cutils.c +0
-0
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.
Reviewed-by Philippe Mathieu-Daudé <[email protected]>
Thanks.
Quoted 4 lines
util/cutils.c +0
-0freq /=1000.0 ; idx++; }
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)Suggested-by Peter Maydell <[email protected]>
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
Signed-off-by Eduardo Habkost <[email protected]>
thanks
-- PMM