public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Marek Polacek <polacek@redhat.com>
To: GCC Patches <gcc-patches@gcc.gnu.org>
Subject: [PATCH] Fix a few UB when left-shifting
Date: Fri, 04 Sep 2015 11:04:00 -0000	[thread overview]
Message-ID: <20150904110333.GB2813@redhat.com> (raw)

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

             reply	other threads:[~2015-09-04 11:03 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-04 11:04 Marek Polacek [this message]
2015-09-09 20:35 ` Jeff Law

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20150904110333.GB2813@redhat.com \
    --to=polacek@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).