From b3cd8a1b13768b387e9e933333788526e548a6ea Mon Sep 17 00:00:00 2001 From: Rajat Goel Date: Tue, 15 Oct 2013 17:25:36 -0700 Subject: [PATCH] Issue with find and npos Summary: I dont know whats the expected behavior but for std::string it seems to work. @override-unit-failures Test Plan: unit-tests [ RUN ] FBString.findWithNpos folly/test/FBStringTest.cpp:1147: Failure Value of: fbstr.find(":", fbstring::npos) Actual: 9 Expected: fbstring::npos Which is: 18446744073709551615 Reviewed By: andrei.alexandrescu@fb.com FB internal diff: D1012870 --- folly/FBString.h | 4 +++- folly/test/FBStringTest.cpp | 5 +++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/folly/FBString.h b/folly/FBString.h index 541baa0f..6f5ea131 100644 --- a/folly/FBString.h +++ b/folly/FBString.h @@ -1794,7 +1794,9 @@ public: const size_type nsize) const { if (!nsize) return pos; auto const size = this->size(); - if (nsize + pos > size) return npos; + // nsize + pos can overflow (eg pos == npos), guard against that by checking + // that nsize + pos does not wrap around. + if (nsize + pos > size || nsize + pos < pos) return npos; // Don't use std::search, use a Boyer-Moore-like trick by comparing // the last characters first auto const haystack = data(); diff --git a/folly/test/FBStringTest.cpp b/folly/test/FBStringTest.cpp index 3e294282..3efd652e 100644 --- a/folly/test/FBStringTest.cpp +++ b/folly/test/FBStringTest.cpp @@ -1139,6 +1139,11 @@ TEST(FBString, testFixedBugs) { } } +TEST(FBString, findWithNpos) { + fbstring fbstr("localhost:80"); + EXPECT_EQ(fbstring::npos, fbstr.find(":", fbstring::npos)); +} + TEST(FBString, testHash) { fbstring a; fbstring b; -- 2.34.1