Skip to content
This repository
Browse code

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 2 changed files with 13 additions and 1 deletion. Show diff stats Hide diff stats

  1. +1 1  folly/FBString.h
  2. +12 0 folly/test/FBStringTest.cpp
2  folly/FBString.h
@@ -2304,7 +2304,7 @@ namespace std {
2304 2304 template <>
2305 2305 struct hash< ::folly::fbstring> {
2306 2306 size_t operator()(const ::folly::fbstring& s) const {
2307   - return ::folly::hash::fnv32(s.c_str());
  2307 + return ::folly::hash::fnv32_buf(s.data(), s.size());
2308 2308 }
2309 2309 };
2310 2310 }
12 folly/test/FBStringTest.cpp
@@ -995,6 +995,18 @@ TEST(FBString, testFixedBugs) {
995 995 }
996 996 }
997 997
  998 +
  999 +TEST(FBString, testHash) {
  1000 + fbstring a;
  1001 + fbstring b;
  1002 + a.push_back(0);
  1003 + a.push_back(1);
  1004 + b.push_back(0);
  1005 + b.push_back(2);
  1006 + std::hash<fbstring> hashfunc;
  1007 + EXPECT_NE(hashfunc(a), hashfunc(b));
  1008 +}
  1009 +
998 1010 int main(int argc, char** argv) {
999 1011 testing::InitGoogleTest(&argc, argv);
1000 1012 google::ParseCommandLineFlags(&argc, &argv, true);

0 comments on commit 0578370

Please sign in to comment.
Something went wrong with that request. Please try again.