From: Adhemerval Zanella Date: Fri, 18 Apr 2025 12:52:04 +0000 (-0300) Subject: x86: Fix UB in x86_cpu_present/x86_cpu_active X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7417fcf25a1a2398bac3126577ae6a8d7cd6db4a;p=thirdparty%2Fglibc.git 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 --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. */