]> sourceware.org Git - glibc.git/commitdiff
s390: Fix undefined behaviour in feenableexcept, fedisableexcept [BZ #30960]
authorStefan Liebler <stli@linux.ibm.com>
Wed, 18 Oct 2023 13:08:40 +0000 (15:08 +0200)
committerStefan Liebler <stli@linux.ibm.com>
Thu, 19 Oct 2023 12:28:22 +0000 (14:28 +0200)
If feenableexcept or fedisableexcept gets excepts=FE_INVALID=0x80
as input, we have a signed left shift: 0x80 << 24 which is not
representable as int and thus is undefined behaviour according to
C standard.

This patch casts excepts as unsigned int before shifting, which is
defined.

For me, the observed undefined behaviour is that the shift is done
with "unsigned"-instructions, which is exactly what we want.
Furthermore, I don't get any exception-flags.

After the fix, the code is using the same instruction sequence as
before.

sysdeps/s390/fpu/fedisblxcpt.c
sysdeps/s390/fpu/feenablxcpt.c

index 728f103f43a0e0a10b89c57f2633bec9dc776c04..55634c299fafb17b140af67da689db66dd00ecd0 100644 (file)
@@ -26,7 +26,8 @@ fedisableexcept (int excepts)
 
   _FPU_GETCW (temp);
   old_exc = (temp & FPC_EXCEPTION_MASK) >> FPC_EXCEPTION_MASK_SHIFT;
-  new_flags = (temp & (~((excepts & FE_ALL_EXCEPT) << FPC_EXCEPTION_MASK_SHIFT)));
+  new_flags = (temp & ~(((unsigned int) excepts & FE_ALL_EXCEPT)
+                       << FPC_EXCEPTION_MASK_SHIFT));
   _FPU_SETCW (new_flags);
 
   return old_exc;
index 0807e610a213b1b05b0e5d64114574050268eac4..d73659f07811ea922eb67b463f78a79af5a82480 100644 (file)
@@ -26,7 +26,8 @@ feenableexcept (int excepts)
 
   _FPU_GETCW (temp);
   old_exc = (temp & FPC_EXCEPTION_MASK) >> FPC_EXCEPTION_MASK_SHIFT;
-  new_flags = (temp | ((excepts & FE_ALL_EXCEPT) <<  FPC_EXCEPTION_MASK_SHIFT));
+  new_flags = (temp | (((unsigned int) excepts & FE_ALL_EXCEPT)
+                      << FPC_EXCEPTION_MASK_SHIFT));
   _FPU_SETCW (new_flags);
 
   return old_exc;
This page took 0.046606 seconds and 5 git commands to generate.