Fix non-deterministic convert_from_registers mutating the caller's registers list#2951
Open
gaoflow wants to merge 1 commit into
Open
Fix non-deterministic convert_from_registers mutating the caller's registers list#2951gaoflow wants to merge 1 commit into
gaoflow wants to merge 1 commit into
Conversation
The STRING/BITS branch of convert_from_registers reversed the registers list in place when word_order="little", corrupting the caller's input so a second identical call decoded garbage (non-deterministic output). The numeric branch already avoids this by reversing a slice copy; reverse a copy here too so decoding is side-effect free.
janiversen
requested changes
Jun 30, 2026
| byte_list = bytearray() | ||
| if word_order == "little": | ||
| registers.reverse() | ||
| # Reverse a copy; the numeric branch below also works on a |
Collaborator
There was a problem hiding this comment.
No need for this comment, it does not provide more information than the the code
| ): | ||
| """convert_from_registers must not mutate input and stay deterministic. | ||
|
|
||
| Regression: for STRING/BITS with word_order="little" the registers list |
Collaborator
There was a problem hiding this comment.
We do not explain bugs in comments! doing that would make the comments larger than the code and with nothing more than historic value.
Seems to continue, please remove all comments that either only have historic value, or explain what the code does without adding value.
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.
The bug
ModbusClientMixin.convert_from_registers(..., word_order="little")reverses the caller'sregisterslist in place forSTRING/BITS, so the input is corrupted and a second identical call returns garbage — the decode is non-deterministic:The same happens for
BITSonce the value spans more than one register. The numeric types (INT*/UINT*/FLOAT*) are not affected — they already decode without mutating the input.Why this is a bug
regs = registers[i:i+data_len]) before reversing, so it never touches the input; only thedata_len == 0(STRING/BITS) branch reverses the original.registers.Root cause
pymodbus/client/mixin.py, in thedata_len == 0branch ofconvert_from_registers:list.reverse()mutates in place. The fix reverses a copy instead, mirroring what the numeric branch below already does:Tests
Added
test_client_mixin_convert_from_registers_no_mutationtotest/client/test_client.py, parametrized overSTRING/ multi-registerBITS/INT32×big/little. It asserts the input list is unchanged after decoding and that two consecutive identical calls return equal results. The threelittleSTRING/BITS cases fail ondevand pass with the fix; the rest (and the existing convert tests) stay green.The existing
test_client_mixin_convertdidn't catch this because it builds a fresh reversed list and callsconvert_from_registersonly once per input.