limit up-front allocation when reading bytes

Fixes #73
This commit is contained in:
David Howden 2020-08-29 07:40:05 +10:00
parent 5d76b8eaae
commit 46e57f75db
2 changed files with 52 additions and 0 deletions

40
sum_test.go Normal file
View File

@ -0,0 +1,40 @@
package tag
import (
"bytes"
"testing"
)
func TestFuzz(t *testing.T) {
fuzzIssue73(dataIssue73)
}
var dataIssue73 = []byte{0x49, 0x44, 0x33, 0x03, 0x00, 0x40, 0x00, 0x00, 0x00, 0x0E, 0xDB, 0xDB, 0xDB, 0xDB, 0xDB, 0xDB,
0xDB, 0x06, 0xFF, 0x54, 0x58, 0x58, 0x00}
func fuzzIssue73(in []byte) {
r := bytes.NewReader(in)
Identify(r)
m, err := ReadFrom(r)
if err != nil {
return
}
m.Format()
m.FileType()
m.Title()
m.Album()
m.Artist()
m.AlbumArtist()
m.Composer()
m.Year()
m.Genre()
m.Track()
m.Disc()
m.Picture()
m.Lyrics()
Sum(r)
}

12
util.go
View File

@ -5,6 +5,7 @@
package tag
import (
"bytes"
"encoding/binary"
"io"
)
@ -40,7 +41,18 @@ func readUint64LittleEndian(r io.Reader) (uint64, error) {
return binary.LittleEndian.Uint64(b), nil
}
// readBytesMaxUpfront is the max up-front allocation allowed
const readBytesMaxUpfront = 10 << 20 // 10MB
func readBytes(r io.Reader, n uint) ([]byte, error) {
if n > readBytesMaxUpfront {
b := &bytes.Buffer{}
if _, err := io.CopyN(b, r, int64(n)); err != nil {
return nil, err
}
return b.Bytes(), nil
}
b := make([]byte, n)
_, err := io.ReadFull(r, b)
if err != nil {