]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
x86: Fix UB in x86_cpu_present/x86_cpu_active
authorAdhemerval Zanella <adhemerval.zanella@linaro.org>
Fri, 18 Apr 2025 12:52:04 +0000 (09:52 -0300)
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>
Wed, 7 May 2025 17:21:21 +0000 (14:21 -0300)
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.

sysdeps/x86/sys/platform/x86.h

index 7f4aeac0c659d40051b3fa6db2b272a90ee50dc5..340146c225e3578d712a367ab8a097b86bf802d7 100644 (file)
@@ -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.  */