Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent infinite loop when calling ReadBoxStructure #151

Merged
merged 1 commit into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion box_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mp4
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"math"
)
Expand Down Expand Up @@ -128,7 +129,6 @@ func ReadBoxInfo(r io.ReadSeeker) (*BoxInfo, error) {
if _, err := bi.SeekToPayload(r); err != nil {
return nil, err
}

} else if bi.Size == 1 {
// read more 8 bytes
buf.Reset()
Expand All @@ -139,6 +139,10 @@ func ReadBoxInfo(r io.ReadSeeker) (*BoxInfo, error) {
bi.Size = binary.BigEndian.Uint64(buf.Bytes())
}

if bi.Size == 0 {
return nil, fmt.Errorf("invalid size")
}

return bi, nil
}

Expand Down
10 changes: 10 additions & 0 deletions read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,13 @@ func TestReadBoxStructureQT(t *testing.T) {
// 47 [stsc] Size=28 Version=0 Flags=0x000000 EntryCount=1 Entries=[{FirstChunk=1 SamplesPerChunk=1 SampleDescriptionIndex=1}]
// 48 [stsz] Size=111852 ... (use "-full stsz" to show all)
// 49 [stco] Size=111848 ... (use "-full stco" to show all)

// this used to cause an infinite loop.
func TestReadBoxStructureZeroSize(t *testing.T) {
b := []byte("\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01")

_, err := ReadBoxStructure(bytes.NewReader(b), func(h *ReadHandle) (interface{}, error) {
return nil, nil
})
require.Error(t, err)
}