-
Notifications
You must be signed in to change notification settings - Fork 70
Description
Checklist
- Checked the issue tracker for similar issues to ensure this is not a duplicate
- Read the documentation to confirm the issue is not addressed there and your configuration is set correctly
- Tested with the latest version to ensure the issue hasn't been fixed
How often does this bug occurs?
always
Expected behavior
When starting the Modbus master, the code must not validate uninitialized memory (i.e., it must not check a descriptor table size that has not yet been set). The mbm_param_descriptor_size field should be initialized deterministically or the descriptor check should be skipped if no descriptor table has been registered.
Actual behavior (suspected bug)
mbc_serial_master_start() checks mbm_param_descriptor_size before it is initialized. Since this field contains uninitialized memory at the time of the check, the result is undefined. Sometimes the start completes successfully (if the random memory value happens to be ≥ 1), and sometimes it fails with ESP_ERR_INVALID_ARG. This means the Modbus master startup behavior depends on whatever garbage value was present in that memory location, causing inconsistent and unreliable initialization.
Error logs or terminal output
Steps to reproduce the behavior
- Take the standard Modbus master serial example from ESP-IDF (no modifications).
- Run it through a debugger or add a temporary log file, then stop it inside the mbc_serial_master_start() function.
- Check the value of mbm_opts->mbm_param_descriptor_size.
- Unless mbc_master_set_descriptor() has been called before mbc_master_start(), you will observe that this field contains uninitialized memory.
- In the current API usage model and examples, mbc_master_set_descriptor() is typically called after the Modbus controller has been started. This means that the mbm_param_descriptor_size field may not contain a valid value when the program starts up.
- The initial result depends entirely on the random contents of that memory location because mbc_serial_master_start() performs a descriptor size validation at this stage. Sometimes the validation succeeds and sometimes it fails. This is completely unrelated to the actual Modbus configuration.
Project release version
main
System architecture
Intel/AMD 64-bit (modern PC, older Mac)
Operating system
Linux
Operating system version
openSUSE Leap 15.6
Shell
ZSH
Additional context
In many designs, it is valid and necessary for the Modbus master to start before any descriptor table is defined. The controller should be able to initialize and operate using raw Modbus frames without parameter descriptors.
Currently, the startup procedure is implicitly tied to the state of the descriptor table. Since the descriptor size field is uninitialized at that time, the outcome of startup becomes non-deterministic. The underlying problem is not the use of descriptors themselves, but rather that the controller’s initialization depends on memory that has not been intentionally set.
A cleaner API model would be:
The Modbus master can start independently of the descriptor table.
Descriptor validation would only occur when descriptor-based APIs are invoked.
If no descriptor table has been registered, the relevant functions return a clear error (e.g., ESP_ERR_NOT_SUPPORTED).
This decouples raw Modbus communication from descriptor-based parameter mapping. It supports both usage patterns cleanly and removes the undefined behavior caused by reading uninitialized memory during startup.