[llvm-bugs] [Bug 33990] clang -O2 miscompiled pointer arithmetics leads to core dumps

via llvm-bugs llvm-bugs at lists.llvm.org
Sat Jul 29 20:24:22 PDT 2017


Tim Northover <t.p.northover at gmail.com> changed:

           What    |Removed                     |Added
                 CC|                            |t.p.northover at gmail.com
             Status|NEW                         |RESOLVED
         Resolution|---                         |INVALID

--- Comment #2 from Tim Northover <t.p.northover at gmail.com> ---
The program has undefined behaviour in this line:

    char *c = (char *)0 + foo();

The left-hand side of the addition is a null pointer, which you're not allowed
to perform arithmetic on.

The clause in the standard defines a few situations in which adding an integer
to a pointer produces a valid result and simply says "otherwise, the behaviour
is undefined". All of the actually defined cases start with a valid object
(which null isn't).

The definition of foo contains a similarly invalid subtraction.

I'm not sure if you've encountered undefined behaviour before but if not Chris
Lattner wrote a pretty good blog series on it a few years ago:

The short message is: avoid it at all costs. Its existence is what lets C and
C++ outperform most other languages but if you actually end up executing any
all bets are off (it's often referred to jokingly as "nasal demons" because it
would be perfectly permitted for the compiler to make demons fly out of your
nose according to the standard if your program has UB).

You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20170730/f312b55e/attachment.html>

More information about the llvm-bugs mailing list