Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #8186

Re: Arithmetic overflow checking

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: Arithmetic overflow checking
Date 2011-07-12 12:16 -0700
Organization None to speak of
Message-ID <lntyartim9.fsf@nuthaus.mib.org> (permalink)
References (10 earlier) <overflow-20110712151620@ram.dialup.fu-berlin.de> <ivhskb$1gp$1@dont-email.me> <1boc0zcugu.fsf@snowball.wb.pfeifferfamily.net> <lny603tmop.fsf@nuthaus.mib.org> <1bsjqb1h2z.fsf@snowball.wb.pfeifferfamily.net>

Show all headers | View raw


Joe Pfeiffer <pfeiffer@cs.nmsu.edu> writes:
> Keith Thompson <kst-u@mib.org> writes:
[...]
>> On many systems, yes, you can detect signed overflow after the fact by
>> examining the values of the operands and the result.  But in C, the
>> behavior is undefined -- and even on systems that use 2's-complement, an
>> optimizing compiler can take advantage of that fact and generate code
>> based on the assumption that overflow never occurs.  For example, this:
>>
>>     int x = INT_MAX;
>>     if (x + 1 < x) {
>>         fprintf(stderr, "Overflow!\n");
>>     }
>>
>> can be optimized away  (For example, gcc does this at -O2 and above.)
>
> True, but I was considering the likelier case of something more like
>
>     y = x + 1;
>
>     if ((x > 0) && (y < 0))
> 	printf("overflow\n");
>
> Which strikes me as much less likely to be optimized away.

Perhaps -- but optimizing compilers can be very clever, often
cleverer than you expect them to be.  (Some years ago, a co-worker
was surprised to discover that a substantial chunk of code was
eliminated entirely, and assumed it was a compiler bug.  On further
investigation, she found that it was a perfectly legitimate
sequence of optimizations; the code didn't actually do anything,
but she didn't expect the optimizer to be clever enough to notice.
And she was working on the optimizer.)

Depending on the compiler *not noticing things* is not a good way to
detect overflows.

Here's a program based on your suggestion:

#include <stdio.h>
#include <time.h>
#include <limits.h>
int main(void)
{
    int x = time(NULL) < 0 ? 42 : INT_MAX;
    int y;

    y = x + 1;

    if ((x > 0) && (y < 0)) {
        printf("overflow\n");
    }
    else {
        printf("no overflow\n");
    }
    return 0;
}

When compiled with "gcc -O1", it prints "overflow".  With "gcc -O2", it
prints "no overflow".

