Skip to content
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

[libs] Fix mDNS string memory corruption, print error on record add failure #260

Merged
merged 12 commits into from
Mar 8, 2024
2 changes: 1 addition & 1 deletion cores/common/arduino/libraries/common/mDNS/mDNS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "mDNS.h"

static char *ensureUnderscore(const char *value) {
uint8_t len = strlen(value) + 1;
uint8_t len = strlen(value) + 1 + 1; // 1 for underscore, 1 for null-terminator
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should this be conditional on value[0] == '_' (or !=) like below for that extra 1?
Granted malloc'ing 1 extra byte isnt the end of the world, and it's free'd relatively soon after anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, we can save one byte this way.
Anyway, I can see a lot of memory allocations there, maybe some of them could be avoided... Like - when there's no need for underscore, why duplicating the string at all?

Copy link
Collaborator

@Cossid Cossid Mar 1, 2024

Choose a reason for hiding this comment

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

yeah, if (value[0] == '_') return value; seems like the better optimization.
Edit: but then the free in a different function becomes more difficult, doesn't it?

char *result = (char *)malloc(len);
result[0] = '_';
strcpy(result + 1, value + (value[0] == '_'));
Expand Down
Loading