[PATCH 3/3] src: Use 1U for bit shifting

Michal Privoznik mprivozn at redhat.com
Mon Jun 14 11:06:14 UTC 2021


In a few places we take 1 and shift it left repeatedly. So much
that it won't longer fit into signed integer. The problem is that
this is undefined behaviour. Switching to 1U makes us stay within
boundaries.

Signed-off-by: Michal Privoznik <mprivozn at redhat.com>
---
 src/conf/domain_capabilities.h |  2 +-
 src/cpu/cpu_x86.c              | 10 +++++-----
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/src/conf/domain_capabilities.h b/src/conf/domain_capabilities.h
index 69e90893cc..b6433b20c9 100644
--- a/src/conf/domain_capabilities.h
+++ b/src/conf/domain_capabilities.h
@@ -231,7 +231,7 @@ virDomainCapsCPUModelsGet(virDomainCapsCPUModels *cpuModels,
                           const char *name);
 
 #define VIR_DOMAIN_CAPS_ENUM_IS_SET(capsEnum, value) \
-    ((capsEnum).values & (1 << value))
+    ((capsEnum).values & (1U << value))
 
 #define VIR_DOMAIN_CAPS_ENUM_SET(capsEnum, ...) \
     do { \
diff --git a/src/cpu/cpu_x86.c b/src/cpu/cpu_x86.c
index a4599499d0..a4792c21da 100644
--- a/src/cpu/cpu_x86.c
+++ b/src/cpu/cpu_x86.c
@@ -2575,12 +2575,12 @@ cpuidSetLeafD(virCPUData *data,
     sub1 = *cpuid;
     for (sub = 2; sub < 64; sub++) {
         if (sub < 32 &&
-            !(sub0.eax & (1 << sub)) &&
-            !(sub1.ecx & (1 << sub)))
+            !(sub0.eax & (1U << sub)) &&
+            !(sub1.ecx & (1U << sub)))
             continue;
         if (sub >= 32 &&
-            !(sub0.edx & (1 << (sub - 32))) &&
-            !(sub1.edx & (1 << (sub - 32))))
+            !(sub0.edx & (1U << (sub - 32))) &&
+            !(sub1.edx & (1U << (sub - 32))))
             continue;
 
         cpuid->ecx_in = sub;
@@ -2614,7 +2614,7 @@ cpuidSetLeafResID(virCPUData *data,
         return -1;
 
     for (sub = 1; sub < 32; sub++) {
-        if (!(res & (1 << sub)))
+        if (!(res & (1U << sub)))
             continue;
         cpuid->ecx_in = sub;
         cpuidCall(cpuid);
-- 
2.31.1




More information about the libvir-list mailing list