From 6d7ccc576d52fe2e7d965bfdb0e63b997e77975a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Date: Tue, 17 Nov 2020 12:56:32 +0000 Subject: [PATCH] util/cutils: Fix Coverity array overrun in freq_to_str() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix Coverity CID 1435957: Memory - illegal accesses (OVERRUN): >>> 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. Reported-by: Eduardo Habkost Signed-off-by: Philippe Mathieu-Daudé Reviewed-by: Peter Maydell Reviewed-by: Eduardo Habkost Reviewed-by: Luc Michel Message-id: 20201101215755.2021421-1-f4bug@amsat.org Suggested-by: Peter Maydell Signed-off-by: Philippe Mathieu-Daudé Signed-off-by: Peter Maydell --- util/cutils.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/util/cutils.c b/util/cutils.c index 9498e28e1a..0b5073b330 100644 --- a/util/cutils.c +++ b/util/cutils.c @@ -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]); }