Skip to content

Fix dangling pointers from temporary c_str() and cleanup safety#31

Merged
cleishm merged 1 commit intomainfrom
fix/dangling-cstr-and-cleanup
Mar 27, 2026
Merged

Fix dangling pointers from temporary c_str() and cleanup safety#31
cleishm merged 1 commit intomainfrom
fix/dangling-cstr-and-cleanup

Conversation

@cleishm
Copy link
Copy Markdown
Owner

@cleishm cleishm commented Mar 27, 2026

Summary

  • Fix undefined behavior: Calling .c_str() on temporary std::string values (from to_string(), std::format(), .message()) and passing the pointer to variadic functions (ESP_LOG*, TEST_ASSERT_EQUAL_STRING) is UB — the temporary can be destroyed before the function reads the pointer. Fixed by storing the string in a local variable first.
  • Null handles after deletion: _delete() / _stop_and_delete() methods in event_loop, i2c master_bus, i2c master_device, task, and timer now set _handle = nullptr after freeing, preventing double-free on repeated calls.
  • Narrow lock scope in i2c: master_device::_try_transmit() / _try_receive() now release the bus lock before calling map_xfer_error(), which does ESP_LOGD with string temporaries.

Across 27 files in 16 components.

… safety

Calling .c_str() on a temporary std::string passed to a variadic function
(ESP_LOG*, TEST_ASSERT_EQUAL_STRING) is undefined behavior — the temporary
may be destroyed before the function reads the pointer. Store the string in
a local variable first.

Also: null handles after deletion in cleanup methods (_delete,
_stop_and_delete) and narrow scoped_lock in i2c master_device to avoid
holding the bus lock during error logging.
@cleishm cleishm merged commit 01d23a2 into main Mar 27, 2026
34 checks passed
@cleishm cleishm deleted the fix/dangling-cstr-and-cleanup branch March 27, 2026 05:31
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.

1 participant