This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[PATCH] Fix PR36227 (partly)
- From: Richard Guenther <rguenther at suse dot de>
- To: gcc-patches at gcc dot gnu dot org
- Date: Tue, 13 May 2008 15:59:46 +0200 (CEST)
- Subject: [PATCH] Fix PR36227 (partly)
This fixes PR36227 where we fold
(unsigned)(ptr + off) > (unsigned)ptr
to
off > 0
via fold_sign_changed_comparison.
Bootstrapped and tested on x86_64-unknown-linxu-gnu, applied to mainline
and the 4.3 branch.
The not fixed part of this PR is that we fold
((unsigned)ptr) + off to ptr +p off possibly introducing undefined
pointer overflow. I'll attack this separately.
Richard.
2008-05-13 Richard Guenther <rguenther@suse.de>
PR middle-end/36227
* fold-const.c (fold_sign_changed_comparison): Do not allow
changes in pointer-ness.
* gcc.dg/pr36227.c: New testcase.
Index: fold-const.c
===================================================================
*** fold-const.c (revision 135255)
--- fold-const.c (working copy)
*************** fold_sign_changed_comparison (enum tree_
*** 6831,6837 ****
&& TREE_TYPE (TREE_OPERAND (arg1, 0)) == inner_type))
return NULL_TREE;
! if (TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
&& code != NE_EXPR
&& code != EQ_EXPR)
return NULL_TREE;
--- 6831,6838 ----
&& TREE_TYPE (TREE_OPERAND (arg1, 0)) == inner_type))
return NULL_TREE;
! if ((TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
! || POINTER_TYPE_P (inner_type) != POINTER_TYPE_P (outer_type))
&& code != NE_EXPR
&& code != EQ_EXPR)
return NULL_TREE;
Index: testsuite/gcc.dg/pr36227.c
===================================================================
*** testsuite/gcc.dg/pr36227.c (revision 0)
--- testsuite/gcc.dg/pr36227.c (revision 0)
***************
*** 0 ****
--- 1,12 ----
+ /* { dg-do compile } */
+ /* { dg-options "-O2 -Wstrict-overflow=3" } */
+
+ volatile unsigned long *
+ sat_add(volatile unsigned long *ptr, unsigned long i, volatile unsigned long *end)
+ {
+ if ((unsigned long)ptr + i * sizeof(*ptr) > (unsigned long)ptr) /* { dg-bogus "pointer wraparound" } */
+ return ptr + i;
+ else
+ return end;
+ }
+