[...]

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Re: Arithmetic overflow checking tm <thomas.mertes@gmx.at> - 2011-07-10 01:47 -0700
  Re: Arithmetic overflow checking China Blue Dolls <chine.bleu@yahoo.com> - 2011-07-10 02:47 -0700
    Re: Arithmetic overflow checking pete <pfiland@mindspring.com> - 2011-07-10 06:04 -0400
      Re: Arithmetic overflow checking China Blue Dolls <chine.bleu@yahoo.com> - 2011-07-10 03:29 -0700
        Re: Arithmetic overflow checking Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-07-10 20:52 +0300
        Re: Arithmetic overflow checking pete <pfiland@mindspring.com> - 2011-07-10 23:29 -0400
    Re: Arithmetic overflow checking tm <thomas.mertes@gmx.at> - 2011-07-10 04:44 -0700
      Re: Arithmetic overflow checking "BartC" <bc@freeuk.com> - 2011-07-12 11:33 +0100
        Re: Arithmetic overflow checking tm <thomas.mertes@gmx.at> - 2011-07-12 04:17 -0700
          Re: Arithmetic overflow checking "BartC" <bc@freeuk.com> - 2011-07-12 12:33 +0100
            Re: Arithmetic overflow checking tm <thomas.mertes@gmx.at> - 2011-07-12 05:24 -0700
              Re: Arithmetic overflow checking Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-07-12 21:45 -0400
          Re: Arithmetic overflow checking Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-07-12 05:25 -0700
            Re: Arithmetic overflow checking tm <thomas.mertes@gmx.at> - 2011-07-12 10:21 -0700
              Re: Arithmetic overflow checking Thomas Boell <tboell@domain.invalid> - 2011-07-12 22:39 +0200
                Re: Arithmetic overflow checking tm <thomas.mertes@gmx.at> - 2011-07-12 14:15 -0700
                Re: Arithmetic overflow checking Keith Thompson <kst-u@mib.org> - 2011-07-12 14:18 -0700
                Re: Arithmetic overflow checking tm <thomas.mertes@gmx.at> - 2011-07-12 23:54 -0700
            Re: Arithmetic overflow checking "BartC" <bc@freeuk.com> - 2011-07-12 19:14 +0100
              Re: Arithmetic overflow checking Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-07-13 00:20 -0700
        Re: Arithmetic overflow checking markspace <-@.> - 2011-07-12 09:26 -0700
          Re: Arithmetic overflow checking Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-07-12 10:52 -0600
            Re: Arithmetic overflow checking Keith Thompson <kst-u@mib.org> - 2011-07-12 10:48 -0700
              Re: Arithmetic overflow checking Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-07-12 12:36 -0600
                Re: Arithmetic overflow checking Keith Thompson <kst-u@mib.org> - 2011-07-12 12:16 -0700
                Re: Arithmetic overflow checking Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-07-12 14:09 -0600
                Re: Arithmetic overflow checking Keith Thompson <kst-u@mib.org> - 2011-07-12 14:16 -0700
              Re: Arithmetic overflow checking, not so hard after all "Greg A. Woods" <woods@robohack.org> - 2011-07-12 21:52 -0700
                Re: Arithmetic overflow checking, not so hard after all Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-07-13 12:50 +0100
                Re: Arithmetic overflow checking, not so hard after all "Greg A. Woods" <woods@robohack.org> - 2011-07-13 11:10 -0700
                Re: Arithmetic overflow checking, not so hard after all Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-07-14 01:34 +0100
                Re: Arithmetic overflow checking, not so hard after all "Greg A. Woods" <woods@robohack.org> - 2011-07-19 17:10 -0700
                Re: Arithmetic overflow checking, not so hard after all Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-07-19 20:47 -0600
                Re: Arithmetic overflow checking, not so hard after all Keith Thompson <kst-u@mib.org> - 2011-07-19 20:23 -0700
                Re: Arithmetic overflow checking, not so hard after all Todd Carnes <toddcarnes@gmail.com> - 2011-07-20 03:48 +0000
                Re: Arithmetic overflow checking, not so hard after all Keith Thompson <kst-u@mib.org> - 2011-07-13 09:27 -0700
              Re: Arithmetic overflow checking "MikeP" <mp011011@some.org> - 2011-07-14 23:55 -0500
          Re: Arithmetic overflow checking Martin Gregorie <martin@address-in-sig.invalid> - 2011-07-12 16:54 +0000
            Re: Arithmetic overflow checking Gene Wirchenko <genew@ocis.net> - 2011-07-12 11:35 -0700
          Re: Arithmetic overflow checking tm <thomas.mertes@gmx.at> - 2011-07-12 10:13 -0700
          Re: Arithmetic overflow checking Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-07-12 21:53 -0400
        Re: Arithmetic overflow checking "MikeP" <mp011011@some.org> - 2011-07-14 23:41 -0500
          Re: Arithmetic overflow checking Gene Wirchenko <genew@ocis.net> - 2011-07-15 10:56 -0700
            Re: Arithmetic overflow checking "MikeP" <mp011011@some.org> - 2011-07-15 21:27 -0500
      Re: Arithmetic overflow checking bugbear <bugbear@trim_papermule.co.uk_trim> - 2011-07-20 09:22 +0100
        Re: Arithmetic overflow checking tm <thomas.mertes@gmx.at> - 2011-07-20 10:51 -0700
          Re: Arithmetic overflow checking gordonb.3urm7@burditt.org (Gordon Burditt) - 2011-07-20 15:39 -0500
          Re: Arithmetic overflow checking "BartC" <bc@freeuk.com> - 2011-07-21 12:12 +0100
    Re: Arithmetic overflow checking Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-07-10 09:28 -0400
      Re: Arithmetic overflow checking Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-07-10 06:52 -0700
        Re: Arithmetic overflow checking Keith Thompson <kst-u@mib.org> - 2011-07-10 14:47 -0700
        Re: Arithmetic overflow checking gordonb.u8vng@burditt.org (Gordon Burditt) - 2011-07-11 18:45 -0500
      Re: Arithmetic overflow checking "MikeP" <mp011011@some.org> - 2011-07-14 23:07 -0500
    Re: Arithmetic overflow checking Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-07-10 12:25 -0400
  Re: Arithmetic overflow checking Robert Wessel <robertwessel2@yahoo.com> - 2011-07-10 10:47 -0500
  Re: Arithmetic overflow checking Gene Wirchenko <genew@ocis.net> - 2011-07-11 07:58 -0700
    Re: Arithmetic overflow checking Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-07-11 10:48 -0700
      Re: Arithmetic overflow checking Gene Wirchenko <genew@ocis.net> - 2011-07-11 14:40 -0700
    Re: Arithmetic overflow checking tm <thomas.mertes@gmx.at> - 2011-07-11 14:54 -0700
      Re: Arithmetic overflow checking Gene Wirchenko <genew@ocis.net> - 2011-07-11 15:55 -0700
      Re: Arithmetic overflow checking Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-07-11 21:51 -0400
        Re: Arithmetic overflow checking Gene Wirchenko <genew@ocis.net> - 2011-07-11 21:31 -0700
          Re: Arithmetic overflow checking Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-07-11 23:16 -0700
          Re: Arithmetic overflow checking James Kuyper <jameskuyper@verizon.net> - 2011-07-12 06:28 -0400
          Re: Arithmetic overflow checking David Thompson <dave.thompson2@verizon.net> - 2011-07-24 22:13 -0400
  Re: Arithmetic overflow checking "io_x" <a@b.c.invalid> - 2011-07-12 09:05 +0200
    Re: Arithmetic overflow checking tm <thomas.mertes@gmx.at> - 2011-07-12 02:22 -0700
      Re: Arithmetic overflow checking "io_x" <a@b.c.invalid> - 2011-07-12 11:34 +0200
        Re: Arithmetic overflow checking Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-07-12 03:04 -0700
          Re: Arithmetic overflow checking "Greg A. Woods" <woods@robohack.org> - 2011-07-12 19:55 -0700
            Re: Arithmetic overflow checking "Greg A. Woods" <woods@robohack.org> - 2011-07-12 21:54 -0700
            Re: Arithmetic overflow checking "MikeP" <mp011011@some.org> - 2011-07-14 23:28 -0500
              Re: Arithmetic overflow checking "Greg A. Woods" <woods@robohack.org> - 2011-07-19 17:16 -0700
        Re: Arithmetic overflow checking tm <thomas.mertes@gmx.at> - 2011-07-12 03:33 -0700
        Re: Arithmetic overflow checking David Lamb <dalamb@cs.queensu.ca> - 2011-07-12 08:29 -0400
      Re: Arithmetic overflow checking "io_x" <a@b.c.invalid> - 2011-07-12 13:18 +0200
      Re: Arithmetic overflow checking Gene Wirchenko <genew@ocis.net> - 2011-07-12 11:39 -0700
        Re: Arithmetic overflow checking Patricia Shanahan <pats@acm.org> - 2011-07-12 12:38 -0700
          Re: Arithmetic overflow checking markspace <-@.> - 2011-07-12 13:20 -0700
            Re: Arithmetic overflow checking Gene Wirchenko <genew@ocis.net> - 2011-07-12 13:23 -0700
              Re: Arithmetic overflow checking Martin Gregorie <martin@address-in-sig.invalid> - 2011-07-12 21:08 +0000
                Re: Arithmetic overflow checking lewbloch <lewbloch@gmail.com> - 2011-07-12 14:48 -0700
                Re: Arithmetic overflow checking Gene Wirchenko <genew@ocis.net> - 2011-07-12 15:24 -0700
                Re: Arithmetic overflow checking lewbloch <lewbloch@gmail.com> - 2011-07-12 16:09 -0700
                Re: Arithmetic overflow checking Gene Wirchenko <genew@ocis.net> - 2011-07-13 10:38 -0700
                Re: Arithmetic overflow checking Patricia Shanahan <pats@acm.org> - 2011-07-13 11:00 -0700
                Re: Arithmetic overflow checking lewbloch <lewbloch@gmail.com> - 2011-07-13 12:16 -0700
                Re: Arithmetic overflow checking Gene Wirchenko <genew@ocis.net> - 2011-07-13 13:10 -0700
                Re: Arithmetic overflow checking markspace <-@.> - 2011-07-13 13:21 -0700
                Re: Arithmetic overflow checking Keith Thompson <kst-u@mib.org> - 2011-07-13 13:41 -0700
                Re: Arithmetic overflow checking Robert Wessel <robertwessel2@yahoo.com> - 2011-07-14 21:10 -0500
                Re: Arithmetic overflow checking "io_x" <a@b.c.invalid> - 2011-07-15 11:57 +0200
                Re: Arithmetic overflow checking Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-07-15 04:36 -0700
      Re: Arithmetic overflow checking "Greg A. Woods" <woods@robohack.org> - 2011-07-12 20:03 -0700
        Re: Arithmetic overflow checking tm <thomas.mertes@gmx.at> - 2011-07-13 00:52 -0700
          Re: Arithmetic overflow checking Patricia Shanahan <pats@acm.org> - 2011-07-13 07:45 -0700
            Re: Arithmetic overflow checking "Greg A. Woods" <woods@robohack.org> - 2011-07-13 11:28 -0700
          Re: Arithmetic overflow checking "Greg A. Woods" <woods@robohack.org> - 2011-07-13 11:15 -0700

csiph-web