Skip to content

Commit dd834b3

Browse files
committed
Ensure that String::setLen() is always after any memory operation
Since `String::setLen()` is now modifying the buffer, this change is required to ensure that the proper buffer is changed.
1 parent 0e55f77 commit dd834b3

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

Diff for: cores/esp32/WString.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ unsigned char String::changeBuffer(unsigned int maxStrLen) {
159159
// Already using SSO, nothing to do
160160
uint16_t oldLen = len();
161161
setSSO(true);
162-
setLen(oldLen);
162+
setLen(oldLen);
163163
return 1;
164164
} else { // if bufptr && !isSSO()
165165
// Using bufptr, need to shrink into sso.buff
@@ -168,8 +168,8 @@ unsigned char String::changeBuffer(unsigned int maxStrLen) {
168168
free(wbuffer());
169169
uint16_t oldLen = len();
170170
setSSO(true);
171-
setLen(oldLen);
172171
memcpy(wbuffer(), temp, maxStrLen);
172+
setLen(oldLen);
173173
return 1;
174174
}
175175
}
@@ -193,8 +193,8 @@ unsigned char String::changeBuffer(unsigned int maxStrLen) {
193193
}
194194
setSSO(false);
195195
setCapacity(newSize - 1);
196-
setLen(oldLen); // Needed in case of SSO where len() never existed
197196
setBuffer(newbuffer);
197+
setLen(oldLen); // Needed in case of SSO where len() never existed
198198
return 1;
199199
}
200200
return 0;
@@ -209,8 +209,8 @@ String & String::copy(const char *cstr, unsigned int length) {
209209
invalidate();
210210
return *this;
211211
}
212-
setLen(length);
213212
memmove(wbuffer(), cstr, length + 1);
213+
setLen(length);
214214
return *this;
215215
}
216216

@@ -219,8 +219,8 @@ String & String::copy(const __FlashStringHelper *pstr, unsigned int length) {
219219
invalidate();
220220
return *this;
221221
}
222-
setLen(length);
223222
memcpy_P(wbuffer(), (PGM_P)pstr, length + 1); // We know wbuffer() cannot ever be in PROGMEM, so memcpy safe here
223+
setLen(length);
224224
return *this;
225225
}
226226

@@ -250,8 +250,8 @@ void String::move(String &rhs) {
250250
setLen(rhs.len());
251251
rhs.setSSO(false);
252252
rhs.setCapacity(0);
253-
rhs.setLen(0);
254253
rhs.setBuffer(nullptr);
254+
rhs.setLen(0);
255255
}
256256
#endif
257257

@@ -827,9 +827,9 @@ void String::trim(void) {
827827
while(isspace(*end) && end >= begin)
828828
end--;
829829
unsigned int newlen = end + 1 - begin;
830-
setLen(newlen);
831830
if(begin > buffer())
832831
memmove(wbuffer(), begin, newlen);
832+
setLen(newlen);
833833
wbuffer()[newlen] = 0;
834834
}
835835

0 commit comments

Comments
 (0)