[glibc/azanella/ubsan-undef] x86: Fix UB in x86_cpu_present/x86_cpu_active
Adhemerval Zanella
azanella@sourceware.org
Fri Apr 25 19:51:40 GMT 2025
https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=655252c110e385b038241ab3d52533905948d52f
commit 655252c110e385b038241ab3d52533905948d52f
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date: Fri Apr 18 09:52:04 2025 -0300
x86: Fix UB in x86_cpu_present/x86_cpu_active
The elf/tst-cpu-features-supports (and other tests that check for
CPU features) triggers the following issue with ubsan:
UBSAN: Undefined behaviour in ../sysdeps/x86/sys/platform/x86.h:59:42 left shift of 1 by 31 cannot be represented in type 'int'
The active_array is unsigned, so use an unsigned constant as well.
Diff:
---
sysdeps/x86/sys/platform/x86.h | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/sysdeps/x86/sys/platform/x86.h b/sysdeps/x86/sys/platform/x86.h
index 7f4aeac0c6..340146c225 100644
--- a/sysdeps/x86/sys/platform/x86.h
+++ b/sysdeps/x86/sys/platform/x86.h
@@ -40,7 +40,7 @@ x86_cpu_present (unsigned int __index)
unsigned int __bit = __reg & (8 * sizeof (unsigned int) - 1);
__reg /= 8 * sizeof (unsigned int);
- return __ptr->cpuid_array[__reg] & (1 << __bit);
+ return __ptr->cpuid_array[__reg] & (1U << __bit);
}
static __inline__ bool
@@ -56,7 +56,7 @@ x86_cpu_active (unsigned int __index)
unsigned int __bit = __reg & (8 * sizeof (unsigned int) - 1);
__reg /= 8 * sizeof (unsigned int);
- return __ptr->active_array[__reg] & (1 << __bit);
+ return __ptr->active_array[__reg] & (1U << __bit);
}
/* CPU_FEATURE_PRESENT evaluates to true if CPU supports the feature. */
More information about the Glibc-cvs
mailing list