public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/56917] New: -ftrapv detects a overflow wrongly.
@ 2013-04-11 10:40 akr at fsij dot org
  2014-11-03 12:31 ` [Bug c/56917] " mpolacek at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: akr at fsij dot org @ 2013-04-11 10:40 UTC (permalink / raw)
  To: gcc-bugs


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

             Bug #: 56917
           Summary: -ftrapv detects a overflow wrongly.
    Classification: Unclassified
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: akr@fsij.org


Created attachment 29855
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=29855
steps to reproduce the -ftrapv problem

I found that following program aborts with -ftrapv with gcc-4.8.0 on
Debian GNU/Linux (x86_64).

  % cat tst.c
  unsigned long ul = 0x8000000000000000UL;

  int main(int argc, char *argv[])
  {
    long l;
    l = (-(long)(ul-1))-1;
    return 0;
  }
  % /home/src/gcc/bin/gcc -g -ftrapv -Wall tst.c
  tst.c: In function 'main':
  tst.c:5:8: warning: variable 'l' set but not used [-Wunused-but-set-variable]
     long l;
      ^
  % ./a.out 
  zsh: abort (core dumped)  ./a.out

I think it should not abort on x86_64.

Since ul is LONG_MAX+1 (in indefinite precision), ul-1 is LONG_MAX which is
representable in long.
So the cast, (long)(ul-1), doesn't change the value.
Its negation, -(long)(ul-1), is -LONG_MAX which is also representable in long.
Its predecessor, (-(long)(ul-1))-1, is -LONG_MAX-1 = LONG_MIN which is also
representable in long.

gcc-ftrapv-problem-log.txt contains the log with gcc -v.


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2015-06-18 14:47 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-04-11 10:40 [Bug c/56917] New: -ftrapv detects a overflow wrongly akr at fsij dot org
2014-11-03 12:31 ` [Bug c/56917] " mpolacek at gcc dot gnu.org
2014-12-03 12:50 ` [Bug middle-end/56917] " mpolacek at gcc dot gnu.org
2014-12-03 12:54 ` mpolacek at gcc dot gnu.org
2014-12-03 12:58 ` rguenth at gcc dot gnu.org
2014-12-04 19:20 ` mpolacek at gcc dot gnu.org
2014-12-04 19:22 ` mpolacek at gcc dot gnu.org
2015-03-11 10:38 ` mpolacek at gcc dot gnu.org
2015-06-18 13:04 ` rguenth at gcc dot gnu.org
2015-06-18 14:47 ` rguenth at gcc dot gnu.org

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).