Skip to contentSkip to conversation

virtio-gpu: use BIT() macro for scanout bitmask operations

PATCH In review Bin Guo 2 participants 2 messages Jul 17

lore
Share & tools
lore

Bin Guo

author
#0 lore raw

PATCH 1/1 virtio-gpu: use BIT() macro for scanout bitmask operations

The scanout_bitmask field is uint32_t, but all six bit operations used the signed literal 1 << shift. When the shift count reaches 31 this is undefined behavior (signed integer overflow). Replace every occurrence with the BIT() macro to perform an unsigned shift, which is well-defined for all bit positions 0-31 and consistent with QEMU bit-operation conventions.

No functional change; the result is already stored in a uint32_t, so the generated code is identical on most platforms.

---
 hw/display/virtio-gpu.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
hw/display/virtio-gpu.c +7 -6
index c20efe4fb9..cd07b70a05 100644
@@ -12,6 +12,7 @@
  */
 
 #include "qemu/osdep.h"
+#include "qemu/bitops.h"
 #include "qemu/units.h"
 #include "qemu/iov.h"
 #include "system/cpus.h"
@@ -393,7 +394,7 @@ void virtio_gpu_disable_scanout(VirtIOGPU *g, int scanout_id)
 
     res = virtio_gpu_find_resource(g, scanout->resource_id);
     if (res) {
-        res->scanout_bitmask &= ~(1 << scanout_id);
+        res->scanout_bitmask &= ~BIT(scanout_id);
     }
 
     qemu_console_set_surface(scanout->con, NULL);
@@ -411,7 +412,7 @@ static void virtio_gpu_resource_destroy(VirtIOGPU *g,
 
     if (res->scanout_bitmask) {
         for (i = 0; i < g->parent_obj.conf.max_outputs; i++) {
-            if (res->scanout_bitmask & (1 << i)) {
+            if (res->scanout_bitmask & BIT(i)) {
                 virtio_gpu_disable_scanout(g, i);
             }
         }
@@ -577,7 +578,7 @@ static void virtio_gpu_resource_flush(VirtIOGPU *g,
     for (i = 0; i < g->parent_obj.conf.max_outputs; i++) {
         QemuRect rect;
 
-        if (!(res->scanout_bitmask & (1 << i))) {
+        if (!(res->scanout_bitmask & BIT(i))) {
             continue;
         }
         scanout = &g->parent_obj.scanout[i];
@@ -611,10 +612,10 @@ void virtio_gpu_update_scanout(VirtIOGPU *g,
     scanout = &g->parent_obj.scanout[scanout_id];
     ores = virtio_gpu_find_resource(g, scanout->resource_id);
     if (ores) {
-        ores->scanout_bitmask &= ~(1 << scanout_id);
+        ores->scanout_bitmask &= ~BIT(scanout_id);
     }
 
-    res->scanout_bitmask |= (1 << scanout_id);
+    res->scanout_bitmask |= BIT(scanout_id);
     scanout->resource_id = res->resource_id;
     scanout->x = r->x;
     scanout->y = r->y;
@@ -1496,7 +1497,7 @@ static int virtio_gpu_post_load(void *opaque, int version_id)
         if (scanout->cursor.resource_id) {
             update_cursor(g, &scanout->cursor);
         }
-        res->scanout_bitmask |= (1 << i);
+        res->scanout_bitmask |= BIT(i);
     }
 
     return 0;
Signature · 1 line
2.50.1 (Apple Git-155)
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Bin Guo

The scanout_bitmask field is uint32_t, but all six bit operations used the signed literal 1 << shift. When the shift count reaches 31 this is undefined behavior (signed integer overflow). Replace every occurrence with the BIT() macro to perform an unsigned shift, which is well-defined for all bit positions 0-31 and consistent with QEMU bit-operation conventions.

Quoted 5 lines

No functional change; the result is already stored in a uint32_t, so the generated code is identical on most platforms.

Regards,
Akihiko Odaki