diff --git a/cores/esp32/Print.cpp b/cores/esp32/Print.cpp index 65fa1e9da68..76b00aa3ccb 100644 --- a/cores/esp32/Print.cpp +++ b/cores/esp32/Print.cpp @@ -52,19 +52,24 @@ size_t Print::printf(const char *format, ...) va_list copy; va_start(arg, format); va_copy(copy, arg); - size_t len = vsnprintf(NULL, 0, format, copy); + int len = vsnprintf(temp, sizeof(loc_buf), format, copy); va_end(copy); + if(len < 0) { + va_end(arg); + return 0; + }; if(len >= sizeof(loc_buf)){ - temp = new char[len+1]; + temp = (char*) malloc(len+1); if(temp == NULL) { + va_end(arg); return 0; } + len = vsnprintf(temp, len+1, format, arg); } - len = vsnprintf(temp, len+1, format, arg); - write((uint8_t*)temp, len); va_end(arg); - if(len >= sizeof(loc_buf)){ - delete[] temp; + len = write((uint8_t*)temp, len); + if(temp != loc_buf){ + free(temp); } return len; }