Skip to content

Fix address bounds check in get_reg_block#2926

Merged
janiversen merged 1 commit intopymodbus-dev:devfrom
foolip:bounds-check
Apr 23, 2026
Merged

Fix address bounds check in get_reg_block#2926
janiversen merged 1 commit intopymodbus-dev:devfrom
foolip:bounds-check

Conversation

@foolip
Copy link
Copy Markdown
Contributor

@foolip foolip commented Apr 23, 2026

Because offset can be negative, register_count <= offset < 0 doesn't work.

It's specifically rt.async_getValues(0x03, 8, 1) that would previously
incorrectly pass the bounds check and return a value (15) due to negative list
indexing. Going even further out of bounds would instead throw an IndexError.

Discovered while SimDevice/SimData for a simple static test server.

Because offset can be negative, register_count <= offset < 0 doesn't work.

It's specifically `rt.async_getValues(0x03, 8, 1)` that would previously
incorrectly pass the bounds check and return a value (15) due to negative list
indexing. Going even further out of bounds would instead throw an IndexError.

Discovered while SimDevice/SimData for a simple static test server.
Copy link
Copy Markdown
Collaborator

@janiversen janiversen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, but needs some small changes.

start_address, register_count, registers, _ = self.block[block_id]
offset = address - start_address
if register_count <= offset < 0 or offset + count > register_count:
if offset < 0 or offset + count > register_count:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be the wrong fix, it would be more logical to test that address >= start_address.

This should also be done in set_values

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean to merge this without any further changes?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, the PR solved the problem which is enough.

@janiversen janiversen merged commit 8a97aa0 into pymodbus-dev:dev Apr 23, 2026
34 of 35 checks passed
@foolip foolip deleted the bounds-check branch April 23, 2026 17:28
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.

2 participants