Skip to content

Commit c18f681

Browse files
committed
Fixed the String::lastIndexOf bug
The `String::lastIndexOf` incorrectly finds character `\0` behind the string size, so extra check was added.
1 parent 0aefc94 commit c18f681

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

Diff for: cores/esp32/WString.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,10 @@ int String::lastIndexOf(char ch, unsigned int fromIndex) const {
677677
wbuffer()[fromIndex + 1] = tempchar;
678678
if(temp == NULL)
679679
return -1;
680-
return temp - buffer();
680+
const int rv = temp - buffer();
681+
if(rv >= len())
682+
return -1;
683+
return rv;
681684
}
682685

683686
int String::lastIndexOf(const String &s2) const {

0 commit comments

Comments
 (0)