Skip to content

Fix NULL pointer dereference in SCCB accessor functions - #851

Merged
me-no-dev merged 2 commits into
espressif:masterfrom
94xhn:fix-sccb-null-deref
Sep 25, 2026
Merged

me-no-dev merged 2 commits into
espressif:masterfrom
94xhn:fix-sccb-null-deref

Conversation

@94xhn

@94xhn 94xhn commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Problem

get_handle_from_address() in driver/sccb-ng.c returns NULL when the
given slave address is not currently registered in the internal devices[]
table, logging "Device with address %02x not found" — this is a designed
graceful-failure path.

However, all 6 accessor functions that call it dereference the return value
unconditionally, e.g. in SCCB_Read:

i2c_master_dev_handle_t dev_handle = *(get_handle_from_address(slv_addr));

i2c_master_dev_handle_t is itself a pointer type
(typedef struct i2c_master_dev_t *i2c_master_dev_handle_t;), so when
get_handle_from_address() returns NULL, 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 calls SCCB_Probe() /
SCCB_Install_Device() (which registers the address) before any of these
accessors run, so it isn't hit in that path. It is reachable when:

  • An application caches the sensor_t * returned by
    esp_camera_sensor_get() (a documented pattern — see README.md and
    examples/camera_example/main/take_picture.c) and calls
    sensor->set_reg / get_reg after esp_camera_deinit(), since
    SCCB_Deinit() clears devices[] / device_count but the cached
    sensor_t* is not invalidated.
  • A concurrent SCCB_Deinit() racing an in-flight SCCB_Read/SCCB_Write
    from another task (no locking around devices[]).

Fix

Add a NULL check right after each get_handle_from_address() call, and
return the same failure value each function already uses on I2C errors
(0 for the read functions, -1 for the write functions) — reusing the
ESP_LOGE the function already emits. This is a minimal, 6-site hardening
change consistent with the file's existing error-handling convention; no
behavior changes on the success path.

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.
@aremmell

Copy link
Copy Markdown

+1

@github-actions

Copy link
Copy Markdown

This pull request appears to be stale. Please close it if its no longer valid.

@me-no-dev
me-no-dev merged commit 4f11c7f into espressif:master Sep 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants