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

Denis Sergeev posted 1 patch 1 week, 6 days ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20250915112722.50169-1-zeff@altlinux.org
Maintainers: "Alex Bennée" <alex.bennee@linaro.org>, Alexandre Iooss <erdnaxe@crans.org>, Mahmoud Mandour <ma.mandourr@gmail.com>, Pierrick Bouvier <pierrick.bouvier@linaro.org>
contrib/plugins/hwprofile.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
[PATCH] contrib/plugins/hwprofile: use BIT_ULL() for CPU mask, avoid UB shift
Posted by Denis Sergeev 1 week, 6 days ago
(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