Skip to content

Fix i2cdev critical bugs#8

Merged
UncleRus merged 8 commits intoesp-idf-lib:mainfrom
quinkq:fix-i2cdev-critical-bugs
Jan 26, 2026
Merged

Fix i2cdev critical bugs#8
UncleRus merged 8 commits intoesp-idf-lib:mainfrom
quinkq:fix-i2cdev-critical-bugs

Conversation

@quinkq
Copy link
Contributor

@quinkq quinkq commented Dec 27, 2025

Fix critical i2cdev bugs

While creating defensive checks for i2cdev_get_shared_handle, I started thinking about existing code, and added some potential safety.

  • Add defensive NULL mutex checks (3 functions) to guard against calling I2C functions before i2cdev_init() (i2c_setup_port, i2c_dev_delete_mutex, i2c_setup_device)
  • Fix 3 possible NULL device pointer crashes in wrapper functions (i2c_dev_read_reg, i2c_dev_write_reg, i2c_dev_probe)
  • Fix reference counting asymmetry causing premature bus deletion when unused devices cleaned up
  • Fix i2cdev_init() re-initialization leak with static guard flag
  • Include my threadsafe version of i2cdev_get_shared_handle() with updated documentation (same as reviewed version in Shared i2c bus handle #6)
  • Fix CMakeLists.txt ESP-IDF 4.x version detection (would be falling through to modern driver due to undefined IDF_VERSION_MAJOR)

Code changes are minimal and version-independent. Tested on: ESP-IDF 5.4
CMakeLists.txt changes target 4.x compatibility but untested on 4.x.

Only handled ESP-IDF 5.x correctly, but failed for ESP-IDF 4.x because,
IDF_VERSION_MAJOR STREQUAL 5 is FALSE for version 4.x,
so it fell through to else and could try to use modern driver components
- fix NULL mutex dereference in 3 functions (i2c_setup_port, i2c_dev_delete_mutex, i2c_setup_device)
- fix NULL device pointer crashes in 3 wrapper functions (i2c_dev_read_reg, i2c_dev_write_reg, i2c_dev_probe)
- fix reference counting asymmetry causing potential premature bus deletion
- fix i2cdev_init reinitialization leak with static guard flag
- fix CMakeLists.txt ESP-IDF version check for 4.x compatibility
  - Remove legacy 'common' submodule
  - Add eil-cmake-utils submodule for cross-version CMake utilities
  - Update CMakeLists.txt to use eil_ci module
  - Required to pass repository structure validation tests
@quinkq
Copy link
Contributor Author

quinkq commented Dec 27, 2025

Added a second commit to migrate from the common submodule to eil-cmake-utils to satisfy the repository structure validation, as in instructions.

@UncleRus UncleRus merged commit 816b9cd into esp-idf-lib:main Jan 26, 2026
57 checks passed
@UncleRus
Copy link
Contributor

@quinkq Thank you!

@UncleRus UncleRus mentioned this pull request Jan 26, 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.

2 participants