Skip to content

fix(entities): hold every entity type to its exact data-stream boundary (2262/116/0 → 2273/96/9) - #69

Merged
DrunkOnJava merged 1 commit into
mainfrom
fix/exact-boundary-everywhere
Aug 30, 2026
Merged

DrunkOnJava merged 1 commit into
mainfrom
fix/exact-boundary-everywhere

Conversation

@DrunkOnJava

@DrunkOnJava DrunkOnJava commented Aug 30, 2026 •

Copy link
Copy Markdown
Owner

What this does

Until now only the entity types that had to know about the R2007+ split streams asserted anything about where their fields end. Everything else decoded with no boundary check at all — the POLYLINE family, MESH, IMAGE, WIPEOUT, VIEWPORT, LEADER, MLINE, the swept/lofted/extruded/revolved surfaces, RAY, XLINE, POINT, CIRCLE, ARC, LINE, ELLIPSE, SOLID, TRACE, ENDBLK, BLOCK — and on the R2000/R2004 band, every entity type. Their zero error count was a property of dispatch.rs, not of the bytes.

Every fixed-code and custom-class entity decoder now runs through entities::dispatch::checked_inline, which requires the field list to end exactly on the record's own data-stream boundary:

Band Boundary Source
R2010 / R2013 / R2018 string-stream start bit, or handle-stream start − 1 when the record carries no strings string_stream::data_field_end
R2000 / R2004 RL object-data-size-in-bits from the object prologue RawObject::obj_size_bits
R2007 none — the RL spans data and strings and this crate cannot locate an AC1021 string stream yet (#110) —
R13 / R14 none — the prologue has no size field —

On R2010+ the number of entity types that decode unchecked is zero. Neither unchecked band has a record that reaches a decoder at all.

checked_inline also hands the decoder the record's string reader now, which is what let BLOCK stop reading its name inline; the six R2010+-only special cases in dispatch_split_stream_entity (INSERT, 3DFACE, LWPOLYLINE, 3DSOLID, REGION, BODY) collapsed into the general path, which is identical.

Per-type table

Corpus is the local 19-file samples/ set. "before" is 7b2afe2 (main with #64).

Type Corpus records Checked before Checked after Result
LINE 101 (98 R2018 + 3) no yes delta 0, 101/101
POINT 40 no yes delta 0, 40/40
BLOCK 54 (27 R2018, 9 R2010, 9 R2013, 9 R2004) no yes 45 R2010+ records were reading the TV inline — fixed, delta 0 on all 54
ENDBLK 54 no yes delta 0 — budget is 0 bits on every record
SOLID 14 no yes delta 0, 14/14
CIRCLE 16 (13 R2018 + 3) no yes delta 0, 16/16
ARC 6 (3 R2018 + 3) no yes delta 0, 6/6
ELLIPSE / RAY / XLINE 1 each no yes delta 0
IMAGE (custom class) 1 no yes delta 0
POLYLINE_PFACE 1 no yes field list was wrong (+52) — fixed, delta 0
VIEWPORT 6 no yes new honest error, delta −819 ×6
MESH (custom class) 2 no yes new honest error, delta −2 ×2
LEADER 1 no yes new honest error, delta −12
SEQEND 4 not dispatched yes new, delta 0, 4/4
POLYLINE_3D 1 not dispatched yes new, delta 0
VERTEX_3D 5 not dispatched yes new, delta 0, 5/5
VERTEX_PFACE 5 not dispatched yes new, delta 0, 5/5
VERTEX_PFACE_FACE 2 not dispatched yes new, delta 0, 2/2
MLINE 3 not dispatched yes new, BS→RC one-token fix, delta 0, 3/3
TEXT / MTEXT / ATTRIB / ATTDEF / TOLERANCE / HATCH / MULTILEADER / DIMENSION ×7 / SPLINE / INSERT / LWPOLYLINE / 3DFACE / 3DSOLID / REGION / UNDERLAY 174 yes yes unchanged, still delta 0
SHAPE 1 n/a n/a still Unhandled — 160-bit budget, no field list
POLYLINE_2D, POLYLINE_MESH, VERTEX_2D, VERTEX_MESH, TRACE, OLE2FRAME, CAMERA, SUN, LIGHT, GEODATA, BODY, WIPEOUT, HELIX, the 4 SURFACE variants 0 no yes check wired, unexercised — no corpus record

TOLERANCE is confirmed exact (per #28): all 3 R2018 records go through tolerance::decode_modern_split_stream, delta 0. TEXT's reading is untouched (#62's BD-vs-RD caveat): all 29 records close at delta 0 with a 202-bit budget, which means every one of them sets the DataFlags bits that suppress oblique_angle / rotation_angle / width_factor — so no record arbitrates #62 and it stays open.

New-error census, with evidence

cargo run --release --example probe_decode_errors -- samples/sample_AC1032.dwg:

Type Handles Budget Consumed Delta Why it is not fixed here
VIEWPORT 0x240, 0x245, 0x252, 0x256, 0x267, 0x26B 1125 bits (identical on all six) 266 −819 entities/viewport.rs is an admitted prefix decoder (BD3 center, BD width, BD height). Six records of one identical shape give no variation to separate candidate token sequences with, so no field list is guessed.
MESH 0x343, 0x380 93942 / 92554 93940 / 92552 −2 The two trailing bits read 10 on both records — the zero/default code of BS, BL and BD alike. A zero corroborates nothing, so under the #58 rule this is "documented offsets and stop". The module's older note said "3 bits" — that counted the strings present trailer flag, which is not an entity field; corrected.
LEADER 0x512 581 569 −12 Twelve bits that several continuations of §19.4.19's list would fit (any mix of defaulted BDs and trailing Bs summing to 12), on a single record.

Net effect on the corpus: 9 errored, up from 0. That is the point of #63 — the same nine records "decoded" before, with nothing checking that they had.

Fixes made (each delta 0 + independent value corroboration)

BLOCK — the TV is in the string stream on R2010+. Budget measured with the new examples/probe_entity_budgets.rs: 0 bits on 27/27 R2018 records and 3/3 on each of arc_2010.dwg / arc_2013.dwg. Reading the name inline overran on all 33 (deltas +58 … +266). Corroboration: the names now decode as *Model_Space, *Paper_Space0, _ArchTick, *D3, MyBlock — matching the BLOCK_RECORD entries they pair with, which come from a different stream. The 9 R2004 records spend 114-122 bits there (inline BS length + NUL-terminated name) and close on their RL with the unchanged inline reading.

POLYLINE_PFACE — BS, BS, BL in 30 bits, not 5×BS + 2 inline H. Handle 0x422, budget 30 bits, probe_entity_field_list … BS,BS,BL → 5, 2, 7, delta 0. Corroboration: the file carries exactly 5 VERTEX_PFACE (0x423..0x427) and 2 VERTEX_PFACE_FACE (0x428, 0x429) records, summing to the BL 7 owned-object count. The two inline H reads were the bulk of the +52 overrun — an object reference never occupies data-stream bits from R2000 on.

MLINE — num_lines is an RC. With BS all three records (0x38B, 0x3A5, 0x3A6) decode lines = 521, verts = -11716 and die. One token changed; all three land at delta 0 with num_lines = 2, num_verts 8/2/2, justification Zero/Top/Zero and scale 1.5 / 2.13 / 1.598 — plausible values where the old reading produced a negative count.

Newly decoded types (previously Unhandled)

Handles 0x422..0x431 of sample_AC1032.dwg are contiguous and read back as one self-consistent chain, which is the corroboration for all five at once:

0x422  POLYLINE_PFACE     BS 5, BS 2, BL 7            30 bits,  delta 0
0x423  VERTEX_PFACE       RC 0xC0, 3BD               142 bits   ┐ 5 vertices
 ...                                                             ┘
0x428  VERTEX_PFACE_FACE  BS [ 1, 2, 3, -4]           48 bits   ┐ 2 faces
0x429  VERTEX_PFACE_FACE  BS [-1, 4, 5,  0]           40 bits   ┘
0x42A  SEQEND             (no fields)                  0 bits
0x42B  POLYLINE_3D        RC 0, RC 0, BL 5            26 bits,  delta 0
0x42C  VERTEX_3D          RC 0x20, 3BD          142/206 bits    ┐ 5 vertices
 ...                                                             ┘
0x431  SEQEND             (no fields)                  0 bits

Every declared count matches the records that follow it. The face indices are all inside the mesh's declared 1..=5 range, with the negative-index invisible-edge convention and a 0 terminating the three-corner face. The flag bytes name themselves against the flag table already in entities/vertex.rs: 0x20 is "3D polyline vertex", 0xC0 is "polyface mesh vertex" + "3D polygon mesh vertex". None of that comes from the bit budget.

BS and BL are indistinguishable for a value below 256 (both spend 01 plus one byte), so the owned-object counts are read as the BL tables::block_record already uses for the same purpose; only the width and the value are claimed.

The three deliberately-undetermined readings (recorded, unchanged)

Gathered into one table in ARCHITECTURE §7i, plus MESH's new one:

Reading Undetermined What would settle it
MULTILEADER's 17 bits MC + B vs B + RS vs B + two RCs; the MC holds only 274/530/786 across 15 records and the B is true on all a record whose value exceeds RS range, or whose B is false
UNDERLAY clip-vertex encoding 2BD vs 2RD — the one corpus record has a zero clip-vertex count so no vertex is ever read any UNDERLAY with a clip boundary
LWPOLYLINE 0x200 not one of §20.4.85's presence bits; every record carrying it also carries no optional field, so it survives either reading a record with 0x200 set and an optional field present
MESH's trailing 10 (new) BS 0, BL 0, BD 0.0 and "two spare bits" encode identically a MESH whose trailing value is non-zero

Also in this change

  • POLYGON_MESH stops reading two H handles from the data stream. No corpus record exercises this type, so its list stays otherwise unverified and the module now says so; the removal is justified by the crate-wide invariant, not by a measurement of this type.
  • examples/probe_entity_budgets.rs (new) — prints, for every entity record, the bit budget its field list has to fill. This is the tool the whole PR was derived from; probe_decode_errors.rs only shows records that already error, which is no help for a type with no decoder at all.
  • examples/dump_decoded_entities.rs prints the six new variants.

Measured coverage

Version Files Decoded Skipped Errored Ratio
R2004 (AC1018) 3 582 → 582 15 → 15 0 → 0 97.5 % → 97.5 %
R2010 (AC1024) 3 531 → 531 12 → 12 0 → 0 97.8 % → 97.8 %
R2013 (AC1027) 3 384 → 384 12 → 12 0 → 0 97.0 % → 97.0 %
R2018 (AC1032) 1 765 → 776 77 → 57 0 → 9 90.9 % → 92.2 %
Aggregate 19 2262 → 2273 116 → 96 0 → 9 95.1 % → 95.6 %

The three unchanged slices are the interesting ones: their counts did not move, but every entity record in them is now checked where none was before, and every one passes. The CI coverage-smoke corpus (tests/fixtures/canonical) stays at 20/0/0/100 %.

Gate

cargo fmt --all -- --check, cargo clippy --all-targets --all-features -- -D warnings, cargo test --release (751 lib + all integration suites green — the canonical-corpus pins are unchanged, the synthetic fixtures already satisfied the new boundary check), cargo +1.85.0 test --no-run, cargo deny check all, RUSTDOCFLAGS='-D warnings' cargo doc --no-deps --all-features — all pass.

Follow-ups


Clean-room declaration. I certify that:

  1. All code and tests in this PR were written from scratch by me.
  2. I consulted only sources listed as allowed in CLEANROOM.md. In particular, I did not consult Autodesk SDK source, ODA SDK (Drawings SDK / Teigha) source, LibreDWG source, decompilations of Autodesk or ODA binaries, or any copyleft-licensed DWG implementation code. Every field list added or corrected here was derived from the corpus bytes: the record's own data-stream boundary, the values that land on it, and the object stream's own counts. No outside vocabulary was needed — flags, vertex_count, face_count, num_owned_objects, location and vertex_indices are the names already used by the sibling modules in this crate.
  3. Where this PR cites a reference (ODA spec section, measured offsets), the citation is in the diff as a doc comment.
  4. I have never had access to any of the forbidden sources.

Closes #63


Note

Medium Risk
The change refactors the central entity dispatch path for every type and alters reported decode success (nine new errors), so regressions in geometry or coverage are possible despite added tests.

Overview
This PR routes all fixed-code and custom-class entity decoders through entities::dispatch::checked_inline, so field lists must end exactly on each record’s data-stream boundary (R2010+ string-stream start or handle trailer; R2000/R2004 RL object-data-size). R2010+ no longer has any entity type that decodes without that check.

Fixes with delta 0: BLOCK reads its name from the string stream on R2010+ (was inline TV); POLYLINE_PFACE is BS/BS/BL counts without inline handles; MLINE uses RC for num_lines instead of BS. New decoders: SEQEND, POLYLINE_3D, VERTEX_3D/MESH/PFACE, VERTEX_PFACE_FACE. POLYGON_MESH drops two inline H reads from the data stream (same rule as polyface).

Intentional errors (9 records): VIEWPORT (−819 bits, prefix decoder), MESH (−2 trailing bits), LEADER (−12 bits)—documented, not guessed. Corpus coverage moves from 2262 / 116 / 0 to 2273 / 96 / 9 (95.1% → 95.6%); R2004–R2013 counts unchanged but every entity there is now boundary-checked.

Adds examples/probe_entity_budgets.rs and extends dump_decoded_entities for the new variants; docs (ARCHITECTURE §7i, README, STATUS, CHANGELOG) reflect the new honesty model.

Reviewed by Cursor Bugbot for commit 376905d. Configure here.

Until now only the entity types that had to know about the R2007+ split
streams asserted anything about where their fields end. The POLYLINE
family, MESH, IMAGE, WIPEOUT, VIEWPORT, LEADER, MLINE, the four SURFACE
variants, RAY, XLINE, POINT, CIRCLE, ARC, LINE, ELLIPSE, SOLID, TRACE,
ENDBLK and BLOCK decoded with no boundary check at all — and on the
R2000/R2004 band, so did every entity type. Their zero error count was a
property of the dispatcher, not of the bytes.

Every fixed-code and custom-class entity decoder now runs through
`checked_inline`, which requires the field list to end exactly on the
record's own data-stream boundary: the string-stream start bit (or the
handle-stream start minus the `strings present` trailer bit) on R2010+,
and the `RL` object-data-size-in-bits on R2000/R2004. On R2010+ no entity
type decodes unchecked any more.

Three field lists the check caught, each fixed to delta 0 with an
independent value corroborating it:

- BLOCK (45 records) read its one TV inline; on R2010+ the name lives in
  the string stream and the record's budget is 0 bits. Names now decode
  as *Model_Space, _ArchTick, MyBlock — matching their BLOCK_RECORDs.
- POLYLINE_PFACE (1) claimed five BS counts and two inline H handles,
  overrunning by 52 bits; the record holds BS 5, BS 2, BL 7 in 30 bits,
  and the file carries exactly 5 VERTEX_PFACE + 2 VERTEX_PFACE_FACE.
- MLINE (3) read num_lines as BS where the record writes one RC.

Five previously-Unhandled types now decode, proved rather than assumed:
SEQEND, POLYLINE_3D, VERTEX_3D/VERTEX_MESH/VERTEX_PFACE and
VERTEX_PFACE_FACE. Handles 0x422..0x431 of sample_AC1032.dwg read back
as a self-consistent chain whose declared counts match the records that
follow them.

Three decoders fail the check and are reported, not patched: VIEWPORT
(6 records, delta -819 — the decoder reads 266 of 1125 bits by design),
MESH (2, delta -2 — two trailing bits reading `10`, the zero code of BS,
BL and BD alike) and LEADER (1, delta -12).

Corpus: 2262 / 116 / 0 / 95.1% -> 2273 / 96 / 9 / 95.6%.

Closes #63
@cursor

cursor Bot commented Aug 30, 2026

Copy link
Copy Markdown

Bugbot couldn't run - usage limit reached

Bugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit.

A user or team admin can review and increase usage limits in the Cursor dashboard.

(requestId: serverGenReqId_a14d1b83-b127-417c-a889-f66cc016c678)

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.

Extend the exact-boundary check (checked_inline) to every remaining entity type — their error count is structurally zero today

1 participant