Skip to content
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions cores/esp32/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,25 @@ 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;
if(temp != loc_buf){
free(temp);
}
return len;
return write((uint8_t*)temp, len);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't do it this way, the buffer has already been freed, free() will overwrite the content of temp with the delete chain links.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops... absolutely right, a sec

Copy link
Contributor

@stickbreaker stickbreaker Sep 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could:

if(temp != loc_buf){
     len = write((uint8_t*)temp,len);  
     free(temp);
}
else len=write((uint8_t*)temp,len);
return len;

Copy link
Contributor Author

@knifter knifter Sep 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also want to write(temp) if temp == loc_buf
e: right, too soon. But see commit

}

size_t Print::print(const __FlashStringHelper *ifsh)
Expand Down