Skip to content

Validate process image read request ranges - #160

Merged
kevinherron merged 1 commit into
masterfrom
codex/fix-158-read-range-validation
Aug 7, 2026
Merged

Validate process image read request ranges#160
kevinherron merged 1 commit into
masterfrom
codex/fix-158-read-range-validation

Conversation

@kevinherron

Copy link
Copy Markdown
Contributor

Invalid Modbus read requests could ask for zero items, too many items, or a range past the end of the address space. The built-in process-image services accepted those requests and returned successful responses instead of the protocol-required error.

This change validates read requests before they reach the process image, using the existing range checks to return the appropriate Modbus exception response.

What changed

  • Validate coil and discrete-input read quantities and address ranges.
  • Validate holding-register and input-register read quantities and address ranges.
  • Apply the checks in ReadOnlyModbusServices; ReadWriteModbusServices inherits the corrected behavior.
  • Add boundary coverage for valid maximums, invalid quantities, and address overruns across all four read function codes.

Closes #158.

The built-in read services never invoked the existing ModbusServices range
validators, allowing invalid reads to reach the process image and return
successful responses. Validate ReadOnlyModbusServices read handlers;
ReadWriteModbusServices inherits the corrected behavior.

Fixes #158
@coderabbitai

coderabbitai Bot commented Aug 7, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 935d3635-dfe3-450f-8a89-ade46763d929

📥 Commits

Reviewing files that changed from the base of the PR and between 6ff26c0 and 846a6f3.

📒 Files selected for processing (2)
  • modbus/src/main/java/com/digitalpetri/modbus/server/ReadOnlyModbusServices.java
  • modbus/src/test/java/com/digitalpetri/modbus/server/ReadOnlyModbusServicesTest.java

📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes
    • Read-only Modbus operations now validate requested bit and register ranges before processing requests.
    • Invalid quantities and out-of-range addresses return the appropriate Modbus protocol exceptions instead of being processed.
    • Valid requests at supported maximum quantities continue to work as expected.
  • Tests
    • Added coverage for coils, discrete inputs, holding registers, and input registers, including boundary and invalid-range scenarios.

Walkthrough

ReadOnlyModbusServices now validates request ranges for all four read operations before accessing the process image. The public methods declare ModbusResponseException. Tests cover valid boundaries and expected protocol exceptions.

Changes

Read-only range validation

Layer / File(s) Summary
Service range validation
modbus/src/main/java/com/digitalpetri/modbus/server/ReadOnlyModbusServices.java
All four read methods validate bit or register address and quantity ranges before reading data. Their declarations include ModbusResponseException.
Range validation coverage
modbus/src/test/java/com/digitalpetri/modbus/server/ReadOnlyModbusServicesTest.java
Tests cover maximum valid quantities, invalid quantities, address overruns, and expected Modbus function and exception codes.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: validating process image read request ranges.
Description check ✅ Passed The description accurately explains the range-validation changes, affected services, protocol errors, and test coverage.
Linked Issues check ✅ Passed The changes address issue #158 by validating all four read methods and testing quantity limits and address overruns.
Out of Scope Changes check ✅ Passed The implementation and tests remain within the linked issue scope of validating read request ranges.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@kevinherron
kevinherron marked this pull request as ready for review August 7, 2026 11:54
@kevinherron
kevinherron merged commit 5a5c024 into master Aug 7, 2026
4 checks passed
@kevinherron
kevinherron deleted the codex/fix-158-read-range-validation branch August 8, 2026 13:48
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.

ReadOnlyModbusServices never calls checkBitRange/checkRegisterRange, so out-of-range read requests succeed

1 participant