From 869a70545f826e003da015eef01b693a235fdd71 Mon Sep 17 00:00:00 2001 From: David Howden Date: Sat, 11 Apr 2020 12:20:04 +1000 Subject: [PATCH] Check versions before using data length indicator (#66) * id3v2: Read and use frame data length indicator * id3v2: only use data length indicator for 2.4 Co-authored-by: Mattias Wadman --- id3v2.go | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/id3v2.go b/id3v2.go index 74314bd..d4c6838 100644 --- a/id3v2.go +++ b/id3v2.go @@ -5,6 +5,7 @@ package tag import ( + "errors" "fmt" "io" "regexp" @@ -269,11 +270,34 @@ func readID3v2Frames(r io.Reader, offset uint, h *id3v2Header) (map[string]inter if flags != nil { if flags.Compression { - _, err = read7BitChunkedUint(r, 4) // read 4 - if err != nil { + switch h.Version { + case ID3v2_3: + // No data length indicator defined. + if _, err := read7BitChunkedUint(r, 4); err != nil { // read 4 + return nil, err + } + size -= 4 + + case ID3v2_4: + // Must have a data length indicator (to give the size) if compression is enabled. + if !flags.DataLengthIndicator { + return nil, errors.New("compression without data length indicator") + } + + default: + return nil, fmt.Errorf("unsupported compression flag used in %v", h.Version) + } + } + + if flags.DataLengthIndicator { + if h.Version == ID3v2_3 { + return nil, fmt.Errorf("data length indicator set but not defined for %v", ID3v2_3) + } + + size, err = read7BitChunkedUint(r, 4) + if err != nil { // read 4 return nil, err } - size -= 4 } if flags.Encryption {