Skip to content

Fix String::replace() #5897

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 20, 2019
Merged

Conversation

earlephilhower
Copy link
Collaborator

@earlephilhower earlephilhower commented Mar 20, 2019

Fixes #5883 and supercedes #5890

The replace() function was using len() while in the middle of buffer
operations. In SSO mode len() is not stored separately and is a call to
strlen(), which may not be legal if you're in the middle of overwriting
the SSO buffer, as was the case in ::replace when the replacement string
was longer than the find string. This caused potential garbage at the
end of the string when accessed. Instead, just cache the length in a
local while doing the operation.

Add in test cases from #5890 as well as some new ones that fail on the
unmodified core.

Fixes esp8266#5883 and supercedes esp8266#5890

The replace() function was using len() while in the middle of buffer
operations.  In SSO mode len() is not stored separately and is a call to
strlen(), which may not be legal if you're in the middle of overwriting
the SSO buffer, as was the case in ::replace when the replacement string
was longer than the find string.  This caused potential garbage at the
end of the string when accessed.  Instead, just cache the length in a
local while doing the operation.

Also make setLength() explicitly write a 0 in the SSO buffer at the
specific offset.  It's probably not needed, but is safe to do and makes
logical sense.

Add in test cases from esp8266#5890 as well as some new ones that fail on the
unmodified core.
@earlephilhower
Copy link
Collaborator Author

Darn it. I did run "make test" and it passed on my box before submitting. Let me check the code once again, there may be other spots where it's iffy.

When pointers are 8 bytes long, the size of a String is larger than 16
chars.  Increase the allocated array we're using in the test to avoid a
"stack smashing" error.

Undo setLen actually writing a 0 in memory when called in SSO mode. The
semantics of setLen are such that it is called as part of a multi-step
process to update a String, and when it is called it does NOT normally
write a 0 to the buffer at the length indicated.  Doing so in SSO mode
and 64b OSes exposed the error, now cleared up.
Just for clarity, manually call the destructor for the Strings() that
are "placement new'd" in the String tests.  It is a no-op for the
existing test, since trhanks to SSO there are no memory allocations, but
will help in case someone adds tests later which include longer strings.
@earlephilhower earlephilhower changed the title Fix String::replace() and clean up SSO setLen Fix String::replace() Mar 20, 2019
TEST_CASE("String SSO handles junk in memory", "[core][String]")
{
// We fill the SSO space with garbage then construct an object in it and check consistency
// This is NOT how you want to use Strings outside of this testing!
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Always nice to see your own code commented with some instruction "This is NOT how you want to use...." ;)

@earlephilhower earlephilhower merged commit 54240d2 into esp8266:master Mar 20, 2019
@earlephilhower earlephilhower deleted the str-repl branch March 20, 2019 13:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants