Skip to content

fix: write a UV element with a null value instead of throwing - #551

Merged
awatson1978 merged 1 commit into
release/1.0-beta-corefrom
core/finding-3-uv-null-write
Sep 30, 2026
Merged

awatson1978 merged 1 commit into
release/1.0-beta-corefrom
core/finding-3-uv-null-write

Conversation

@awatson1978

Copy link
Copy Markdown
Collaborator

This fixes review finding 3 from the PR #512 review map: a UV element with a null value threw on write.

UV is the Unsigned 64-bit Very Long VR, and its bytes go out through DataView.setBigUint64, which is the one DataView setter that refuses to coerce — hand it anything that is not already a BigInt and it throws a TypeError. So while a bare Value: null was fine (the base write() short-circuit turns it into a zero-length element), a null inside the value array — or a plain JavaScript Number, which is how most callers naturally spell an integer — blew up the whole DicomDict.write() call:

const dicomDict = new DicomDict({});
// (0066,0040) LongPrimitivePointIndexList has VR UV
dicomDict.dict = { "00660040": { vr: "UV", Value: [null] } };
dicomDict.write();
// before: TypeError: Cannot convert null to a BigInt
// after: writes a valid part10 buffer, the entry re-reads as UV

dicomDict.dict = { "00660040": { vr: "UV", Value: [5] } };
dicomDict.write();
// before: TypeError: Cannot convert 5 to a BigInt
// after: writes the value as an unsigned 64-bit integer

That matters for conformance: PS3.5 7.4 requires a Type 2 attribute to be present with zero length when its value is unknown, and a writer that throws on the null can never produce that element. It is also inconsistent with the neighboring numeric VRs — US and UL run their values through toInt, so they already accept Numbers and numeric strings and quietly write a null entry as 0.

The fix is small and lives in Unsigned64BitVeryLong.writeBytes in src/ValueRepresentation.js: coerce each value to BigInt before the write. BigInts pass through untouched, Numbers and numeric strings go through BigInt(v), and a null or undefined entry becomes 0 exactly the way US/UL treat it. A null, undefined or empty scalar is deliberately left alone so the existing zero-length short-circuit keeps producing the empty Type 2 element.

The regression test is test_uv_null_and_number_values_write in test/data.test.js, sitting right next to test_null_number_vrs, which owns the null-numeric-write territory. It pins all seven value shapes — null, [null], undefined, [], [5], ["5"], [BigInt(5)] — through a full write-then-read round trip, and it fails on the current branch head with the exact Cannot convert null to a BigInt from the finding.

One note for the checklist's "verify master" column: master (0.49.2, 81e7e11) has the identical uncoerced writeBigUint64 call, so [null] and Number values throw the same TypeError there; only the scalar-null zero-length path behaves. The defect is real on master too, minus the eager-coercion variant the review saw on the 1.0 development branch.

🤖 Generated with Claude Code

DataView.setBigUint64 refuses anything that is not a BigInt, so a UV
element whose Value held a null entry or a plain Number threw
"Cannot convert null to a BigInt" from DicomDict.write(). PS3.5 7.4
requires a Type 2 attribute to be written with zero length when the
value is unknown, and the other numeric VRs already accept null
entries and Number values through toInt/toFloat. Coerce UV values to
BigInt on write: null and undefined entries become 0 like US/UL, and
a null, undefined or empty scalar keeps the zero-length short-circuit.

Fixes review finding 3 from the PR #512 review map.

Co-Authored-By: Claude Fable 5 <[email protected]>
@awatson1978
awatson1978 merged commit e62a740 into release/1.0-beta-core Sep 30, 2026
13 of 17 checks passed
@awatson1978
awatson1978 deleted the core/finding-3-uv-null-write branch September 30, 2026 12:13
awatson1978 added a commit that referenced this pull request Sep 30, 2026
…ne and event-stream findings

Engine findings 1/3/4/24/25/27/28/32 landed as PRs #544-#551; the
writers wave (#552-#557) fixed 5/6/7/11/22/23 in the ports and retired
14 by trimming the video factories from the core facade. Master
verdicts are now verified against origin/master, not just triaged.

Co-Authored-By: Claude Fable 5 <[email protected]>
@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