-
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
fix(zigbee): memory leak issue with malloc #11196
Conversation
👋 Hello SuGlider, we appreciate your contribution to this project! 📘 Please review the project's Contributions Guide for key guidelines on code, documentation, testing, and more. 🖊️ Please also make sure you have read and signed the Contributor License Agreement for this project. Click to see more instructions ...
Review and merge process you can expect ...
|
Memory usage test (comparing PR against master branch)The table below shows the summary of memory usage change (decrease - increase) in bytes and percentage for each target.
Click to expand the detailed deltas report [usage change in BYTES]
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
VLAs like char zb_name[name_length + 2]
are considered Undefined Behavior in C++ (although GCC supports it as an extension).
Better just to add the missing free
to avoid the memory leak.
Test Results 76 files 76 suites 12m 48s ⏱️ Results for commit d09c3ee. ♻️ This comment has been updated with latest results. |
In such case, using |
let's go with C++ way. Safer. |
@P-R-O-C-H-Y - I can't test it here to make sure that it works. Please test it using HA and the Zigbee dongle. Thanks! |
Reviewing it over again... code change is wrong. |
Sorry guys. Current code is 100% fine. No issues. My bad. |
if (_read_manufacturer != nullptr) { | ||
free(_read_manufacturer); | ||
} | ||
free(_read_manufacturer); // CPP tests it for nullptr |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what does the comment mean?
char *zb_name = new char[name_length + 2]; | ||
char *zb_model = new char[model_length + 2]; | ||
std::vector<char> zb_name(name_length + 2); | ||
std::vector<char> zb_model(model_length + 2); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what will happen if there was not enough memory?
zb_manufacturer[zbstr->len] = '\0'; | ||
log_i("Peer Manufacturer is \"%s\"", zb_manufacturer.data()); | ||
free(_read_manufacturer); // Free any previously allocated memory | ||
_read_manufacturer = strdup(zb_manufacturer.data()); // Duplicate the information for persistent storage |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
strdup
will return NULL if there was not enough memory
Description of Change
In
libraries/Zigbee/src/ZigbeeEP.cpp
file,void ZigbeeEP::zbReadBasicCluster(const esp_zb_zcl_attribute_t *attribute)
has a memory leak due to amalloc()
with nofree()
.The same is applied to
bool ZigbeeEP::setManufacturerAndModel(const char *name, const char *model)
with necessary NULL test ofbasic_cluster
.Changed code to use
std::vector<char>
whenever possible.Tests scenarios
CI only.
Related links
None