metaImage: validate LIST file dimension and slice count#141
Merged
dzenanz merged 1 commit intoJul 22, 2026
Conversation
The ElementDataFile = LIST branch accepted an out-of-range optional file dimension and did not verify that the list named enough files. An explicit dimension was rejected only when zero or greater than NDims, so a negative value reached m_DimSize[fileImageDim - 1] and m_SubQuantity[fileImageDim], indexing outside the fixed [10] arrays, and a value equal to NDims silently read nothing while reporting success. Treat any value outside [1, NDims) as absent and fall back to NDims - 1, as the existing comment already describes. The read loop also stops at end of stream, so a list naming fewer files than DimSize requires returned success with the tail of the buffer never written. Count the slices actually read and fail if any are missing.
dzenanz
approved these changes
Jul 22, 2026
Contributor
Author
|
@dzenanz Can you find someone who can merge these? After merging, I'll run the ITK UpdateFromUpstream.sh to get these fixes in place in ITK. |
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.
The
ElementDataFile = LISTreader accepts an out-of-range file dimension and does not check that the list named enough files. Both cases report success while leaving pixel data unread; one also indexes outside a fixed-size array.Found while auditing MetaIO as vendored by ITK (InsightSoftwareConsortium/ITK#6575, item B64). New test
testMeta13ImageListcovers all three cases and fails before this change.Out-of-range file dimension
ElementDataFile = LISTtakes an optional file dimension. It was rejected only when zero or greater thanNDims:m_DimSizeandm_SubQuantityare fixed[10]members indexable over[0, NDims), so:LIST -1passes the guard, thenfor (i = m_NDims; i > fileImageDim; i--) totalFiles *= m_DimSize[i - 1];walks down toi = 0and readsm_DimSize[-1], and the slice read usesm_SubQuantity[-1]. Both are out-of-bounds reads on values taken straight from the header.LIST NwhereN == NDimsleavestotalFilesat 1 and uses the zero-initializedm_SubQuantity[NDims], so a single zero-length slice is "read" and success is returned having read nothing.Any value outside
[1, NDims)is now treated as absent and falls back toNDims - 1, which is what the existing comment already describes.Short list
The read loop also stops at end of stream:
A list naming fewer files than
DimSizerequires exits early and falls through to success, leaving the tail ofm_ElementDatanever written. Note the loop counter still reachestotalFilesin that case (the final iteration reads nothing), so the count of slices actually read is tracked separately and checked after the loop.Testing
testMeta13ImageListbuilds a 2x2x2MET_UCHARvolume from two single-slice raw files and checks four headers: a well-formed list reads both slices; a short list must fail;LIST -1andLIST 3must fall back and read both slices correctly.Verified fail-before/pass-after — before the change it reports
Short LIST reported success: FAIL. Full suite 13/13 passing (12 pre-existing + this one).The same fixtures were also run through ITK's MetaImageIO, where all four previously reported success; afterwards the short list raises and the two out-of-range dimensions read correctly. ITK's Meta test suite shows no regressions.
The
ReadROIvariant has its ownLISTbranch, but it does not parse a file dimension, so only the short-list shape could apply there; it is left alone here to keep this change focused.