[cfe-commits] Undefined Behavior when invoking StringRef constructor on a null pointer

Chris Lattner clattner at apple.com
Sun May 1 11:40:07 PDT 2011


On Apr 30, 2011, at 3:34 AM, Benjamin Kramer wrote:

>> 
>> I've used the second approach in the patch enclosed (which gmail thoroughly refused to. I have not measured the performance impact though.
> 
> Hi Matthieu,
> 
> We had this discussion before <http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20090928/088120.html>.
> 
> The result was that we don't want to have a NULL check in StringRef's ctor because it would slow down many users of StringRef and instead code that passes NULL to StringRef should be fixed.
> 
> An assert would be ok, but I don't think it's needed because strlen(NULL) is going to crash anyway.

Given that this has come up twice now, I think that adding an assert is the best way to go.  That makes it really obviously part of the API contract.

-Chris



More information about the cfe-commits mailing list