Skip to content

Commit 9c4a123

Browse files
authored
String: informative messages on issues (#8821)
Always enable messages when serial debug port is enabled Allocation issue was not reported
1 parent 79dde21 commit 9c4a123

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

cores/esp8266/WString.cpp

+21-5
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,15 @@ bool String::reserve(unsigned int size) {
197197
return false;
198198
}
199199

200+
#ifdef DEBUG_ESP_PORT
201+
static void identifyString (const String& badOne)
202+
{
203+
DEBUGV("[String] '%." STR(OOM_STRING_BORDER_DISPLAY) "s ... %." STR(OOM_STRING_BORDER_DISPLAY) "s': ",
204+
badOne.c_str(),
205+
badOne.length() > OOM_STRING_BORDER_DISPLAY? badOne.c_str() + std::max((int)badOne.length() - OOM_STRING_BORDER_DISPLAY, OOM_STRING_BORDER_DISPLAY): "");
206+
}
207+
#endif
208+
200209
bool String::changeBuffer(unsigned int maxStrLen) {
201210
// Can we use SSO here to avoid allocation?
202211
if (maxStrLen < sizeof(sso.buff) - 1) {
@@ -218,16 +227,19 @@ bool String::changeBuffer(unsigned int maxStrLen) {
218227
}
219228
// Fallthrough to normal allocator
220229
size_t newSize = (maxStrLen + 16) & (~0xf);
221-
#ifdef DEBUG_ESP_OOM
230+
#ifdef DEBUG_ESP_PORT
222231
if (!isSSO() && capacity() >= OOM_STRING_THRESHOLD_REALLOC_WARN && maxStrLen > capacity()) {
223232
// warn when badly re-allocating
224-
DEBUGV("[String] Reallocating large String(%d -> %d bytes) '%." STR(OOM_STRING_BORDER_DISPLAY) "s ... %." STR(OOM_STRING_BORDER_DISPLAY) "s'\n",
225-
len(), maxStrLen, c_str(),
226-
len() > OOM_STRING_BORDER_DISPLAY? c_str() + std::max((int)len() - OOM_STRING_BORDER_DISPLAY, OOM_STRING_BORDER_DISPLAY): "");
233+
identifyString(*this);
234+
DEBUGV("Reallocating large String(%d -> %d bytes)\n", len(), maxStrLen);
227235
}
228236
#endif
229237
// Make sure we can fit newsize in the buffer
230238
if (newSize > CAPACITY_MAX) {
239+
#ifdef DEBUG_ESP_PORT
240+
identifyString(*this);
241+
DEBUGV("Maximum capacity reached (" STR(CAPACITY_MAX) ")\n");
242+
#endif
231243
return false;
232244
}
233245
uint16_t oldLen = len();
@@ -247,6 +259,10 @@ bool String::changeBuffer(unsigned int maxStrLen) {
247259
setBuffer(newbuffer);
248260
return true;
249261
}
262+
#ifdef DEBUG_ESP_PORT
263+
identifyString(*this);
264+
DEBUGV("OOM: %d -> %d bytes\n", isSSO() ? 0: capacity(), newSize);
265+
#endif
250266
return false;
251267
}
252268

@@ -804,7 +820,7 @@ void String::replace(const String &find, const String &replace) {
804820
if (size == len())
805821
return;
806822
if (size > capacity() && !changeBuffer(size))
807-
return; // XXX: tell user!
823+
return;
808824
int index = len() - 1;
809825
while (index >= 0 && (index = lastIndexOf(find, index)) >= 0) {
810826
readFrom = wbuffer() + index + find.len();

0 commit comments

Comments
 (0)