Skip to content

fix: fall back to a structural meta-group walk when (0002,0000) is overstated - #549

Merged
awatson1978 merged 1 commit into
release/1.0-beta-corefrom
core/finding-24-meta-group-length
Sep 30, 2026
Merged

awatson1978 merged 1 commit into
release/1.0-beta-corefrom
core/finding-24-meta-group-length

Conversation

@awatson1978

Copy link
Copy Markdown
Collaborator

This closes the quiet half of the wrong-group-length problem: an overstated (0002,0000) no longer pulls dataset elements into the meta header. Review finding 24 from the PR #512 review map.

Quick background. A Part 10 file opens with the File Meta Information — a small group-0002 header whose first element, (0002,0000) File Meta Information Group Length, declares how many bytes of meta follow it. readFile trusts that number: it slices a window of exactly that many bytes and parses the meta group out of it. Real clinical files get this number wrong in both directions, and the two directions fail very differently. An understated length just means the declared window ends early — the window still holds only group-0002 elements. An overstated length is worse: the window reaches past the end of the meta group and swallows the first dataset elements whole. Before this fix that file still parsed without a single warning — the swallowed elements sat in dicomDict.meta, were missing from dicomDict.dict, and the body was mis-framed from its first element. The quiet case, not the warned case.

The fix is one guard in readFile. After parsing the declared window, it checks that every element in it actually belongs to group 0002. If anything else got in, it warns and re-walks the meta group structurally from just after the group-length element, stopping at the first non-meta tag — the same structural walk the missing-group-length path already uses — so the body is framed exactly where the meta group really ends:

// (0002,0000) declares 10 bytes more than the meta group holds,
// so the declared window swallows (0028,0002) SamplesPerPixel.
const dicomDict = DicomMessage.readFile(buffer);

// before: quiet mis-frame
dicomDict.meta["00280002"]; // a dataset element, hiding in the meta header
dicomDict.dict["00280002"]; // undefined

// after: a warning, and correct framing
dicomDict.meta;             // group-0002 elements only
dicomDict.dict["00280002"]; // { vr: "US", Value: [1] }

What deliberately did not change: understated lengths are left alone — a short window still holds only group-0002 elements, so it is used as-is, exactly as before. The allowMissingHeader opt-in from issue #93 and the structural fallback for files with no (0002,0000) at all are untouched; the new guard only fires when a group-length element is present and overstated. Whether readFile should also grow a strict mode that throws on a wrong group length is the review map's open question 8, and stays open — this PR keeps recovery as the behavior.

Writing the regression test surfaced a small fixture bug worth confessing: the sampleDicomPart10 helper referenced TagHex.MediaStorageSOPClassUID, which didn't exist in the constants, so every synthetic sample file carried that element under a mangled (0000,000F) tag inside its meta group. The constant now exists and the helper writes a proper (0002,0002).

The new test in test/data.test.js pins it: it builds a sample file, bumps (0002,0000) by exactly the size of the first dataset element, and asserts the meta header stays pure group-0002, the swallowed element lands back in the dataset, and the recovery warns. It fails on the unguarded code and passes with the fix. Full suite is green: 620 passed, 18 skipped. For what it's worth, master (0.49.x line) has the identical unguarded window read, so this gap exists there too.

🤖 Generated with Claude Code

…erstated

An overstated File Meta Information Group Length used to pull dataset
elements into dicomDict.meta with no warning and drop them from
dicomDict.dict. readFile now checks that the declared window holds
only group-0002 elements; when it does not, it warns and re-walks the
meta group structurally, framing the body at the first non-meta
element. Understated lengths and the allowMissingHeader / missing
group-length leniencies are unchanged.

Also adds the missing TagHex.MediaStorageSOPClassUID constant, which
the sampleDicomPart10 test helper referenced; without it the helper
wrote that element under a garbage (0000,000F) tag inside the meta
group of every synthetic sample file.

Pins review finding 24 from the PR #512 review map.

Co-Authored-By: Claude Fable 5 <[email protected]>
@awatson1978
awatson1978 force-pushed the core/finding-24-meta-group-length branch from 67240d6 to 92925e4 Compare September 30, 2026 12:12
@awatson1978
awatson1978 merged commit 77e5640 into release/1.0-beta-core Sep 30, 2026
10 of 11 checks passed
@awatson1978
awatson1978 deleted the core/finding-24-meta-group-length branch September 30, 2026 12:19
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 1.0.0-beta.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant