<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><span class="vcard"><a class="email" href="mailto:t.p.northover@gmail.com" title="Tim Northover <t.p.northover@gmail.com>"> <span class="fn">Tim Northover</span></a>
</span> changed
          <a class="bz_bug_link 
          bz_status_RESOLVED  bz_closed"
   title="RESOLVED INVALID - clang -O2 miscompiled pointer arithmetics leads to core dumps"
   href="https://bugs.llvm.org/show_bug.cgi?id=33990">bug 33990</a>
          <br>
             <table border="1" cellspacing="0" cellpadding="8">
          <tr>
            <th>What</th>
            <th>Removed</th>
            <th>Added</th>
          </tr>

         <tr>
           <td style="text-align:right;">CC</td>
           <td>
                
           </td>
           <td>t.p.northover@gmail.com
           </td>
         </tr>

         <tr>
           <td style="text-align:right;">Status</td>
           <td>NEW
           </td>
           <td>RESOLVED
           </td>
         </tr>

         <tr>
           <td style="text-align:right;">Resolution</td>
           <td>---
           </td>
           <td>INVALID
           </td>
         </tr></table>
      <p>
        <div>
            <b><a class="bz_bug_link 
          bz_status_RESOLVED  bz_closed"
   title="RESOLVED INVALID - clang -O2 miscompiled pointer arithmetics leads to core dumps"
   href="https://bugs.llvm.org/show_bug.cgi?id=33990#c2">Comment # 2</a>
              on <a class="bz_bug_link 
          bz_status_RESOLVED  bz_closed"
   title="RESOLVED INVALID - clang -O2 miscompiled pointer arithmetics leads to core dumps"
   href="https://bugs.llvm.org/show_bug.cgi?id=33990">bug 33990</a>
              from <span class="vcard"><a class="email" href="mailto:t.p.northover@gmail.com" title="Tim Northover <t.p.northover@gmail.com>"> <span class="fn">Tim Northover</span></a>
</span></b>
        <pre>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:
<a href="http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html">http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html</a>

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).</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>