15 responses

  1. ncm
    January 9, 2008

    I see that -Wstrict-overflow is accepted by Debian’s gcc-4.2.3. However, it issues no warnings for the code examples presented.

    Is it included in -W or -Wall? (Debian has not packaged gcc-4.2 docs yet.)

    Reply

  2. Ian Lance Taylor
    January 9, 2008

    -Wstrict-overflow should issue a warning if you compile with -O2. -Wstrict-overflow warns about cases where an optimization relies on undefined overflow; it doesn’t have any way to detect possible overflow except when an optimization is applied. In some cases you will need to use -Wstrict-overflow=5.

    -Wall includes -Wstrict-overflow=1. Specifying -Wstrict-overflow with no number is equivalent to -Wstrict-overflow=2.

    The docs are available at http://gcc.gnu.org/onlinedocs/ .

    Reply

  3. ncm
    January 9, 2008

    I’m running

    $ gcc –version

    gcc (GCC) 4.2.3 20080102 (prerelease) (Debian 4.2.2-5)

    and putting all your code examples into t.c and compiling with

    $ gcc -O3 -Wstrict-overflow=5 -fstrict-overflow -Wall -W -c t.c

    I get no warnings at all. Should I be surprised at this?

    Reply

  4. Ian Lance Taylor
    January 10, 2008

    I think the full effects only come in on gcc mainline. At least, gcc 4.2 does not eliminate the loop, but gcc 4.3 will. All my examples were run with gcc mainline.

    Reply

  5. ncm
    January 10, 2008

    I see what happened:

    $ gcc -O3 -Wstrict-overflow=5 -Wall -c t.c

    is not the same as

    $ gcc -O3 -Wall -Wstrict-overflow=5 -c t.c

    Evidently the -Wall in the first example bumps the warning level back down to 1. I see that gcc-4.2 warns only about the first example, I suppose because it doesn’t perform that optimization on the next two. I can’t seem to get a warning about the last example from gcc-4.3. Shouldn’t I? It looks like bad code.

    Reply

  6. Ian Lance Taylor
    January 11, 2008

    There is no warning about the last example from gcc mainline because gcc doesn’t actually optimize it differently based on undefined signed overflow. It just goes ahead and generated the overflow code. I wasn’t clear: I didn’t intend that to be an example of using overflow to optimize, I intended to show a case where -fwrapv differed from -fno-strict-overflow.

    Reply

  7. egnor
    March 11, 2013

    At this point, several years later, would you say that -fno-strict-overflow is preferred to -fwrapv? The consensus on the Interwebs seems to prefer -fno-strict-overflow, because (1) it’s reported to generate better code (most of this probably because of your one anecdote above), (2) it avoids bugs in older gcc -fwrapv implementations.

    (I know, you would probably prefer people write code that doesn’t depend on overflow semantics at all, but that can be challenging in low level applications and legacy codebases.)

    Reply

Leave a Reply

Back to top
mobile desktop