Repository navigation
fix(container): a record's MS excludes its own MC, and the class table is not always a live census - #80
Merged
Conversation
…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
Bugbot couldn't run - usage limit reachedBugbot 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) |
…-slice # Conflicts: # CHANGELOG.md
This was referenced Aug 30, 2026
Closed
This was referenced Oct 26, 2026
Closed
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.
Summary
Two container-layer fixes in one pass: the walker's record slice ended one
MCwidth 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
MSexcludes its ownMC#75 measured that every inter-record run in
AcDb:AcDbObjectsequals the width of the preceding record's leadingMChandle-stream-size field:MCwidthMCwidthon
sample_AC1032.dwg, with 100 % agreement onarc_2010.dwg,line_2010.dwgandline_2013.dwg. Only 8 of those 916 bytes are zero, so they are content, not padding; 772 one-byte + 70 two-byteMCfields + the 4-byte0x0dcaprologue account for all 916 exactly.So a record is framed
and
RawObject::rawis now sizedmc_width + size_bytes.size_byteskeeps reporting theMSverbatim (documented), soraw.len() != size_byteson R2010+.The
+ mc_field_bitscorrection instring_stream::data_section_endwas compensating for exactly that short slice and is removed — the rule is nowpayload_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:
MCwidth per record on R2010+, becauseposadvanced to the shortcrc_end.DwgFile::objects()uses that mode;sample_AC1032.dwg0x0dcaprologue), 0 between recordsarc_2010.dwgarc_2013.dwgarc_2004.dwg,arc_2007.dwgMCon those bands)New unit tests in
src/object.rspin the slice width on byte-built records: a one-byteMC(raw.len() == size_bytes + 1, and the second record'sstream_offsetlands where the first record really ends), a two-byteMC(+ 2), the pre-R2010 case (raw.len() == size_bytes), andmc_field_widthitself.The fixture writer wrote the old rule
examples/build_fixturesemittedMS = payload.len()including theMC, and declared a zero-bit handle stream. The second part mattered more than the first: withhandle_bits = 0the old formula putdata_section_endpast the end of the record, which returnedNoneand silently disabled every decoder's boundary check onsynthetic_2010/2013/2018.dwg. The writer now emitsMS = payload.len() - mc_widthand writes the §19.1 strings present trailer bit with theMCrecording the byte padding that stands in for the handle stream — the same conventionstring_stream::tests::build_payloaduses. Those three fixtures are regenerated;synthetic_2000/synthetic_2004are byte-identical.#76 — DICTIONARYVAR / CELLSTYLEMAP are not under-walked
New probe
examples/probe_reference_closureruns three measurements that never consult the class table. All three are closed on all nineteen corpus files:0xA0Bofsample_AC1032.dwgto0xD17,0xD18,0xD40,0xD41,0xD95,0xD96, in a drawing that leaves 2,841 of the 3,683 handle values in0x1..=0xE63unused; 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.)Measurement 3 is decisive:
AcDbVariableDictionarykeysarc/circle/line_2004.dwgarc/circle/line_2007.dwgarc/circle/line_2010.dwgarc/circle/line_2013.dwgsample_AC1032.dwgKeys 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 (handle0x8F) is literallyCANNOSCALE, CMLEADERSTYLE, CTABLESTYLE, CVIEWDETAILSTYLE, CVIEWSECTIONSTYLE, LAYEREVAL, LAYERNOTIFY, LIGHTINGUNITS, MSLTSCALE, XCLIPFRAME— ten keys, ten records, sixteen declared.CELLSTYLEMAP says the same thing more sharply:
arc_2010.dwgdeclares one, contains none, and carries noACAD_ROUNDTRIP_2008_TABLESTYLE_CELLSTYLEMAPdictionary 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_layoutonarc_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 misreadnum_objectswould not agree on eight and diverge on two.What moved in the ratchet
Nothing was under-walked to recover, so the
KNOWN_SHORTFALLcounts are unchanged — a fixed under-walk would have moved them, and this was not one. What changed is the classification:tests/class_census.rsand the probe'sALLOWLISTno 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 anywalkedfigure 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:
arc/circle/line_2004.dwgarc/circle/line_2004.dwgarc/circle/line_2007.dwgarc/circle/line_2007.dwgarc/circle/line_2010.dwgarc/circle/line_2010.dwgsample_AC1032.dwgsample_AC1032.dwgEvery 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/mainis6cc9b13, no merge needed):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).
diffof 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 --allcargo clippy --all-targets --all-features -- -D warningscargo test --release— 19 test binaries, all green (770 lib tests + integration)cargo deny check allRUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-featurescargo +1.85.0 test --no-run(MSRV, no let-chains)tests/class_census.rsran against the real 19-file corpus (not skipped) and passespub fncarries a///lineprobe_reference_closureover the canonical fixtures alongside the existing census gateSource-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:
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, soRawObject::rawis nowmc_width + size_byteswhilesize_bytesstill reflects the on-diskMS. The compensating+ mc_field_bitsinstring_stream::data_section_endis 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_closureand 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-declarednum_objects, not missing walk records; ratchet pins intests/class_census.rskeep the same counts.build_fixturesnow emitsMS = 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.