mirror of
https://github.com/esp8266/Arduino.git
synced 2025-06-09 03:41:41 +03:00
Reduce stack usage by Print::printf
Print::printf would allocate 1460 bytes on the stack, which in some cases would overflow the stack. Additionally it didn't handle (rare) cases when vsnprintf needed a buffer longer than 1460 bytes. This change makes default stack-allocated buffer 64 bytes long, and checks the result returned by vsnprintf. If a buffer longer than 64 bytes is needed, it is allocated on the heap.
This commit is contained in:
parent
f28c5be479
commit
d891704c1e
@ -47,10 +47,23 @@ size_t ICACHE_FLASH_ATTR Print::write(const uint8_t *buffer, size_t size) {
|
||||
size_t Print::printf(const char *format, ...) {
|
||||
va_list arg;
|
||||
va_start(arg, format);
|
||||
char temp[1460];
|
||||
size_t len = ets_vsnprintf(temp, 1460, format, arg);
|
||||
len = print(temp);
|
||||
char temp[64];
|
||||
char* buffer = temp;
|
||||
size_t len = vsnprintf(temp, sizeof(temp), format, arg);
|
||||
va_end(arg);
|
||||
if (len > sizeof(temp) - 1) {
|
||||
buffer = new char[len + 1];
|
||||
if (!buffer) {
|
||||
return 0;
|
||||
}
|
||||
va_start(arg, format);
|
||||
vsnprintf(buffer, len + 1, format, arg);
|
||||
va_end(arg);
|
||||
}
|
||||
len = write((const uint8_t*) buffer, len);
|
||||
if (buffer != temp) {
|
||||
delete[] buffer;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user