Skip to content

LegacyRxGeneric: "Invalid legacy Rx attribute header" when the last attribute's header is cut short - #63

Merged
hutcheb merged 1 commit into
hutcheb:mainfrom
arosenberger-ops:fix/legacy-rx-short-header
Nov 3, 2026
Merged

hutcheb merged 1 commit into
hutcheb:mainfrom
arosenberger-ops:fix/legacy-rx-short-header

Conversation

@arosenberger-ops

Copy link
Copy Markdown
Contributor

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 check offset + 8 > len(raw) then raises before the existing tolerance applies.

Impact: load_acd / ExportL5x(...).project aborts 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 CommPath 0x6A with declared length 2: 6 bytes are left (id + 2 bytes of the length), 4 bytes short, 412 + 8 + 2 = 422 = len(raw) + 4 for a 418-byte record with u32@24 = len - 24.

Affected versions: 0.3a2 (latest release) and main 3a2aa87. acd/record/rx.py is identical in both.

Reproduction: with only placeholder bytes, using the _legacy_rx helper from test/test_rx.py:

raw = bytearray(_legacy_rx(attributes=[(0x6E, b"\x01" * 12), (0x6A, b"\x00\x00")]))
del raw[-4:]                                    # the record holds 4 bytes less than declared
struct.pack_into("<I", raw, 24, len(raw) - 24)  # legacy branch of RxGeneric.from_bytes
RxGeneric.from_bytes(bytes(raw))
  • Actual: ValueError: Invalid legacy Rx attribute header.
  • Expected: a LegacyRxGeneric whose 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) + 4 check 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:

--- a/acd/record/rx.py
+++ b/acd/record/rx.py
@@ -58,10 +58,17 @@ class LegacyRxGeneric:
         self.extended_records: List[LegacyAttributeRecord] = []
 
         offset = 32
+        # Like the LastAttributeRecord of the generated layout, the last
+        # attribute declares 4 bytes more than the record holds.  With a short
+        # value (e.g. an empty CommPath 0x6A of length 2) the missing bytes
+        # already cut into the attribute header, so read the header from a
+        # zero-padded copy and let the length check below decide.
+        padded = raw + b"\x00" * 4
         for index in range(self.count_record):
-            if offset + 8 > len(raw):
+            last = index == self.count_record - 1
+            if offset + 8 > len(raw) + (4 if last else 0):
                 raise ValueError("Invalid legacy Rx attribute header")
-            attribute_id, length = struct.unpack_from("<II", raw, offset)
+            attribute_id, length = struct.unpack_from("<II", padded, offset)
             value_end = offset + 8 + length
             if value_end > len(raw):
                 if index == self.count_record - 1 and value_end == len(raw) + 4:
--- a/test/test_rx.py
+++ b/test/test_rx.py
@@ -479,3 +479,19 @@
             TaskBuilder(cursor, 100).build({})
     finally:
         database.close()
+
+
+def test_legacy_rx_last_attribute_short_header():
+    # The last attribute declares 4 bytes more than the record holds. With a
+    # 2-byte value the shortfall already reaches into the attribute header.
+    raw = bytearray(_legacy_rx(attributes=[(0x6E, b"\x01" * 12), (0x6A, b"\x00\x00")]))
+    del raw[-4:]
+    struct.pack_into("<I", raw, 24, len(raw) - 24)
+
+    parsed = RxGeneric.from_bytes(bytes(raw))
+
+    assert isinstance(parsed, LegacyRxGeneric)
+    assert [(a.attribute_id, a.value) for a in parsed.extended_records] == [
+        (0x6E, b"\x01" * 12),
+        (0x6A, b""),
+    ]

Not checked:

  • (Checked since: with pytest in a checkout of 3a2aa87, the new test fails without the change; with it, test/test_rx.py passes 20 of 20 and the whole suite run from test/ passes 135 with 2 skipped.)
  • Projects saved with versions other than those in our archive.
  • Whether the legacy CommPath should be read elsewhere. An empty value now gives no CommPath prefix, the same as an absent attribute.

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.

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.
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