Skip to content

Commit fdf8599

Browse files
committed
Fix a crash in String::changeBuffer()
Calling String::reserve() causes a crash if String object was in invalidated state. Per the comment on the method's declaration in ESP_SSD1306.h, This method was supposed to recover invalidated strings. This change fixes the edge case bug in String::changeBuffer() which is the root cause of the crash exposed from String::reserve(). Following test code was used to reproduce the problem and also to validate the fix: String result; while(true){ char c = 'A'; result += c; // the loop will cause malloc() to fail at some point. if (result.c_str()==0) { Serial.println("String INVALIDATED!!!!!"); result.reserve(0); // before fix, this would crash. Serial.println("Trying to empty...."); result=""; Serial.println("Emptied!!!!"); break; } }
1 parent f28c5be commit fdf8599

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

Diff for: cores/esp8266/WString.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,11 @@ unsigned char ICACHE_FLASH_ATTR String::changeBuffer(unsigned int maxStrLen) {
156156
char *newbuffer = (char *) malloc(newSize);
157157
if(newbuffer) {
158158
memset(newbuffer, 0, newSize);
159-
memcpy(newbuffer, buffer, len);
160159
if (buffer)
160+
{
161+
memcpy(newbuffer, buffer, len);
161162
free(buffer);
163+
}
162164
capacity = newSize - 1;
163165
buffer = newbuffer;
164166
return 1;

0 commit comments

Comments
 (0)