Fix fbstring hash
authorWei Xu <weixu@fb.com>
Tue, 9 Oct 2012 04:39:44 +0000 (21:39 -0700)
committerJordan DeLong <jdelong@fb.com>
Fri, 12 Oct 2012 04:34:01 +0000 (21:34 -0700)
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

folly/FBString.h
folly/test/FBStringTest.cpp

index 9ac8918ce7e22d14af08bc79ebeb7f8650fb1740..1c8ec1d308a5d9b39233d9411e435461d5d167b9 100644 (file)
@@ -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());
   }
 };
 }
index 428bed28fd33d6e5d55493b918d43ba1375711a7..0e6e57f1b1c6c664b097e282ab0154cc119a68ee 100644 (file)
@@ -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);