From: Philip Pronin Date: Wed, 10 Aug 2016 22:03:37 +0000 (-0700) Subject: fix bug in FBString::find X-Git-Tag: v2016.08.15.00~20 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=28c41287ec1006bac2a465b0244deb1f00607bc0;p=folly.git fix bug in FBString::find Summary: Standard (21.4.7.2 / 1): > Determines the lowest position xpos, if possible, such that both of the following conditions obtain: - pos <= xpos and xpos + str.size() <= size(); - traits::eq(at(xpos+I), str.at(I)) for all elements I of the string controlled by str. The existing logic violates the first requirement for `str.size() == 0` by unconditionally returning `pos`. Reviewed By: ot, Gownta Differential Revision: D3698862 fbshipit-source-id: 9622f1b99b259d2d81ae83795dff9cd94619c725 --- diff --git a/folly/FBString.h b/folly/FBString.h index b3ec8f43..077d1be9 100644 --- a/folly/FBString.h +++ b/folly/FBString.h @@ -1768,11 +1768,12 @@ public: size_type find(const value_type* needle, const size_type pos, const size_type nsize) const { - if (!nsize) return pos; auto const size = this->size(); // 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; + + if (nsize == 0) return pos; // 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 56b6ef0c..c6945a1f 100644 --- a/folly/test/FBStringTest.cpp +++ b/folly/test/FBStringTest.cpp @@ -1267,6 +1267,9 @@ TEST(FBString, testFixedBugs) { auto test2 = "a" + std::move(s2); EXPECT_EQ(2, test2.size()); } + { // D3698862 + EXPECT_EQ(fbstring().find(fbstring(), 4), fbstring::npos); + } } TEST(FBString, findWithNpos) {