public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Fix a few UB when left-shifting
@ 2015-09-04 11:04 Marek Polacek
  2015-09-09 20:35 ` Jeff Law
  0 siblings, 1 reply; 2+ messages in thread
From: Marek Polacek @ 2015-09-04 11:04 UTC (permalink / raw)
  To: GCC Patches

This patch fixes a few -Wshift-negative-value warnings in the code.  It
oughtn't to cause any functional changes.  This will be needed for the
bootstrap to pass after I commit some other patch.

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

2015-09-03  Marek Polacek  <polacek@redhat.com>

	* c-common.c (warn_for_sign_compare): Cast to unsigned when shifting
	a negative value.

	* c-typeck.c (set_nonincremental_init_from_string): Use
	HOST_WIDE_INT_M1U when shifting a negative value.

	* rtlanal.c (split_double): Cast to unsigned when shifting a negative
	value.
	* sched-int.h (UNKNOWN_DEP_COST): Likewise.

diff --git gcc/c-family/c-common.c gcc/c-family/c-common.c
index 7691035..16d8346 100644
--- gcc/c-family/c-common.c
+++ gcc/c-family/c-common.c
@@ -12142,7 +12142,7 @@ warn_for_sign_compare (location_t location,
           if (bits < TYPE_PRECISION (result_type)
               && bits < HOST_BITS_PER_LONG && unsignedp)
             {
-              mask = (~ (HOST_WIDE_INT) 0) << bits;
+              mask = (~ (unsigned HOST_WIDE_INT) 0) << bits;
               if ((mask & constant) != mask)
 		{
 		  if (constant == 0)
diff --git gcc/c/c-typeck.c gcc/c/c-typeck.c
index e8c8189..04947a5 100644
--- gcc/c/c-typeck.c
+++ gcc/c/c-typeck.c
@@ -8250,7 +8250,7 @@ set_nonincremental_init_from_string (tree str,
 	    {
 	      if (val[0] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
 		{
-		  val[0] |= ((HOST_WIDE_INT) -1) << bitpos;
+		  val[0] |= HOST_WIDE_INT_M1U << bitpos;
 		  val[1] = -1;
 		}
 	    }
@@ -8261,8 +8261,7 @@ set_nonincremental_init_from_string (tree str,
 	    }
 	  else if (val[1] & (((HOST_WIDE_INT) 1)
 			     << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
-	    val[1] |= ((HOST_WIDE_INT) -1)
-		      << (bitpos - HOST_BITS_PER_WIDE_INT);
+	    val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
 	}
 
       value = wide_int_to_tree (type,
diff --git gcc/rtlanal.c gcc/rtlanal.c
index ef98f4b..b1be4db 100644
--- gcc/rtlanal.c
+++ gcc/rtlanal.c
@@ -5805,9 +5805,9 @@ split_double (rtx value, rtx *first, rtx *second)
       if (BITS_PER_WORD < HOST_BITS_PER_LONG && BITS_PER_WORD == 32)
 	{
 	  if (l[0] & ((long) 1 << 31))
-	    l[0] |= ((long) (-1) << 32);
+	    l[0] |= ((unsigned long) (-1) << 32);
 	  if (l[1] & ((long) 1 << 31))
-	    l[1] |= ((long) (-1) << 32);
+	    l[1] |= ((unsigned long) (-1) << 32);
 	}
 #endif
 
diff --git gcc/sched-int.h gcc/sched-int.h
index 61825ce..800262c 100644
--- gcc/sched-int.h
+++ gcc/sched-int.h
@@ -240,7 +240,7 @@ struct _dep
   int cost:20;
 };
 
-#define UNKNOWN_DEP_COST (-1<<19)
+#define UNKNOWN_DEP_COST ((int) ((unsigned int) -1 << 19))
 
 typedef struct _dep dep_def;
 typedef dep_def *dep_t;

	Marek

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

* Re: [PATCH] Fix a few UB when left-shifting
  2015-09-04 11:04 [PATCH] Fix a few UB when left-shifting Marek Polacek
@ 2015-09-09 20:35 ` Jeff Law
  0 siblings, 0 replies; 2+ messages in thread
From: Jeff Law @ 2015-09-09 20:35 UTC (permalink / raw)
  To: Marek Polacek, GCC Patches

On 09/04/2015 05:03 AM, Marek Polacek wrote:
> This patch fixes a few -Wshift-negative-value warnings in the code.  It
> oughtn't to cause any functional changes.  This will be needed for the
> bootstrap to pass after I commit some other patch.
>
> Bootstrapped/regtested on x86_64-linux, ok for trunk?
>
> 2015-09-03  Marek Polacek  <polacek@redhat.com>
>
> 	* c-common.c (warn_for_sign_compare): Cast to unsigned when shifting
> 	a negative value.
>
> 	* c-typeck.c (set_nonincremental_init_from_string): Use
> 	HOST_WIDE_INT_M1U when shifting a negative value.
>
> 	* rtlanal.c (split_double): Cast to unsigned when shifting a negative
> 	value.
> 	* sched-int.h (UNKNOWN_DEP_COST): Likewise.
OK.
jeff

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

end of thread, other threads:[~2015-09-09 19:57 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-09-04 11:04 [PATCH] Fix a few UB when left-shifting Marek Polacek
2015-09-09 20:35 ` 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).