Repository navigation
LegacyRxGeneric: "Invalid legacy Rx attribute header" when the last attribute's header is cut short - #63
Merged
Conversation
The last attribute of a legacy Rx record may declare 4 bytes more than the record holds; LegacyRxGeneric already accepts this when the missing bytes fall inside the value. With a value shorter than 4 bytes (e.g. an empty CommPath 0x6A of length 2) the shortfall reaches into the attribute header and the header check raised before the tolerance applied, so load_acd aborted for the whole project. Read the last attribute's header from a zero-padded copy and let the existing value_end == len(raw) + 4 check decide. Adds a regression test.
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.
Cause:
LegacyRxGeneric.__init__(acd/record/rx.py:60-76) already accepts that the last attribute declares 4 bytes more than the record holds (value_end == len(raw) + 4), like the LastAttributeRecord of the generated layout. It only accepts this when the missing bytes fall inside the value. When the last attribute's value is shorter than 4 bytes, the shortfall reaches into the attribute header. The checkoffset + 8 > len(raw)then raises before the existing tolerance applies.Impact:
load_acd/ExportL5x(...).projectaborts for the whole project, because the root controller record (ControllerBuilder.build,elements.py:2885) is affected. No controller, programs, routines or rungs come out. In our archive this hits 7 of 279 projects, all last saved with V20.05 and with a save history back to V10. In each of them the last attribute of the controller record is CommPath0x6Awith declared length 2: 6 bytes are left (id + 2 bytes of the length), 4 bytes short,412 + 8 + 2 = 422 = len(raw) + 4for a 418-byte record withu32@24 = len - 24.Affected versions: 0.3a2 (latest release) and main
3a2aa87.acd/record/rx.pyis identical in both.Reproduction: with only placeholder bytes, using the
_legacy_rxhelper fromtest/test_rx.py:ValueError: Invalid legacy Rx attribute header.LegacyRxGenericwhose last extended record is(0x6A, b""). That is what the parser already returns for the same 4-byte shortfall with a longer value, minus the missing bytes.With the change below, all 7 real projects load without other changes, with controller, programs, routines and rungs.
Solution: read the header of the last attribute from a copy padded with 4 zero bytes. The existing
value_end == len(raw) + 4check still decides whether the shortfall is the tolerated one. Anything shorter still raises, and non-last attributes are unchanged. The patch adds a test that fails on main and passes with the change:Not checked:
3a2aa87, the new test fails without the change; with it,test/test_rx.pypasses 20 of 20 and the whole suite run fromtest/passes 135 with 2 skipped.)AI disclosure: An AI coding assistant (Claude) helped find the cause and write the fix and this report. A human checks the report before it is submitted.