[PATCH] D11948: Add some macros to abstract marking of parameters as "not null", and use them in <cstring>

Marshall Clow via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 11 14:06:58 PDT 2015


On Tue, Aug 11, 2015 at 1:34 PM, Dan Albert <danalbert at google.com> wrote:

> 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.
>

Dan -- that's the situation you're in today.
GCC has done that kind of optimization for *years*.

Consider the following code (simplified to the point that it's a toy,
but...)

void * doFoo ( void *p, size_t sz ) {
std::memcpy(buf, p, sz);
if (p == nullptr)
p = malloc (10);
return p;
}

int main() {
void * q = doFoo(nullptr, 0);
std::cout << q << std::endl;
}

You see the UB there - right?  memcpy(buf, null, 0);

Built that with gcc (I used 4.9) - and see what it prints.
At -O3 (on my machine - Mac OS X) it prints "0".
It has removed the check for nullptr, and the malloc.

Interestingly enough, clang completely elides the call to doFoo, and just
calls malloc(10).

-- Marshall
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150811/6b69b5c8/attachment.html>


More information about the cfe-commits mailing list