stop calling memcmp with nullptrs

Summary:
it doesn't take nullptr according to its declaration in glibc, and calling it in this way causes our sanitizers (ubsan, clang analyze) to fail.
Closes https://github.com/facebook/rocksdb/pull/2776

Differential Revision: D5683260

Pulled By: ajkr

fbshipit-source-id: 114b137ee188172f96eedc43139255cae7bee80a
This commit is contained in:
Andrew Kryczka 2017-08-22 16:40:55 -07:00 committed by Facebook Github Bot
parent 78cb6b6112
commit 39ef900551

View File

@ -213,18 +213,17 @@ inline bool operator!=(const Slice& x, const Slice& y) {
return !(x == y);
}
// UBSAN complain that we pass nullptr to memcmp that's fine since
// we always do that for a string of len = 0
#ifdef ROCKSDB_UBSAN_RUN
#if defined(__clang__)
__attribute__((__no_sanitize__("undefined")))
#elif defined(__GNUC__)
__attribute__((__no_sanitize_undefined__))
#endif
#endif
inline int Slice::compare(const Slice& b) const {
const size_t min_len = (size_ < b.size_) ? size_ : b.size_;
assert((data_ != nullptr && b.data_ != nullptr) || min_len == 0);
if (min_len == 0) {
if (size_ > 0) {
return 1;
} else if (b.size_ > 0) {
return -1;
}
return 0;
}
assert(data_ != nullptr && b.data_ != nullptr);
int r = memcmp(data_, b.data_, min_len);
if (r == 0) {
if (size_ < b.size_) r = -1;