Skip to content

Commit

Permalink
Fix crash when GetAllChildren returns nil,nil
Browse files Browse the repository at this point in the history
When GetAllChildren return nil, nil the following panic happens

panic: runtime error: makeslice: cap out of range

goroutine 1 [running]:
iso9660.(*File).GetChildren(0x54ec60?)

/home/tzachmann/develop/test/go/iso9660/src/iso9660/image_reader.go:254
+0x51

Here is the code that is problematic. When GetAllChildren returns nil,
nil make with a size of -2 will be called which results in the above
panic.

func (f *File) GetChildren() ([]*File, error) {
     children, err := f.GetAllChildren()
     if err != nil {
         return nil, err
     }

     filteredChildren := make([]*File, 0, len(children)-2)

This patch returns an error in case there are no children or the ReadAt
fails.

If in this cases a nil, nil is fine the above code could be changed to

     filteredChildren := make([]*File, 0, len(children))

to fix the problem.
  • Loading branch information
zagge-cgeo committed Dec 12, 2023
1 parent 4c03881 commit 470883c
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion image_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func (f *File) GetAllChildren() ([]*File, error) {
buffer := make([]byte, sectorSize)
for bytesProcessed := uint32(0); bytesProcessed < uint32(f.de.ExtentLength); bytesProcessed += sectorSize {
if _, err := f.ra.ReadAt(buffer, int64(baseOffset+bytesProcessed)); err != nil {
return nil, nil
return nil, err

Check warning on line 182 in image_reader.go

View check run for this annotation

Codecov / codecov/patch

image_reader.go#L182

Added line #L182 was not covered by tests
}

for i := uint32(0); i < sectorSize; {
Expand Down Expand Up @@ -239,6 +239,9 @@ func (f *File) GetAllChildren() ([]*File, error) {
f.children = append(f.children, newFile)
}
}
if f.children == nil {
return nil, fmt.Errorf("no children found")
}

Check warning on line 244 in image_reader.go

View check run for this annotation

Codecov / codecov/patch

image_reader.go#L243-L244

Added lines #L243 - L244 were not covered by tests

return f.children, nil
}
Expand Down

0 comments on commit 470883c

Please sign in to comment.