Skip to content

fix(elements): degrade gracefully on corrupt program Comps record - #55

Open
colinlove wants to merge 1 commit into
hutcheb:mainfrom
colinlove:fix/corrupt-program-comps-record
Open

colinlove wants to merge 1 commit into
hutcheb:mainfrom
colinlove:fix/corrupt-program-comps-record

Conversation

@colinlove

Copy link
Copy Markdown
Contributor

This fix is part of a series found with AI assistance (Claude Code) — see the first PR in this batch for the full disclosure.


One corrupt program comps record aborts the whole project's export (kaitaistruct.EndOfStreamError)

Note: this patch touches the same file as 0004/0006/0007 and adds the
same from loguru import logger as log import each does. If applying more
than one of these to acd/l5x/elements.py at once, keep a single copy of
that import line — see the top-level README's "Applying more than one
patch to the same file" note.

Symptom

kaitaistruct.EndOfStreamError: requested 16842752 bytes, but only 4054 bytes available

raised from deep inside RxGeneric.from_bytes() (via the generated Kaitai
parser reading an AttributeRecord's len_value-prefixed value), aborting
the entire project's export over a single program.

Root cause

One program's comps record (a real, named program — not a deleted/tombstone
record) has a corrupt extended-attribute length prefix partway through its
attribute list: at some AttributeRecord, the len_value field reads back
as 16842752 when only 4054 bytes remain in the buffer. This is a hard
desync — once one length-prefixed field's length is wrong, there's no
reliable way to resync to where the next attribute actually starts, so
this can't be parsed correctly by looking further into the same record. No
second file with the same corruption shape was available to compare
against and confirm a specific misread offset.

This one comps record is read from two independent places:

  1. ProgramBuilder.build(), to build the Program element itself.
  2. _program_schedule_id(), called from ControllerBuilder.build() while
    building a comment_id_to_program lookup used for resolving each Task's
    scheduled-program list.

Both call RxGeneric.from_bytes() on the same corrupt record and both
would raise, so both needed the same treatment.

Fix

Wrap both call sites in try/except Exception, logging a warning naming
the program and degrading gracefully instead of raising:

  • In ControllerBuilder.build()'s program loop: on failure, still append a
    Program for that name, but as an empty placeholder (no routines, no
    tags, no main/fault routine) instead of the fully-built one.
  • In the comment_id_to_program dict-comprehension (converted to an
    explicit loop so each iteration can be individually wrapped): on failure,
    just skip that program's entry — it won't be resolvable as any task's
    scheduled program, which is a far smaller loss than failing the whole
    file's export.

Suggested validation

Only one real-world instance of this exact corruption was available to
test against (see the top-level README's note on why no sample file is
attached). The fix is intentionally conservative — it only catches and
degrades at the two points that actually call into this record's parsing,
so it shouldn't mask unrelated bugs elsewhere in the codebase.

🤖 Generated with Claude Code

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@santiagolgzz

Copy link
Copy Markdown

I ran this against 20 projects (the 7 in resources/, two public projects and 11 private ones). The guard never fired and the output didn't change, so it looks like a safe net to have.

One suggestion: when it does fire, the placeholder looks like an ordinary empty program in the L5X, so whoever opens the file can't tell a program was lost. Could the placeholder carry a description saying so, e.g. "acd-tools: this program's record could not be read"? Studio 5000 shows descriptions, so it would be visible where people actually look.

This branch has not been deployed

No deployments
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