Skip to content

Commit

Permalink
fix: infinite loop on dcm with no meta length (#331)
Browse files Browse the repository at this point in the history
  • Loading branch information
conrad-scherb committed Dec 13, 2022
1 parent 46d2dcf commit 51b156b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
11 changes: 9 additions & 2 deletions packages/adapters/src/DicomMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,15 @@ class DicomMessage {
if (stream.readAsciiString(4) !== "DICM") {
throw new Error("Invalid DICOM file, expected header is missing");
}
var el = DicomMessage._readTag(stream, useSyntax),
metaLength = el.values[0];

var el = DicomMessage._readTag(stream, useSyntax);
if (el.tag.toCleanString() !== "00020000") {
throw new Error(
"Invalid DICOM file, meta length tag is malformed or not present."
);
}

var metaLength = el.values[0];

//read header buffer
var metaStream = stream.more(metaLength);
Expand Down
25 changes: 25 additions & 0 deletions packages/adapters/test/data.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -765,3 +765,28 @@ it("Reads and writes numbers with NaN and Infinity values of tags with type FD (
Infinity
);
});

it("Tests that reading fails on a DICOM without a meta length tag", () => {
const rawFile = fs.readFileSync("test/no-meta-length-test.dcm");

let arrayBuffer = rawFile.buffer;
if (
rawFile.byteOffset !== 0 ||
rawFile.byteLength !== arrayBuffer.byteLength
) {
arrayBuffer = arrayBuffer.slice(
rawFile.byteOffset,
rawFile.byteOffset + rawFile.byteLength
);
}

expect(() => {
dcmjs.data.DicomMessage.readFile(arrayBuffer, {
ignoreErrors: false,
untilTag: "0020000E",
includeUntilTagValue: true
});
}).toThrow(
"Invalid DICOM file, meta length tag is malformed or not present."
);
});
Binary file added packages/adapters/test/no-meta-length-test.dcm
Binary file not shown.

0 comments on commit 51b156b

Please sign in to comment.