Skip to content

fix(container): a record's MS excludes its own MC, and the class table is not always a live census - #80

Merged
DrunkOnJava merged 3 commits into
mainfrom
fix/underwalk-and-raw-slice
Aug 30, 2026
Merged

DrunkOnJava merged 3 commits into
mainfrom
fix/underwalk-and-raw-slice

Conversation

@DrunkOnJava

@DrunkOnJava DrunkOnJava commented Aug 30, 2026 •

Copy link
Copy Markdown
Owner

Summary

Two container-layer fixes in one pass: the walker's record slice ended one MC width early on every R2010+ record (#77), and the DICTIONARYVAR / CELLSTYLEMAP "under-walk" (#76) turns out not to be an under-walk at all — the files declare instances they do not contain.

Closes #76
Closes #77


#77 — a record's MS excludes its own MC

#75 measured that every inter-record run in AcDb:AcDbObjects equals the width of the preceding record's leading MC handle-stream-size field:

Reading Agreements
run = preceding record's MC width 841 of 841
run = following record's MC width 734 of 841

on sample_AC1032.dwg, with 100 % agreement on arc_2010.dwg, line_2010.dwg and line_2013.dwg. Only 8 of those 916 bytes are zero, so they are content, not padding; 772 one-byte + 70 two-byte MC fields + the 4-byte 0x0dca prologue account for all 916 exactly.

So a record is framed

MS field | MC field | MS bytes of payload | 2-byte CRC

and RawObject::raw is now sized mc_width + size_bytes. size_bytes keeps reporting the MS verbatim (documented), so raw.len() != size_bytes on R2010+.

The + mc_field_bits correction in string_stream::data_section_end was compensating for exactly that short slice and is removed — the rule is now payload_bits - handle_bits, and the two edits cancel to the same bit offset. That is why no decoder changes behaviour.

Two things the short slice was hiding:

  • the sequential (handle-map-less) walk drifted by one MC width per record on R2010+, because pos advanced to the short crc_end. DwgFile::objects() uses that mode;
  • the object stream looked gappy. It is not. Every R2004+ corpus file now tiles exactly:
file before after
sample_AC1032.dwg 1,191,935 / 1,192,851 claimed, 916 unclaimed 1,192,847 / 1,192,851, 4 unclaimed (the 0x0dca prologue), 0 between records
arc_2010.dwg 88,745 / 88,935, 190 unclaimed 88,931 / 88,935, 4 unclaimed, 0 between records
arc_2013.dwg 13,827 / 13,968, 141 unclaimed 13,964 / 13,968, 4 unclaimed, 0 between records
arc_2004.dwg, arc_2007.dwg already exact (no MC on those bands) unchanged

