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

fix(i2c_master): NULL pointer dereference in i2c_master_bus_destroy when i2c_master->base is NULL #14845

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 2 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
15 changes: 10 additions & 5 deletions components/esp_driver_i2c/i2c_master.c
Original file line number Diff line number Diff line change
Expand Up @@ -784,9 +784,12 @@ static esp_err_t i2c_master_bus_destroy(i2c_master_bus_handle_t bus_handle)
{
ESP_RETURN_ON_FALSE(bus_handle, ESP_ERR_INVALID_ARG, TAG, "no memory for i2c master bus");
i2c_master_bus_handle_t i2c_master = bus_handle;
i2c_common_deinit_pins(i2c_master->base);
if (i2c_release_bus_handle(i2c_master->base) == ESP_OK) {
if (i2c_master) {

// Check if i2c_master->base is not NULL before using it
if (i2c_master->base) {
i2c_common_deinit_pins(i2c_master->base);

if (i2c_release_bus_handle(i2c_master->base) == ESP_OK) {
if (i2c_master->bus_lock_mux) {
vSemaphoreDeleteWithCaps(i2c_master->bus_lock_mux);
i2c_master->bus_lock_mux = NULL;
Expand All @@ -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);
Copy link

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.

}

free(i2c_master);
} else {
free(i2c_master);
}

return ESP_OK;
}


static esp_err_t s_i2c_asynchronous_transaction(i2c_master_dev_handle_t i2c_dev, i2c_operation_t *i2c_ops, size_t ops_dim, int timeout_ms)
{
i2c_master_bus_t *i2c_master = i2c_dev->master_bus;
Expand Down