Repository navigation
Fix NULL pointer dereference in SCCB accessor functions - #851
Merged
Merged
Conversation
get_handle_from_address() returns NULL when the given slave address isn't registered in devices[] (e.g. after SCCB_Deinit(), or a concurrent deinit racing an in-flight transaction), logging "Device with address %02x not found" as a designed graceful-failure path. All 6 accessor functions (SCCB_Read, SCCB_Write, SCCB_Read16, SCCB_Write16, SCCB_Read_Addr16_Val16, SCCB_Write_Addr16_Val16) dereferenced that return value unconditionally, turning the intended error path into a NULL pointer dereference / crash. Add a NULL check before each dereference, returning the same failure value each function already uses for I2C errors (0 for reads, -1 for writes), consistent with this file's existing error-handling convention.
|
+1 |
|
This pull request appears to be stale. Please close it if its no longer valid. |
me-no-dev
approved these changes
Sep 25, 2026
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
get_handle_from_address()indriver/sccb-ng.creturnsNULLwhen thegiven slave address is not currently registered in the internal
devices[]table, logging
"Device with address %02x not found"— this is a designedgraceful-failure path.
However, all 6 accessor functions that call it dereference the return value
unconditionally, e.g. in
SCCB_Read:i2c_master_dev_handle_tis itself a pointer type(
typedef struct i2c_master_dev_t *i2c_master_dev_handle_t;), so whenget_handle_from_address()returnsNULL, this dereferences a NULL pointer,crashing instead of hitting the intended error path.
Affected functions:
SCCB_Read,SCCB_Write,SCCB_Read16,SCCB_Write16,SCCB_Read_Addr16_Val16,SCCB_Write_Addr16_Val16.How this is reachable
The normal
esp_camera_init()flow always callsSCCB_Probe()/SCCB_Install_Device()(which registers the address) before any of theseaccessors run, so it isn't hit in that path. It is reachable when:
sensor_t *returned byesp_camera_sensor_get()(a documented pattern — seeREADME.mdandexamples/camera_example/main/take_picture.c) and callssensor->set_reg/get_regafteresp_camera_deinit(), sinceSCCB_Deinit()clearsdevices[]/device_countbut the cachedsensor_t*is not invalidated.SCCB_Deinit()racing an in-flightSCCB_Read/SCCB_Writefrom another task (no locking around
devices[]).Fix
Add a NULL check right after each
get_handle_from_address()call, andreturn the same failure value each function already uses on I2C errors
(
0for the read functions,-1for the write functions) — reusing theESP_LOGEthe function already emits. This is a minimal, 6-site hardeningchange consistent with the file's existing error-handling convention; no
behavior changes on the success path.