Permalink
Browse files

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
  • Loading branch information...
1 parent 1fd9e62 commit 057837098c41af5345cb91b7591846ab7df4e2c1 Wei Xu committed with jdelong Oct 9, 2012
Showing with 13 additions and 1 deletion.
  1. +1 −1 folly/FBString.h
  2. +12 −0 folly/test/FBStringTest.cpp
View
@@ -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);

0 comments on commit 0578370

Please sign in to comment.