-
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
Open
pback34
wants to merge
3
commits into
espressif:master
Choose a base branch
from
pback34:fix/i2c_master_bus_destroy_null_bug
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+10
−9
Open
fix(i2c_master): NULL pointer dereference in i2c_master_bus_destroy
when i2c_master->base
is NULL
#14845
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unless the ESP coding style suggests this, I would refactor the |
||
} | ||
} 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; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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!