diff options
author | Marek Polacek <polacek@redhat.com> | 2014-12-04 19:20:12 +0000 |
---|---|---|
committer | Marek Polacek <mpolacek@gcc.gnu.org> | 2014-12-04 19:20:12 +0000 |
commit | 3119ac2925882e0e7b30984d71093eef349cabcc (patch) | |
tree | 57ddb125dd815eba52b7c03670badf6f9e880a46 | |
parent | d348be26b9d5c668e37459f65ab88ece7d2db2d6 (diff) | |
download | gcc-3119ac2925882e0e7b30984d71093eef349cabcc.zip gcc-3119ac2925882e0e7b30984d71093eef349cabcc.tar.gz gcc-3119ac2925882e0e7b30984d71093eef349cabcc.tar.bz2 |
re PR middle-end/56917 (-ftrapv detects a overflow wrongly.)
PR middle-end/56917
* fold-const.c (fold_unary_loc): Perform the negation in A's type
when transforming ~ (A - 1) or ~ (A + -1) to -A.
* c-c++-common/ubsan/pr56917.c: New test.
From-SVN: r218395
-rw-r--r-- | gcc/ChangeLog | 6 | ||||
-rw-r--r-- | gcc/fold-const.c | 11 | ||||
-rw-r--r-- | gcc/testsuite/ChangeLog | 5 | ||||
-rw-r--r-- | gcc/testsuite/c-c++-common/ubsan/pr56917.c | 34 |
4 files changed, 53 insertions, 3 deletions
diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 056ced2..2d41d93 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,9 @@ +2014-12-04 Marek Polacek <polacek@redhat.com> + + PR middle-end/56917 + * fold-const.c (fold_unary_loc): Perform the negation in A's type + when transforming ~ (A - 1) or ~ (A + -1) to -A. + 2014-12-04 Tobias Burnus <burnus@net-b.de> * Makefile.in: Remove CLOOGLIB and CLOOGINC. diff --git a/gcc/fold-const.c b/gcc/fold-const.c index 17eb5bb..c268007 100644 --- a/gcc/fold-const.c +++ b/gcc/fold-const.c @@ -8141,9 +8141,14 @@ fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0) && integer_onep (TREE_OPERAND (arg0, 1))) || (TREE_CODE (arg0) == PLUS_EXPR && integer_all_onesp (TREE_OPERAND (arg0, 1))))) - return fold_build1_loc (loc, NEGATE_EXPR, type, - fold_convert_loc (loc, type, - TREE_OPERAND (arg0, 0))); + { + /* Perform the negation in ARG0's type and only then convert + to TYPE as to avoid introducing undefined behavior. */ + tree t = fold_build1_loc (loc, NEGATE_EXPR, + TREE_TYPE (TREE_OPERAND (arg0, 0)), + TREE_OPERAND (arg0, 0)); + return fold_convert_loc (loc, type, t); + } /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */ else if (TREE_CODE (arg0) == BIT_XOR_EXPR && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type, diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index a0a78dc..0b3a9d6 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2014-12-04 Marek Polacek <polacek@redhat.com> + + PR middle-end/56917 + * c-c++-common/ubsan/pr56917.c: New test. + 2014-12-04 Martin Jambor <mjambor@suse.cz> * gcc.dg/ipa/propalign-1.c: New test. diff --git a/gcc/testsuite/c-c++-common/ubsan/pr56917.c b/gcc/testsuite/c-c++-common/ubsan/pr56917.c new file mode 100644 index 0000000..cfbae97 --- /dev/null +++ b/gcc/testsuite/c-c++-common/ubsan/pr56917.c @@ -0,0 +1,34 @@ +/* PR middle-end/56917 */ +/* { dg-do run } */ +/* { dg-options "-fsanitize=undefined -fno-sanitize-recover=undefined" } */ + +#define INT_MIN (-__INT_MAX__ - 1) +#define LONG_MIN (-__LONG_MAX__ - 1L) +#define LLONG_MIN (-__LONG_LONG_MAX__ - 1LL) + +int __attribute__ ((noinline,noclone)) +fn1 (unsigned int u) +{ + return (-(int) (u - 1U)) - 1; +} + +long __attribute__ ((noinline,noclone)) +fn2 (unsigned long int ul) +{ + return (-(long) (ul - 1UL)) - 1L; +} + +long long __attribute__ ((noinline,noclone)) +fn3 (unsigned long long int ull) +{ + return (-(long long) (ull - 1ULL)) - 1LL; +} + +int +main (void) +{ + if (fn1 (__INT_MAX__ + 1U) != INT_MIN + || fn2 (__LONG_MAX__ + 1UL) != LONG_MIN + || fn3 (__LONG_LONG_MAX__ + 1ULL) != LLONG_MIN) + __builtin_abort (); +} |