Commit 28c41287 authored by Philip Pronin's avatar Philip Pronin Committed by Facebook Github Bot 5

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
parent b62a976d
......@@ -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();
......
......@@ -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) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment