Skip to content

Commit 72eac42

Browse files
committed
[xxHash] Don't trigger UB on empty StringRef
This is quite silly, but casting to uintptr_t seems like the easiest option to quiet ubsan. llvm/lib/Support/xxhash.cpp:107:12: runtime error: applying non-zero offset 8 to null pointer #0 0x7fe3660404c0 in llvm::xxHash64(llvm::StringRef) llvm/lib/Support/xxhash.cpp:107:12
1 parent cfc4860 commit 72eac42

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

llvm/lib/Support/xxhash.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,15 @@ uint64_t llvm::xxHash64(StringRef Data) {
104104

105105
H64 += (uint64_t)Len;
106106

107-
while (P + 8 <= BEnd) {
107+
while (reinterpret_cast<uintptr_t>(P) + 8 <=
108+
reinterpret_cast<uintptr_t>(BEnd)) {
108109
uint64_t const K1 = round(0, endian::read64le(P));
109110
H64 ^= K1;
110111
H64 = rotl64(H64, 27) * PRIME64_1 + PRIME64_4;
111112
P += 8;
112113
}
113114

114-
if (P + 4 <= BEnd) {
115+
if (reinterpret_cast<uintptr_t>(P) + 4 <= reinterpret_cast<uintptr_t>(BEnd)) {
115116
H64 ^= (uint64_t)(endian::read32le(P)) * PRIME64_1;
116117
H64 = rotl64(H64, 23) * PRIME64_2 + PRIME64_3;
117118
P += 4;

llvm/unittests/Support/xxhashTest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using namespace llvm;
1313

1414
TEST(xxhashTest, Basic) {
15+
EXPECT_EQ(0xef46db3751d8e999U, xxHash64(StringRef()));
1516
EXPECT_EQ(0x33bf00a859c4ba3fU, xxHash64("foo"));
1617
EXPECT_EQ(0x48a37c90ad27a659U, xxHash64("bar"));
1718
EXPECT_EQ(0x69196c1b3af0bff9U,

0 commit comments

Comments
 (0)