New unit tests in src/object.rs pin the slice width on byte-built records: a one-byte MC (raw.len() == size_bytes + 1, and the second record's stream_offset lands where the first record really ends), a two-byte MC (+ 2), the pre-R2010 case (raw.len() == size_bytes), and mc_field_width itself.

The fixture writer wrote the old rule

examples/build_fixtures emitted MS = payload.len() including the MC, and declared a zero-bit handle stream. The second part mattered more than the first: with handle_bits = 0 the old formula put data_section_end past the end of the record, which returned None and silently disabled every decoder's boundary check on synthetic_2010/2013/2018.dwg. The writer now emits MS = payload.len() - mc_width and writes the §19.1 strings present trailer bit with the MC recording the byte padding that stands in for the handle stream — the same convention string_stream::tests::build_payload uses. Those three fixtures are regenerated; synthetic_2000/synthetic_2004 are byte-identical.


#76 — DICTIONARYVAR / CELLSTYLEMAP are not under-walked

New probe examples/probe_reference_closure runs three measurements that never consult the class table. All three are closed on all nineteen corpus files:

  1. Stream tiling — bytes between two walked records are room an unreferenced record could occupy. Zero on every R2004+ file (after RawObject::raw likely ends 1-2 bytes short: MS excludes the record's own MC handle-stream-size field (841/841 measured) #77).
  2. Reference closure — decode every record's handle stream and resolve each reference. Every hard reference (§2.13 codes 3 and 5) in the corpus resolves. The only unanswered references anywhere are six code-4 soft pointers — the class the spec permits to dangle — from BLOCK_HEADER 0xA0B of sample_AC1032.dwg to 0xD17, 0xD18, 0xD40, 0xD41, 0xD95, 0xD96, in a drawing that leaves 2,841 of the 3,683 handle values in 0x1..=0xE63 unused; the four siblings in that same list that do resolve are INSERT entities. (This refines TABLECONTENT / TABLEGEOMETRY: 5 declared in the class table, 2 walked — is num_objects a live census or a registration counter? #56's "no reference the walk cannot answer", which was measured before the soft/hard split was drawn.)
  3. Owner census — a dictionary key is the only way a dictionary-owned object is reachable, so the key count bounds the population.

Measurement 3 is decisive:

file AcDbVariableDictionary keys DICTIONARYVAR walked declared
arc/circle/line_2004.dwg 10 10 16
arc/circle/line_2007.dwg 6 6 11
arc/circle/line_2010.dwg 5 5 10
arc/circle/line_2013.dwg 5 5 5
sample_AC1032.dwg 11 11 11

Keys and walked records agree on every file — including the two whose class table also agrees. The class table is the outlier, not the walk. arc_2004.dwg's variable dictionary (handle 0x8F) is literally CANNOSCALE, CMLEADERSTYLE, CTABLESTYLE, CVIEWDETAILSTYLE, CVIEWSECTIONSTYLE, LAYEREVAL, LAYERNOTIFY, LIGHTINGUNITS, MSLTSCALE, XCLIPFRAME — ten keys, ten records, sixteen declared.

CELLSTYLEMAP says the same thing more sharply: arc_2010.dwg declares one, contains none, and carries no ACAD_ROUNDTRIP_2008_TABLESTYLE_CELLSTYLEMAP dictionary key for one to hang from. A live census cannot declare an instance with no owner slot. (The 2004 and 2007 files each carry exactly one such key and walk exactly one record, against 5 and 3 declared.)

The class-table parse is not in doubt either — examples/probe_class_layout on arc_2004.dwg: ten records consume 5161 of the declared 5168 bits (7 bits of byte padding), class numbers run 500..509 with no gap, names decode cleanly, and eight of the ten declared counts match the walk exactly (VISUALSTYLE 24, SCALE 17, MATERIAL 3, TABLESTYLE 1, …). A misread num_objects would not agree on eight and diverge on two.

What moved in the ratchet

Nothing was under-walked to recover, so the KNOWN_SHORTFALL counts are unchanged — a fixed under-walk would have moved them, and this was not one. What changed is the classification: tests/class_census.rs and the probe's ALLOWLIST no longer describe DICTIONARYVAR / CELLSTYLEMAP as "an open walker gap, not explained" but as files that over-declare, with the three measurements and the per-band numbers recorded inline. The pins still gate: a drop in any walked figure fails the ratchet, which is what they are for now.

Per-class walked-vs-declared, before → after

Identical, by design — the walk reaches the same records:

file class declared walked before walked after
arc/circle/line_2004.dwg DICTIONARYVAR 16 10 10
arc/circle/line_2004.dwg CELLSTYLEMAP 5 1 1
arc/circle/line_2007.dwg DICTIONARYVAR 11 6 6
arc/circle/line_2007.dwg CELLSTYLEMAP 3 1 1
arc/circle/line_2010.dwg DICTIONARYVAR 10 5 5
arc/circle/line_2010.dwg CELLSTYLEMAP 1 0 0
sample_AC1032.dwg TABLECONTENT 5 2 2
sample_AC1032.dwg TABLEGEOMETRY 5 2 2

Every other class on every other file reaches its declared count exactly, on both sides of the change.


Coverage — byte-identical

cargo run --release --example coverage_report -- <19-file corpus>, on the merged tree (origin/main is 6cc9b13, no merge needed):

before after
decoded 3695 3695
skipped 741 741
errored 9 9
walker skips 0 0
ratio 83.1 % 83.1 %

Per-version rows are unchanged too (R2004 97.5, R2007 82.8, R2010 97.8, R2013 97.0, R2018 92.2, R2000 84.4, R14 48.6). diff of the two reports is empty. Handle-map self-check stays 0 mismatches; every handle-map entry still resolves to a record.

Test plan

  • cargo fmt --all
  • cargo clippy --all-targets --all-features -- -D warnings
  • cargo test --release — 19 test binaries, all green (770 lib tests + integration)
  • cargo deny check all
  • RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features
  • cargo +1.85.0 test --no-run (MSRV, no let-chains)
  • tests/class_census.rs ran against the real 19-file corpus (not skipped) and passes
  • New unit tests on byte-built records pin the slice width; every new pub fn carries a /// line
  • Coverage re-measured on the corpus, before/after attached above
  • CI gains probe_reference_closure over the canonical fixtures alongside the existing census gate

Source-provenance declaration

By submitting this PR I certify that my contribution does not incorporate executable code from any source whose license is incompatible with Apache-2.0. Specifically:

  • No Autodesk DWG SDK source (RealDWG, ObjectARX, ObjectDBX) or NDA-protected Autodesk documentation was consulted or imported.
  • No Open Design Alliance Teigha / Drawings SDK source was consulted or imported.
  • No GPL-licensed DWG implementation source (LibreDWG, etc.) was consulted or imported.

This work is based on the ODA's freely-redistributable Open Design Specification for .dwg files (v5.4.1) and on direct byte observation of DWG files produced by publicly available CAD software. No third-party source or comments were read. See CLEANROOM.md.


Note

Medium Risk
Changes how every R2010+ object record is sliced in the core walker; impact is broad but mitigated by unchanged decode coverage, new closure probes in CI, and unit tests on record framing.

Overview
Fixes R2010+ object record framing (#77): the walker's payload slice was one leading MC (handle-stream size) byte short, so RawObject::raw is now mc_width + size_bytes while size_bytes still reflects the on-disk MS. The compensating + mc_field_bits in string_stream::data_section_end is removed (trailer_end = payload_bits - handle_bits), so split-stream bit offsets stay the same but sequential walks and stream tiling are correct—R2004+ corpus files show zero bytes between records after the fix.

Adds probe_reference_closure and a CI gate that checks stream tiling and hard handle-reference closure (without using the class table). DICTIONARYVAR / CELLSTYLEMAP shortfalls are re-documented as over-declared num_objects, not missing walk records; ratchet pins in tests/class_census.rs keep the same counts.

build_fixtures now emits MS = payload.len() - mc_width, writes the §19.1 strings-present trailer, and regenerates R2010/R2013/R2018 canonical fixtures so decoder boundary checks work again.

Reviewed by Cursor Bugbot for commit fa04f18. Configure here.

…e is not always a live census

#77 — the walker's raw slice ended one MC width short on every R2010+
record. #75 measured that each inter-record run in AcDb:AcDbObjects
equals the width of the *preceding* record's leading MC handle-stream-
size field (841/841 on sample_AC1032.dwg, 100% on three more files,
against 734/841 for the following-record reading, with only 8 of the 916
bytes zero). A record is framed `MS | MC | MS bytes | CRC`, so
RawObject::raw is now sized mc_width + size_bytes; size_bytes still
reports the MS verbatim.

string_stream::data_section_end drops its `+ mc_field_bits` correction,
which existed only to compensate for the short slice — the rule is now
`payload_bits - handle_bits` and the two edits cancel to the same bit
offset. Coverage over the 19-file corpus is byte-identical before and
after: 3695 / 741 / 9 / 83.1%.

Two things the short slice hid: the sequential (handle-map-less) walk
drifted by one MC width per record on R2010+, and the object stream
looked gappy. Every R2004+ corpus file now tiles exactly — sample_
AC1032.dwg goes from 916 unclaimed bytes to the 4-byte 0x0dca prologue,
with zero bytes between records.

examples/build_fixtures writes the same rule, and now also emits the
§19.1 "strings present" trailer bit with the MC recording the padding
that stands in for the handle stream; declaring a zero-bit handle stream
put the data boundary past the end of the record, silently disabling
every decoder's boundary check on the three R2010+ fixtures. Those are
regenerated.

#76 — DICTIONARYVAR / CELLSTYLEMAP are not under-walked. The new
examples/probe_reference_closure runs three measurements that never
consult the class table, and all three are closed on all 19 files:
records tile the section with zero bytes between them; every hard
handle reference (§2.13 codes 3 and 5) resolves, the only unanswered
references in the corpus being six code-4 soft pointers from
BLOCK_HEADER 0xA0B of sample_AC1032.dwg to erased INSERTs; and the
AcDbVariableDictionary, sole owner of a drawing's DICTIONARYVAR
objects, holds exactly as many keys as the walk finds records on every
file — 10/6/5 on the R2004/R2007/R2010 bands that declare 16/11/10, and
5 and 11 on the two files whose class tables agree with the walk.
arc_2010.dwg settles CELLSTYLEMAP: it declares one, contains none, and
has no ACAD_ROUNDTRIP_2008_TABLESTYLE_CELLSTYLEMAP key for one to hang
from. The class parse is not in doubt — arc_2004.dwg's ten records
consume 5161 of the declared 5168 bits and eight of ten counts match
the walk exactly.

The KNOWN_SHORTFALL pins keep their counts (nothing was under-walked to
recover) and are re-documented as files that over-declare rather than as
an open walker gap; a drop in any walked figure still fails the ratchet.
CI gains the closure probe over the canonical fixtures.

Closes #76
Closes #77
@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_4c222b80-a4b5-4a8b-afa7-2cbcdf24a5f5)

@DrunkOnJava
DrunkOnJava merged commit 44cc130 into main Aug 30, 2026
19 checks passed
@DrunkOnJava
DrunkOnJava deleted the fix/underwalk-and-raw-slice branch August 30, 2026 23:49
DrunkOnJava added a commit that referenced this pull request Oct 26, 2026
#309)

A lone diff3 base line (|||||||) reached main twice: CHANGELOG.md since
#80 and ARCHITECTURE.md with #299. Both are removed; the text around each
was complete. The PII guard job gains a step that fails on any committed
<<<<<<<, ||||||| or >>>>>>> marker line.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant