Repository navigation
Server: answer ILLEGAL_VALUE for out-of-range read counts - #3066
Merged
janiversen merged 7 commits intoOct 19, 2026
Merged
janiversen merged 7 commits into
janiversen merged 7 commits into
Conversation
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.
Problem
When a client sends a read request (FC1/FC2/FC3/FC4) whose quantity exceeds the MODBUS maximum (2000 bits, 125 registers), the pymodbus server answers
0x01 ILLEGAL FUNCTIONinstead of the specification-required0x03 ILLEGAL DATA VALUE.Reproduced on the wire (FC3, count=126): the response is
... 01 83 01; the expected response is... 01 83 03.Root cause
verifyCount(MAX_COUNT)was added tox_decode()by #2997. It raisesValueError, whichDecodePDU.decode()catches and converts intoModbusIOException;ServerRequestHandlerthen answersILLEGAL_FUNCTION. The count never reachesdatastore_update(), the layer that can returnILLEGAL_VALUE.Fix
Finish the pattern established for FC23 writes by #3016 (see
test_readwrite_decode_write_count_above_limit):x_decode()parses;datastore_update()validates and returnsExceptionResponse(fc, ExcCodes.ILLEGAL_VALUE).verifyCount()from FC1, FC2, FC3, FC4 and from the FC23read_count.if not 1 <= count <= MAX_COUNT: return ExceptionResponse(self.function_code, ExcCodes.ILLEGAL_VALUE)at the top of each readdatastore_update, before any datastore access.x_encode()keeps itsverifyCount()— API misuse still raises.write_countbehaviour and allMAX_COUNTconstants are unchanged.Tests
fc | 0x80with exception0x03, and the datastore is not queried (assert_not_awaited); a count exactly atMAX_COUNTis accepted; decode accepts over-max frames.... 01 83 03; reverting the fix reproduces... 01 83 01.Out of scope
FC15 (
WriteMultipleCoilsRequest) has the same class of decode-time count bug and will be addressed in a follow-up.