(1 << cpu_index) promotes 1 to int and can trigger undefined behavior on
32-bit ints when cpu_index >= 31. Static analyzers also flag this as a
potential overflow.
cpu_read/cpu_write are 64-bit bitmasks, so use BIT_ULL(cpu_index) from
qemu/bitops.h to construct the mask explicitly as 1ULL<<cpu_index.
This preserves the existing 64-bit semantics and removes the UB.
Found by Linux Verification Center (linuxtesting.org) with SVACE.
Signed-off-by: Denis Sergeev <zeff@altlinux.org>
---
contrib/plugins/hwprofile.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/contrib/plugins/hwprofile.c b/contrib/plugins/hwprofile.c
index a9838ccc87..7a470bbfd9 100644
--- a/contrib/plugins/hwprofile.c
+++ b/contrib/plugins/hwprofile.c
@@ -17,6 +17,7 @@
#include <glib.h>
#include <qemu-plugin.h>
+#include "qemu/bitops.h"
QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION;
@@ -187,10 +188,10 @@ static void inc_count(IOCounts *count, bool is_write, unsigned int cpu_index)
{
if (is_write) {
count->writes++;
- count->cpu_write |= (1 << cpu_index);
+ count->cpu_write |= BIT_ULL(cpu_index);
} else {
count->reads++;
- count->cpu_read |= (1 << cpu_index);
+ count->cpu_read |= BIT_ULL(cpu_index);
}
}
--
2.50.1