public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Fix UB in expmed.c (PR middle-end/61903)
@ 2014-08-25 19:21 Marek Polacek
  2014-08-29 20:08 ` Jeff Law
  0 siblings, 1 reply; 2+ messages in thread
From: Marek Polacek @ 2014-08-25 19:21 UTC (permalink / raw)
  To: GCC Patches

The following ought to fix two spots where an undefined behavior
can occur when compiling pr28045.c with instrumented compiler.
It does so by changing the type of V to an unsigned HOST_WIDE_INT
and performing the shift on unsigned HOST_WIDE_INT.
Hopefully it doesn't break anything...

Bootstrapped/regtested on x86_64-linux, ok for trunk?

2014-08-25  Marek Polacek  <polacek@redhat.com>

	PR middle-end/61903
	* expmed.c (store_fixed_bit_field_1): Shift UHWI 1 instead of HWI 1.
	Change the type of V to unsigned HOST_WIDE_INT.

diff --git gcc/expmed.c gcc/expmed.c
index 7b71616a2..e8d5c23 100644
--- gcc/expmed.c
+++ gcc/expmed.c
@@ -1051,16 +1051,17 @@ store_fixed_bit_field_1 (rtx op0, unsigned HOST_WIDE_INT bitsize,
 
   if (CONST_INT_P (value))
     {
-      HOST_WIDE_INT v = INTVAL (value);
+      unsigned HOST_WIDE_INT v = UINTVAL (value);
 
       if (bitsize < HOST_BITS_PER_WIDE_INT)
-	v &= ((HOST_WIDE_INT) 1 << bitsize) - 1;
+	v &= ((unsigned HOST_WIDE_INT) 1 << bitsize) - 1;
 
       if (v == 0)
 	all_zero = 1;
       else if ((bitsize < HOST_BITS_PER_WIDE_INT
-		&& v == ((HOST_WIDE_INT) 1 << bitsize) - 1)
-	       || (bitsize == HOST_BITS_PER_WIDE_INT && v == -1))
+		&& v == ((unsigned HOST_WIDE_INT) 1 << bitsize) - 1)
+	       || (bitsize == HOST_BITS_PER_WIDE_INT
+		   && v == (unsigned HOST_WIDE_INT) -1))
 	all_one = 1;
 
       value = lshift_value (mode, v, bitnum);

	Marek

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] Fix UB in expmed.c (PR middle-end/61903)
  2014-08-25 19:21 [PATCH] Fix UB in expmed.c (PR middle-end/61903) Marek Polacek
@ 2014-08-29 20:08 ` Jeff Law
  0 siblings, 0 replies; 2+ messages in thread
From: Jeff Law @ 2014-08-29 20:08 UTC (permalink / raw)
  To: Marek Polacek, GCC Patches

On 08/25/14 13:21, Marek Polacek wrote:
> The following ought to fix two spots where an undefined behavior
> can occur when compiling pr28045.c with instrumented compiler.
> It does so by changing the type of V to an unsigned HOST_WIDE_INT
> and performing the shift on unsigned HOST_WIDE_INT.
> Hopefully it doesn't break anything...
>
> Bootstrapped/regtested on x86_64-linux, ok for trunk?
>
> 2014-08-25  Marek Polacek  <polacek@redhat.com>
>
> 	PR middle-end/61903
> 	* expmed.c (store_fixed_bit_field_1): Shift UHWI 1 instead of HWI 1.
> 	Change the type of V to unsigned HOST_WIDE_INT.
OK.
Jeff

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2014-08-29 20:08 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-08-25 19:21 [PATCH] Fix UB in expmed.c (PR middle-end/61903) Marek Polacek
2014-08-29 20:08 ` Jeff Law

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).