Repository navigation
fix(handle-map): the handle delta is an unsigned MC and both accumulators restart per handle section - #53
Merged
Conversation
…tors restart per handle section The AcDb:Handles delta run was decoded with two wrong rules, and their combination is what #44 measured as a 96,739-byte address-space shortfall against AcDb:AcDbObjects. 1. The handle delta is an UNSIGNED modular char; only the offset delta is signed. Reading it as signed steals bit 0x40 of the terminating byte as a negation flag, so every single-byte delta in 0x40..=0x7F decoded small and negative. On sample_AC1032.dwg the first divergence is entry 272, after which every handle ran 108 low. 2. Both accumulators restart at zero on every handle section. The first pair of a section is absolute. Carrying the totals across the boundary threw entries 656..841 (186 of 842) into a fabricated address space and produced the phantom offset 1,289,590. The section assembly was never wrong: AcDb:AcDbObjects is 41 pages of 0x7400 decompressed bytes truncated to its declared 1,192,851, and the trailing 24,685 bytes are the last page's zero padding. After the fix the map's highest offset is 1,192,823. Every record repeats its own handle, so the object stream verifies the map against itself. Agreement goes from 272/842 to 842/842, walker skips from 97 to 0, and the walked records cover 1,191,935 of 1,192,851 section bytes (916 unclaimed, longest run 4). BitCursor::read_handle now rejects a counter above 8 with the new Error::HandleCounterTooLarge, surfaced as a walker skip rather than a decoder error so coverage ratios stay comparable. Coverage over the local 19-file corpus: R2018 561/145/39/75.3% -> 712/91/39/84.6%; aggregate 1923/319/39/84.3% -> 2074/265/39/87.2%. Closes #43 Closes #44 Closes #51 Claude-Session: https://claude.ai/code/session_01BaHKf1Rw5aJBGK5y3xmx7C
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_94fd1163-7878-4592-b7d3-3de47c7602dc) |
…s-space # Conflicts: # CHANGELOG.md # README.md # STATUS.md
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
AcDb:Handleswas decoded with two wrong rules. Their combination — not thesection assembly — is what #44 measured as a 96,739-byte address-space
shortfall against
AcDb:AcDbObjects, and what #43 saw as classes the walkernever reached.
signed. Handles grow monotonically inside a handle section, so the encoder
spends all seven low bits of the terminating byte on magnitude. Reading it as
a signed MC steals bit
0x40as a negation flag, so every single-bytedelta in
0x40..=0x7Fdecoded small and negative and the multi-byte formsplit its limbs one bit short. Byte offsets genuinely move backward (deleted
objects' space gets reused), so the offset delta keeps the signed encoding.
(handle, offset)pair of a section is absolute, not a delta from theprevious section's last entry.
Evidence
The section assembly was never wrong.
examples/probe_section_pages.rs(new) walks
AcDb:AcDbObjectspage by page onsample_AC1032.dwg:start_offsetstepping by exactly0x7400(29,696) from 0 to 1,187,840.bytes and 24,685 bytes of padding — consistent with that page's compressed
payload being only 3,317 bytes where every full page is ~29,817.
max_decomp_page_size29,696,compressed2,encrypted0,unknown1,every
hdr.start_offsetequal to itsref start. Nothing is skipped ortruncated.
The map was wrong. Every record in
AcDb:AcDbObjectsrepeats its ownhandle, so the object stream is a free oracle for the map: does the record at
the offset this map produced carry the handle this map produced? Decoding the
same 2,642 handle bytes four ways:
Byte-level witnesses on that file:
0x438at offset 148,422;the record there carries
0x4A4. Every later handle ran 108 low.exactly). Section 2's first pair is
hd=2851, od=99522. Carried across theboundary that reads as handle
0x1343at offset 197,439, where no recordstarts. Read as absolute it is handle
0xB23at offset 99,522 — and therecord at 99,522 declares size 1,608 and handle 2851. The next pair
(
+1,+1613) lands at 101,135 on a record declaring handle 2852.map's highest offset is 1,192,823, 28 bytes inside the declared section,
and all six previously out-of-range entries resolve to real records.
No records are hiding outside the map. The 842 walked records cover
1,191,935 of the section's 1,192,851 bytes; 916 bytes are unclaimed and the
longest unclaimed run is 4 bytes (inter-record padding).
Measured results
cargo run --release --example coverage_report -- <19-file corpus>, basef706937vs this branch:R2004/R2010/R2013 are unchanged because those samples carry a single handle
section and no delta ≥
0x40— neither bug could fire there. That is thecontrol.
Walker diagnostics on
sample_AC1032.dwg: 97 handle-map entries yielded norecord before (91 "no record at offset", 6 "past end of stream"); 0 after.
Handle self-check mismatches: 430 before (measured with this PR's new
diagnostic against the old delta rules), 0 after.
Declared vs walked per class (#43)
examples/probe_class_census.rs(new) againstAcDb:Classesnum_objects. Only rows that changed or are still short:Pre-fix the census also over-counted several classes (ACDBDICTIONARYWDFLT
1/2, DICTIONARYVAR 11/12, MESH 2/3, BLOCKLINEARPARAMETER 1/2,
ACDB_BLOCKREPRESENTATION_DATA 2/3) because garbage offsets parsed into
plausible-looking records; those are gone too.
41 of 43 classes now reach their declared instance count. LAYOUT (4) and
ACDBPLACEHOLDER (1) are credited via their fixed type codes
0x52/0x50—both were promoted out of the custom-class range, so the drawing registers the
class but writes the instances with the fixed code. TABLECONTENT and
TABLEGEOMETRY each declare 5 and reach 2; with the object stream 99.92 %
covered and zero unclaimed runs longer than 4 bytes, those three instances
apiece are not present as standalone records in this drawing. That is a
class-table bookkeeping question, not a walker gap — follow-up below.
#51 —
read_handlerejects a counter above 8BitCursor::read_handlenow returns the newError::HandleCounterTooLargewhen the counter nibble exceeds 8. The nibble holds 0..=15 but a handle value
is at most a
u64, so a wider counter proves the cursor is not on a handlefield; the old code shifted the excess out and produced a plausible handle from
garbage (the
counter = 11in #44). Measured against the pre-fix addressspace, this gate alone caught 43 phantom records on
sample_AC1032.dwg.It surfaces as a walker skip, never as a dispatch error, so coverage ratios
stay comparable —
coverage_reportgrows a separatewskipcolumn and awalker-diagnostic histogram.
The three phantom
CUSTOM(727)records from #44 are gone, along withCUSTOM(517)at 212,392 andCUSTOM(564)at 229,599.Changes
src/handle_map.rs—read_unsigned_mc/write_unsigned_mc; accumulatorsscoped to the handle section on both read and write; the writer opens a new
section when a caller's handles move backward (unencodable as an unsigned
delta). Five new byte-built tests, including one that pins
0x56as +86 andnot −22, and one that pins the per-section restart.
src/bitcursor.rs— the counter gate + two tests (a full 8-byte counter stillround-trips; a
BitWriter-builtcode 2 / counter 11is rejected).src/object.rs—ObjectWalkSummary::handle_mismatchesrecords every recordwhose own handle disagrees with the map. Reported, not enforced.
src/reader.rs—DwgFile::all_objects_lossy()exposes the walk summary.src/section_map.rs—SectionDescription::unknownsurfaced for the probe.tests/canonical_corpus.rs— two new gates across all four fixtures: thehandle map must resolve to records that agree with it (zero skips, zero
mismatches, walked count == map length), and the walk must reach every
declared class instance.
src/entities/dispatch.rs— one negative-control test now expects themisaligned read to fail at
read_handlerather than in the entity body.examples/probe_section_pages.rsandexamples/probe_class_census.rs;examples/coverage_report.rsgains thewalker-diagnostic reporting.
and the corrected
AcDb:Handleswire description.No fixture bytes changed, so
tests/canonical_corpus.rs's existing field pinsare untouched: the writer moved in lockstep with the reader and the synthetic
maps are single-section with small deltas.
Spec sections
§2.6 (modular chars), §2.13 (handle references), §4.5 / §4.6 (section info and
the 0x20 data-page header), §21 (object map /
AcDb:Handles), §20 (objectstream records).
Source provenance
Clean-room. Derived from the ODA DWG specification v5.4.1 and from measurements
of
sample_AC1032.dwgitself using the probes in this repository. NoLibreDWG, ODA, Autodesk, or other third-party DWG source was read or consulted
for this change;
CLEANROOM.mdneeds no new disclosure.Gate
cargo fmt --all,cargo clippy --all-targets --all-features -- -D warnings,cargo test --release(703 lib + all integration suites),cargo deny check all,RUSTDOCFLAGS='-D warnings' cargo doc --no-deps --all-features, andcargo +1.85.0 test --no-runall pass.Follow-up candidates
sample_AC1032.dwgand only 2 exist as standalone records. Decide whethernum_objectsis a live census or a registration-time counter, or whether themissing instances are embedded inside the two
ACAD_TABLEentities.probe_class_censusexits non-zero on any under-walked class; wiring it intoCI once the two rows above are explained would make the class table a
standing completeness gate.
presumably record alignment padding; confirming that against the spec would
turn "99.92 % covered" into "100 % accounted for".
sample_AC1032.dwg, now concentrated inMULTILEADER (15) and HATCH (8) — both grew because the fix recovered records
that previously never reached a decoder.
Closes #43
Closes #44
Closes #51
https://claude.ai/code/session_01BaHKf1Rw5aJBGK5y3xmx7C
Note
Medium Risk
Changes core R2004+ object enumeration (
HandleMapread/write and the object walker), which affects every downstream decode path; behavior is heavily tested and corpus-gated, but regressions on multi-section handle maps would be high impact.Overview
Fixes
AcDb:Handlesdecoding so handle deltas use unsigned modular chars (offset deltas stay signed) and handle/offset accumulators reset at each handle section. The writer mirrors the same rules, including splitting sections when handles would move backward.Adds
Error::HandleCounterTooLargewhenBitCursor::read_handlesees a counter above 8,ObjectWalkSummary::handle_mismatches, andDwgFile::all_objects_lossy()so walks can be checked against each record’s embedded handle. Corpus integration tests and new probes (probe_section_pages,probe_class_census) gate completeness;coverage_reportadds a separatewskipcolumn for walker skips.On
sample_AC1032.dwg, handle-map self-check goes to 842/842, walker skips drop to 0, and measured decode coverage rises (712 decoded, 84.6% file / 87.2% aggregate). Closes #43, #44, #51.Reviewed by Cursor Bugbot for commit ac5e7e0. Configure here.