Skip to content

Commit 219990d

Browse files
committed
ARROW-4501: Fix out-of-bounds read in DoubleCrcHash
1 parent 2b9155a commit 219990d

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

cpp/src/arrow/util/hash-util.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,13 @@ class HashUtil {
133133
}
134134
switch (nbytes) {
135135
case 3:
136-
h1 = HW_crc32_u8(h1, p[3]);
136+
h1 = HW_crc32_u8(h1, p[2]);
137137
// fallthrough
138138
case 2:
139-
h2 = HW_crc32_u8(h2, p[2]);
139+
h2 = HW_crc32_u8(h2, p[1]);
140140
// fallthrough
141141
case 1:
142-
h1 = HW_crc32_u8(h1, p[1]);
142+
h1 = HW_crc32_u8(h1, p[0]);
143143
// fallthrough
144144
case 0:
145145
break;

cpp/src/arrow/util/hashing-test.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,25 @@ TEST(HashingQuality, Strings) {
126126
0.96 * static_cast<double>(2 * values.size()));
127127
}
128128

129+
TEST(HashingBounds, Strings) {
130+
std::vector<size_t> sizes({1, 2, 3, 4, 5, 7, 8, 9, 15, 16, 17, 18, 19, 20, 21});
131+
for (const auto s : sizes) {
132+
std::string str;
133+
for (size_t i = 0; i < s; i++) {
134+
str.push_back(static_cast<char>(i));
135+
}
136+
hash_t h = ComputeStringHash<1>(str.c_str(), str.size());
137+
int different = 0;
138+
for (char i = 0; i < 120; i++) {
139+
str[str.size() - 1] = i;
140+
if (ComputeStringHash<1>(str.c_str(), str.size()) != h) {
141+
different++;
142+
}
143+
}
144+
ASSERT_GE(different, 118);
145+
}
146+
}
147+
129148
TEST(ScalarMemoTable, Int64) {
130149
const int64_t A = 1234, B = 0, C = -98765321, D = 12345678901234LL, E = -1, F = 1,
131150
G = 9223372036854775807LL, H = -9223372036854775807LL - 1;

0 commit comments

Comments
 (0)