kaitai_struct_go_runtime
kaitai_struct_go_runtime copied to clipboard
Potential bug in `Stream.Size()`
The current implementation of Stream.Size() is:
// Size returns the number of bytes of the stream.
func (k *Stream) Size() (int64, error) {
// Go has no internal ReadSeeker function to get current ReadSeeker size,
// thus we use the following trick.
// Remember our current position
curPos, err := k.Pos()
if err != nil {
return 0, err
}
// Seek to the end of the File object
_, err = k.Seek(0, io.SeekEnd)
if err != nil {
return 0, err
}
// Remember position, which is equal to the full length
fullSize, err := k.Pos()
if err != nil {
return fullSize, err
}
// Seek back to the current position
_, err = k.Seek(curPos, io.SeekStart)
return fullSize, err
}
It seems that if the assignment fullSize, err := k.Pos() returns an error the current position pointer is never set to its original value.
A potential mitigation of the problem could be:
// Size returns the number of bytes of the stream.
func (k *Stream) Size() (int64, error) {
// Go has no internal ReadSeeker function to get current ReadSeeker size,
// thus we use the following trick.
// Remember our current position
curPos, err := k.Pos()
if err != nil {
return 0, err
}
// Deferred seek back to the current position
defer k.Seek(curPos, io.SeekStart)
// Seek to the end of the File object
_, err = k.Seek(0, io.SeekEnd)
if err != nil {
return 0, err
}
// return position, which is equal to the full length
return k.Pos()
}
@chavacava Good catch! Can you please open a pull request with the fix?
@generalmimon please check PR #27