Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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...
commit 057837098c41af5345cb91b7591846ab7df4e2c1 1 parent 1fd9e62
Wei Xu authored jdelong committed
Showing with 13 additions and 1 deletion.
  1. +1 −1  folly/FBString.h
  2. +12 −0 folly/test/FBStringTest.cpp
View
2  folly/FBString.h
@@ -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());
}
};
}
View
12 folly/test/FBStringTest.cpp
@@ -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);
Please sign in to comment.
Something went wrong with that request. Please try again.