You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Parse panics with "index out of range [0]" when an integer element such as (0002,0000) or Rows has length 0 #392
dicom.Parse panics on a 140-byte file whose File Meta Information Group Length (0002,0000) is UL with a value length of 0. The empty UL decodes to an empty []int, and readHeader takes [0] from it without checking the length (read.go:230):
metaLen:=maybeMetaLen.Value.GetValue().([]int)[0]
Nothing recovers the panic, so any program that parses files it did not create with dicom.Parse crashes instead of getting an error.
panic: runtime error: index out of range [0] with length 0
github.com/suyashkumar/dicom.(*reader).readHeader(...)
github.com/suyashkumar/dicom.NewParser(...)
github.com/suyashkumar/dicom.parseInternal(...)
github.com/suyashkumar/dicom.Parse(...)
Second site
readNativeFrames does the same with MustGetInts(...)[0] for Rows, Columns, BitsAllocated and SamplesPerPixel (read.go:446-454). A file with a valid header and an empty US Rows and Columns before PixelData panics there the same way. read.go:433 (NumberOfFrames) and write.go:322 index the same way, but I haven't produced a panic from either.
Expected
An error, the same as for other malformed headers. ErrorMetaElementGroupLength looks like the right one for read.go:230.
Fix
I have a fix ready. An empty (0002,0000) is treated like a missing one, so it returns ErrorMetaElementGroupLength unless AllowMissingMetaElementGroupLength is set. readNativeFrames reads Rows, Columns, BitsAllocated and SamplesPerPixel through a small helper that returns an error for an empty value. A new TestParse_EmptyIntElements covers both sites and panics without the fix, and make test passes with it. I'll open a PR if that sounds right to you.
(Found by fuzzing and reproduced with the help of an AI coding assistant.)
Activity