This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c/12237] New: wrong code generated due to wrong static evaluation of a signed/unsigned comparison


PLEASE REPLY TO gcc-bugzilla@gcc.gnu.org ONLY, *NOT* gcc-bugs@gcc.gnu.org.

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=12237

           Summary: wrong code generated due to wrong static evaluation of a
                    signed/unsigned comparison
           Product: gcc
           Version: 3.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: philippe dot coucaud at antevista dot com
                CC: gcc-bugs at gcc dot gnu dot org
 GCC build triplet: i686-pc-linux
  GCC host triplet: i686-pc-cygwin
GCC target triplet: i686-pc-cygwin

The following program contains a comparison of an expression with 0 (j-k < 0).
The compiler assumes this expression is unsigned and thus cannot be negative
(the then branch is thus removed and f() always print "false"). But as j is
signed, (j-k) should be signed too and f() should print "true".

$ gcc t.c
$ ./a.exe
f(7) (j-k)=-1 -> false

----------------------------

void f(unsigned int k){
  int j = k - 1;

  printf("f(%i) (j-k)=%i -> ",k,j-k);
  
  if( (j-k) < 0)
     printf("true\n");
  else
     printf("false\n");
}

int main(){
  f(7);
  return 0;
}

----------------------------

adding option -W exhibits the wrong assumption on (j-k) and maybe the location
of the error (this message is generated in function shorten_compare, in file
c-common.c):

$ gcc t.c -W
t.c: In function `f':
t.c:7: warning: comparison of unsigned expression < 0 is always false


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]