Skip to content

Commit 717ca79

Browse files
knifterme-no-dev
authored andcommitted
#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
1 parent 07613b3 commit 717ca79

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

cores/esp32/Print.cpp

+11-6
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,24 @@ size_t Print::printf(const char *format, ...)
5252
va_list copy;
5353
va_start(arg, format);
5454
va_copy(copy, arg);
55-
size_t len = vsnprintf(NULL, 0, format, copy);
55+
int len = vsnprintf(temp, sizeof(loc_buf), format, copy);
5656
va_end(copy);
57+
if(len < 0) {
58+
va_end(arg);
59+
return 0;
60+
};
5761
if(len >= sizeof(loc_buf)){
58-
temp = new char[len+1];
62+
temp = (char*) malloc(len+1);
5963
if(temp == NULL) {
64+
va_end(arg);
6065
return 0;
6166
}
67+
len = vsnprintf(temp, len+1, format, arg);
6268
}
63-
len = vsnprintf(temp, len+1, format, arg);
64-
write((uint8_t*)temp, len);
6569
va_end(arg);
66-
if(len >= sizeof(loc_buf)){
67-
delete[] temp;
70+
len = write((uint8_t*)temp, len);
71+
if(temp != loc_buf){
72+
free(temp);
6873
}
6974
return len;
7075
}

0 commit comments

Comments
 (0)