Commit 05783709 authored by Wei Xu's avatar Wei Xu Committed by Jordan DeLong

Fix fbstring hash

Summary: '\0' may actually be part of string. We cannot assume its null terminated and should use another form of fnv32.

Test Plan: FBStringTest

Reviewed By: xliux@fb.com

FB internal diff: D595287
parent 1fd9e62b
......@@ -2304,7 +2304,7 @@ namespace std {
template <>
struct hash< ::folly::fbstring> {
size_t operator()(const ::folly::fbstring& s) const {
return ::folly::hash::fnv32(s.c_str());
return ::folly::hash::fnv32_buf(s.data(), s.size());
}
};
}
......
......@@ -995,6 +995,18 @@ TEST(FBString, testFixedBugs) {
}
}
TEST(FBString, testHash) {
fbstring a;
fbstring b;
a.push_back(0);
a.push_back(1);
b.push_back(0);
b.push_back(2);
std::hash<fbstring> hashfunc;
EXPECT_NE(hashfunc(a), hashfunc(b));
}
int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
google::ParseCommandLineFlags(&argc, &argv, true);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment