-
Notifications
You must be signed in to change notification settings - Fork 7.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Print.printf() always runs vsnprintf twice #3181
Comments
@knifter Please send a PR with this change. |
@knifter I think you also need to call if(len >= sizeof(loc_buf)){
temp = new char[len+1];
if(temp == NULL) {
+++ va_end(arg);
return 0;
} Chuck. |
one more stupid question: I am questioning the use of temp = new char[len+1];
...
delete[] temp; I can't find the rule for when from cplusplus.com delete[] class OBJECTx
{
static void operator delete(void* ptr, std::size_t sz)
{
delete (ptr); // ::operator delete(ptr) can also be used
}
static void operator delete[](void* ptr, std::size_t sz)
{
delete (ptr); // ::operator delete(ptr) can also be used
}
}
OBJECTx x = new OBJECTx[10];
delete[] x; Chuck. |
@stickbreaker likely standard delete is fine here. But likely only because it is a basic char array. A more c++ approach might be do away with allocations entirely and use std::vector<uint8_t> tmp; tmp.resize(len); vsnprintf(tmp.data(),.... |
@atanisoft I don't vote for the Chuck. |
Yeah, there are definitely a few ways to "fix" this and likely the simplest will be actually to switch from new/delete to malloc/free like the rest of the arduino-esp32 code uses (mostly) as well as the proposed fixes above. |
I've created a pull request with above mentioned changes, including:
|
* 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
[STALE_SET] This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. Thank you for your contributions. |
[STALE_DEL] This stale issue has been automatically closed. Thank you for your contributions. |
It seems to me that Print.printf(fmt, ...) is allocating a local buffer but never actually uses it, always running vsnprintf() twice. A buffer of size 64 is allocated but then vsnprintf is called with (NULL, 0, ...) parameters just to calculate the length. Then it always allocates a new buffer to vsnprintf into. I've modified the code in such a way that loc_buf[64] is used at first and if it proofs insufficient, only then allocate another. I think this was the original idea.
Also: vsnprintf() returns int, and negative int for an error. I guess this may wreck havoc in various ways when that gets assigned to size_t (uint) and becomes some (large) positive number.
Have a look at this diff for the problem and the fix:
The text was updated successfully, but these errors were encountered: