diff mbox series

[kvmtool,4/4] x86/cpuid: fix undefined behaviour

Message ID 20220601165138.3135246-5-andre.przywara@arm.com (mailing list archive)
State New, archived
Headers show
Series Fix some undefined behaviour | expand

Commit Message

Andre Przywara June 1, 2022, 4:51 p.m. UTC
Shifting signed values is rarely a good idea, especially if the result
ends up setting the most significant bit. UBSAN warns about two
occasions in the CPUID filter code:
===========================
x86/cpuid.c:23:25: runtime error: left shift of 255 by 24 places cannot be represented in type 'int'
x86/cpuid.c:27:22: runtime error: left shift of 1 by 31 places cannot be represented in type 'int'
===========================

Fix those warnings by making sure we only deal with unsigned values.

Signed-off-by: Andre Przywara <andre.przywara@arm.com>
---
 x86/cpuid.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
diff mbox series

Patch

diff --git a/x86/cpuid.c b/x86/cpuid.c
index f4347a84..1ae681ce 100644
--- a/x86/cpuid.c
+++ b/x86/cpuid.c
@@ -8,7 +8,7 @@ 
 
 #define	MAX_KVM_CPUID_ENTRIES		100
 
-static void filter_cpuid(struct kvm_cpuid2 *kvm_cpuid, int cpu_id)
+static void filter_cpuid(struct kvm_cpuid2 *kvm_cpuid, unsigned int cpu_id)
 {
 	unsigned int i;
 
@@ -20,11 +20,11 @@  static void filter_cpuid(struct kvm_cpuid2 *kvm_cpuid, int cpu_id)
 
 		switch (entry->function) {
 		case 1:
-			entry->ebx &= ~(0xff << 24);
+			entry->ebx &= ~(0xffU << 24);
 			entry->ebx |= cpu_id << 24;
 			/* Set X86_FEATURE_HYPERVISOR */
 			if (entry->index == 0)
-				entry->ecx |= (1 << 31);
+				entry->ecx |= (1U << 31);
 			break;
 		case 6:
 			/* Clear X86_FEATURE_EPB */