Skip to content

Commit

Permalink
Fix fbstring hash
Browse files Browse the repository at this point in the history
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
Wei Xu authored and jdelong committed Oct 12, 2012
1 parent 1fd9e62 commit 0578370
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion folly/FBString.h
Expand Up @@ -2304,7 +2304,7 @@ namespace std {
template <> template <>
struct hash< ::folly::fbstring> { struct hash< ::folly::fbstring> {
size_t operator()(const ::folly::fbstring& s) const { size_t operator()(const ::folly::fbstring& s) const {
return ::folly::hash::fnv32(s.c_str()); return ::folly::hash::fnv32_buf(s.data(), s.size());
} }
}; };
} }
Expand Down
12 changes: 12 additions & 0 deletions folly/test/FBStringTest.cpp
Expand Up @@ -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) { int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv); testing::InitGoogleTest(&argc, argv);
google::ParseCommandLineFlags(&argc, &argv, true); google::ParseCommandLineFlags(&argc, &argv, true);
Expand Down

0 comments on commit 0578370

Please sign in to comment.