This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[PATCH] Fix a few UB when left-shifting
- From: Marek Polacek <polacek at redhat dot com>
- To: GCC Patches <gcc-patches at gcc dot gnu dot org>
- Date: Fri, 4 Sep 2015 13:03:34 +0200
- Subject: [PATCH] Fix a few UB when left-shifting
- Authentication-results: sourceware.org; auth=none
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