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 1 commit
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
56 changes: 32 additions & 24 deletions components/esp_driver_i2c/i2c_master.c
Original file line number Diff line number Diff line change
Expand Up @@ -784,40 +784,48 @@ 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) {
if (i2c_master->bus_lock_mux) {
vSemaphoreDeleteWithCaps(i2c_master->bus_lock_mux);
i2c_master->bus_lock_mux = NULL;
}
if (i2c_master->cmd_semphr) {
vSemaphoreDeleteWithCaps(i2c_master->cmd_semphr);
i2c_master->cmd_semphr = NULL;
}
if (i2c_master->event_queue) {
vQueueDeleteWithCaps(i2c_master->event_queue);
}
if (i2c_master->queues_storage) {
free(i2c_master->queues_storage);
}
free(i2c_master->i2c_async_ops);
for (int i = 0; i < I2C_TRANS_QUEUE_MAX; i++) {
if (i2c_master->trans_queues[i]) {
vQueueDelete(i2c_master->trans_queues[i]);

// 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) {

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.

Copy link
Author

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.

Choose a reason for hiding this comment

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

Looks much better!

if (i2c_master->bus_lock_mux) {
vSemaphoreDeleteWithCaps(i2c_master->bus_lock_mux);
i2c_master->bus_lock_mux = NULL;
}
if (i2c_master->cmd_semphr) {
vSemaphoreDeleteWithCaps(i2c_master->cmd_semphr);
i2c_master->cmd_semphr = NULL;
}
if (i2c_master->event_queue) {
vQueueDeleteWithCaps(i2c_master->event_queue);
}
if (i2c_master->queues_storage) {
free(i2c_master->queues_storage);
}
free(i2c_master->i2c_async_ops);
for (int i = 0; i < I2C_TRANS_QUEUE_MAX; i++) {
if (i2c_master->trans_queues[i]) {
vQueueDelete(i2c_master->trans_queues[i]);
}
}
bus_handle = NULL;
}
bus_handle = NULL;
}

free(i2c_master);
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.

}
} 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