Repository navigation
Refuse a short buffer in every setter - #975
Merged
Merged
Conversation
Slice assignments past the end grow a bytearray, which changes the length written to the PLC or shifts later fields. Every setter now raises ValueError instead, including the ones that raised struct.error or IndexError.
…ffer # Conflicts: # CHANGES.md
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.
Fixes #969.
Many
snap7.utilsetters assign with a slice, so a buffer that's too short silently grows. It then gets written back to the PLC at the wrong length, or the fields after it shift.Every setter now raises
ValueErrorwhen the value doesn't fit, before changing anything. That includesDBandRow, which call these setters with their own buffers.Behaviour changes:
set_lrealraisedstruct.errorhere, andset_string,set_booland the single-byte setters raisedIndexError. They now raiseValueError, so code catching the old exceptions needs updating.set_stringneeds2 + max_sizebytes, since it pads tomax_size.set_wstringneeds room for the header and the value only, because it doesn't pad and its defaultmax_sizeis 16382. A negativebyte_indexisn't covered here.This touches the same line of
set_wcharas #968, so whichever lands second has a small conflict.Checks: full test suite, mypy, ruff and pre-commit pass locally on Windows. Every setter is tested with an exact buffer, one byte short, and offset by one. Not tested against a real PLC.