<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Tue, Aug 11, 2015 at 1:34 PM, Dan Albert <span dir="ltr"><<a href="mailto:danalbert@google.com" target="_blank">danalbert@google.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Yeah, those sound like exactly what we want. Helping people find UB is good, but optimizing assuming we've fixed all of the UB isn't something we can do. </div></blockquote><div><br></div><div>Dan -- that's the situation you're in today.</div><div>GCC has done that kind of optimization for *years*.</div><div><br></div><div>Consider the following code (simplified to the point that it's a toy, but...)</div><div><br></div><div><div><div><span class="Apple-tab-span" style="white-space:pre">      </span>void * doFoo ( void *p, size_t sz ) {</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>std::memcpy(buf, p, sz);</div><div><span class="Apple-tab-span" style="white-space:pre">             </span>if (p == nullptr)</div><div><span class="Apple-tab-span" style="white-space:pre">                    </span>p = malloc (10);</div><div><span class="Apple-tab-span" style="white-space:pre">             </span>return p;</div><div><span class="Apple-tab-span" style="white-space:pre">            </span>}</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">   </span>int main() {</div><div><span class="Apple-tab-span" style="white-space:pre">         </span>void * q = doFoo(nullptr, 0);</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>std::cout << q << std::endl;</div><div><span class="Apple-tab-span" style="white-space:pre">     </span>}</div></div></div><div><br></div><div>You see the UB there - right?  memcpy(buf, null, 0);</div><div><br></div><div>Built that with gcc (I used 4.9) - and see what it prints.</div><div>At -O3 (on my machine - Mac OS X) it prints "0".</div><div>It has removed the check for nullptr, and the malloc.</div><div><br></div><div>Interestingly enough, clang completely elides the call to doFoo, and just calls malloc(10).</div><div><br></div><div>-- Marshall</div></div></div></div>