#3181 printf double vsnprintf() fix, malloc, va_end (#3184)

* Use loc_buf for small strings, check for error return from vsnprintf

* cleanup arg when bailing out of new

* Use malloc/free instead of new/delete in printf

* Return actual bytes written in printf

* FIX: write before free
This commit is contained in:
Tijs van Roon 2019-09-08 22:49:32 +02:00 committed by Me No Dev
parent 07613b3158
commit 717ca79ecb

View File

@ -52,19 +52,24 @@ size_t Print::printf(const char *format, ...)
va_list copy; va_list copy;
va_start(arg, format); va_start(arg, format);
va_copy(copy, arg); va_copy(copy, arg);
size_t len = vsnprintf(NULL, 0, format, copy); int len = vsnprintf(temp, sizeof(loc_buf), format, copy);
va_end(copy); va_end(copy);
if(len < 0) {
va_end(arg);
return 0;
};
if(len >= sizeof(loc_buf)){ if(len >= sizeof(loc_buf)){
temp = new char[len+1]; temp = (char*) malloc(len+1);
if(temp == NULL) { if(temp == NULL) {
va_end(arg);
return 0; return 0;
} }
len = vsnprintf(temp, len+1, format, arg);
} }
len = vsnprintf(temp, len+1, format, arg);
write((uint8_t*)temp, len);
va_end(arg); va_end(arg);
if(len >= sizeof(loc_buf)){ len = write((uint8_t*)temp, len);
delete[] temp; if(temp != loc_buf){
free(temp);
} }
return len; return len;
} }