Skip to content

Commit a8844b2

Browse files
davidbenmarco
authored and
marco
committed
Fix invalid pointer arithmetic in Hash (google#1222)
It is UB to exceed the bounds of the buffer when doing pointer arithemetic. That means the following is not a valid bounds check: if (start + 4 <= limit) Because if we were at the end of the buffer, we wouldn't be allowed to add 4 anyway. Instead, this must be written as: if (limit - start >= 4) Basic forms of this issue are flagged by UBSan. If building with -fsanitize=undefined, the following test trips an error: [ RUN ] HASH.SignedUnsignedIssue .../leveldb/util/hash.cc:30:15: runtime error: applying non-zero offset 4 to null pointer SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/local/google/home/davidben/leveldb/util/hash.cc:30:15 in [ OK ] HASH.SignedUnsignedIssue (1 ms) (cherry picked from commit 578eeb7)
1 parent 688561c commit a8844b2

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

util/hash.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ uint32_t Hash(const char* data, size_t n, uint32_t seed) {
2727
uint32_t h = seed ^ (n * m);
2828

2929
// Pick up four bytes at a time
30-
while (data + 4 <= limit) {
30+
while (limit - data >= 4) {
3131
uint32_t w = DecodeFixed32(data);
3232
data += 4;
3333
h += w;

0 commit comments

Comments
 (0)