UBSAN complains that this is undefined behavior.
We can assume that empty substring (N==1) always satisfy conditions. So
std::memcmp will be called only only for N > 1 and Str.size() > 0.
Differential D26646
Avoid calling std::memcmp with nullptr vitalybuka on Nov 14 2016, 3:51 PM. Authored by
Details UBSAN complains that this is undefined behavior. We can assume that empty substring (N==1) always satisfy conditions. So
Diff Detail
Event Timeline
Comment Actions Do you mean that if it is called with "", such as StartsWith("", foo), S becomes a nullptr? Comment Actions Were you asking me? My comment is just saying that string literal is not the only way to invoke this overload. You can invoke it with a char array too, and strlen(char_array) is not guaranteed to be the same as sizeof(char_array) - 1. Comment Actions No, I was asking Vitaly. The commit message says that "Avoid calling std::memcmp with nullptr", but I don't understand how can a nullptr be passed to memcmp. |
I don't think this second assert is valid. Consider this code:
There is no guarantee what is the value of S[N-1]