-
Notifications
You must be signed in to change notification settings - Fork 7.3k
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(i2c_master): NULL pointer dereference in i2c_master_bus_destroy
when i2c_master->base
is NULL
#14845
base: master
Are you sure you want to change the base?
fix(i2c_master): NULL pointer dereference in i2c_master_bus_destroy
when i2c_master->base
is NULL
#14845
Conversation
👋 Hello pback34, 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 ...
|
i2c_master_bus_destroy
when i2c_master->base
is NULL
i2c_master_bus_destroy
when i2c_master->base
is NULLi2c_master_bus_destroy
when i2c_master->base
is NULL (IDFGH-14024)
i2c_master_bus_destroy
when i2c_master->base
is NULL (IDFGH-14024)i2c_master_bus_destroy
when i2c_master->base
is NULL
i2c_common_deinit_pins(i2c_master->base); | ||
|
||
if (i2c_release_bus_handle(i2c_master->base) == ESP_OK) { | ||
if (i2c_master) { |
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.
this specific check for non-null is redundant at this point since the pointer will have been dereferenced already and has been validated already as part of the first line in the function.
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.
Good catch. Please see if latest looks correct.
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.
Looks much better!
That's a good candidate to use |
@@ -808,16 +811,18 @@ static esp_err_t i2c_master_bus_destroy(i2c_master_bus_handle_t bus_handle) | |||
} | |||
} | |||
bus_handle = NULL; | |||
free(i2c_master); | |||
} else { | |||
free(i2c_master); |
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.
Unless the ESP coding style suggests this, I would refactor the free(i2c_master);
calls into a single statement at the end end remove two else
clauses.
Description:
This pull request addresses a potential crash in the
i2c_master_bus_destroy
function of the ESP-IDF I2C master driver. The issue arises wheni2c_master->base
isNULL
, leading to a NULL pointer dereference and subsequent program crash.Issue Details:
i2c_new_master_bus
, if the bus is not available (e.g., all I2C ports are in use), the functioni2c_acquire_bus_handle
fails.i2c_master->base
remainsNULL
.i2c_new_master_bus
encounters this failure, it callsi2c_master_bus_destroy
to clean up.i2c_master_bus_destroy
function does not check ifi2c_master->base
isNULL
before using it.i2c_common_deinit_pins(i2c_master->base)
andi2c_release_bus_handle(i2c_master->base)
, causing a crash.Proposed Fix:
Add NULL Check:
Introduced a check to verify if
i2c_master->base
is notNULL
before dereferencing it ini2c_master_bus_destroy
.This ensures that functions which require
i2c_master->base
only execute when it's valid.Minimal Changes:
The fix is minimal and preserves the existing logic and flow of the function.
The conditional checks and resource cleanup are maintained as per the original implementation.