Skip to contentSkip to conversation

contrib/plugins/hwprofile: use BIT_ULL() for CPU mask, avoid UB shift

PATCH New Denis Sergeev 1 participant 1 message Sep 15, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 contrib/plugins/hwprofile: use BIT_ULL() for CPU mask, avoid UB shift

(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.

---
 contrib/plugins/hwprofile.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
contrib/plugins/hwprofile.c +3 -2
index a9838ccc87..7a470bbfd9 100644
@@ -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);
     }
 }
 
Signature · 1 line
2.50.1