Skip to content

Commit

Permalink
#3181 printf double vsnprintf() fix, malloc, va_end (#3184)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
knifter authored and me-no-dev committed Sep 8, 2019
1 parent 07613b3 commit 717ca79
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions cores/esp32/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 717ca79

Please sign in to comment.