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


Groups > comp.lang.c > #126644

Re: The bug

From David Brown <david.brown@hesbynett.no>
Newsgroups comp.lang.c
Subject Re: The bug
Date 2018-02-13 10:51 +0100
Organization A noiseless patient Spider
Message-ID <p5ucel$22j$1@dont-email.me> (permalink)
References <p5ignt$45g$1@dont-email.me> <p5uaug$orp$1@dont-email.me>

Show all headers | View raw


On 13/02/18 10:25, jacobnavia wrote:
> Le 08/02/2018 à 22:50, jacobnavia a écrit :
<snip>
>> Compiled without any optimizations, this program outputs nothing. Gcc
>> with -O2 will print "bug".
>>
>> Why is this happening?
>>
> 
> I found the root cause: gcc/clang will assume that there is no "UB" i.e.
> no overflow.

That is the way C works - it is why "undefined behaviour" is a useful
thing that can lead to optimisations and more efficient code, as long as
the code is correct.  There is a balance between "trust the programmer
and the code as written" and "be helpful to the programmer even if the
code is flawed".  Usually, the programmer wants the first behaviour when
he/she knows their code is correct, and the second behaviour when they
have made a mistake!  Static warning options can help, and compilation
with the "-fsanitize" options spot many run-time behaviour errors.

>  Then, a multiplication of two unsigned values into a signed
> result can't be negative.

I don't know of any real-world non-negative integers that can be
multiplied together to get a negative result - it is a perfectly
reasonable assumption for the compiler.

> 
> Then, the compilers wrongly assume, the bit 127 (the sign bit) can never
> be set.

The compiler's assumption is correct here.

> Then, the test at line 25 can never yioeld anything other than
> zero and the code in line 27 is dead code that is eliminated.
> 

So you get more efficient object code, based on the source code you wrote.

> This chain of false reasoning starts with the assumption: Overflows
> never exist.

It is not false reasoning or a false assumption.  It is /correct/
reasoning - but it comes from incorrect source code.

> 
> Great.
> 

It is great that you found the bug, and great that you understand the
compiler logic here.  It is less great that you don't understand that
the compiler's logic is correct.



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


Thread

The bug jacobnavia <jacob@jacob.remcomp.fr> - 2018-02-08 22:50 +0100
  Re: The bug Keith Thompson <kst-u@mib.org> - 2018-02-08 14:15 -0800
  Re: The bug jacobnavia <jacob@jacob.remcomp.fr> - 2018-02-13 10:25 +0100
    Re: The bug David Brown <david.brown@hesbynett.no> - 2018-02-13 10:51 +0100
      Re: The bug jacobnavia <jacob@jacob.remcomp.fr> - 2018-02-13 11:19 +0100
        Re: The bug David Brown <david.brown@hesbynett.no> - 2018-02-13 12:23 +0100
      Re: The bug Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2018-02-13 08:19 -0800
        Re: The bug David Brown <david.brown@hesbynett.no> - 2018-02-15 10:52 +0100
    Re: The bug Noob <root@127.0.0.1> - 2018-02-13 16:19 +0100
      Re: The bug Tim Rentsch <txr@alumni.caltech.edu> - 2018-02-19 12:11 -0800
        Re: The bug supercat@casperkitty.com - 2018-02-19 16:31 -0800
          Re: The bug Tim Rentsch <txr@alumni.caltech.edu> - 2018-02-21 23:03 -0800
            Re: The bug supercat@casperkitty.com - 2018-02-23 16:40 -0800
              Re: The bug Richard Damon <Richard@Damon-Family.org> - 2018-02-24 05:57 -0500
              Re: The bug Tim Rentsch <txr@alumni.caltech.edu> - 2018-02-24 13:11 -0800
                Re: The bug Anton Shepelev <anton.txt@gmail.com> - 2018-02-25 00:15 +0300
    Re: The bug Keith Thompson <kst-u@mib.org> - 2018-02-13 11:44 -0800
      Re: The bug asetofsymbols@gmail.com - 2018-02-13 12:13 -0800
      Re: The bug Noob <root@127.0.0.1> - 2018-02-15 09:45 +0100

csiph-web