Skip to content

Commit

Permalink
Fix panic for malformed playlists
Browse files Browse the repository at this point in the history
Fix the panic that appear in case of bad tags EXT-X-KEY, EXT-X-MAP
and custom tags.
  • Loading branch information
khenarghot committed Oct 16, 2023
1 parent 325e59e commit c28ec30
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
12 changes: 9 additions & 3 deletions reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,9 @@ func decodeLineOfMediaPlaylist(p *MediaPlaylist, wv *WV, state *decodingState, l
}
// If EXT-X-KEY appeared before reference to segment (EXTINF) then it linked to this segment
if state.tagKey {
p.Segments[p.last()].Key = &Key{state.xkey.Method, state.xkey.URI, state.xkey.IV, state.xkey.Keyformat, state.xkey.Keyformatversions}
if segment := p.Segments[p.last()]; segment != nil {
segment.Key = &Key{state.xkey.Method, state.xkey.URI, state.xkey.IV, state.xkey.Keyformat, state.xkey.Keyformatversions}
}
// First EXT-X-KEY may appeared in the header of the playlist and linked to first segment
// but for convenient playlist generation it also linked as default playlist key
if p.Key == nil {
Expand All @@ -546,7 +548,9 @@ func decodeLineOfMediaPlaylist(p *MediaPlaylist, wv *WV, state *decodingState, l
}
// If EXT-X-MAP appeared before reference to segment (EXTINF) then it linked to this segment
if state.tagMap {
p.Segments[p.last()].Map = &Map{state.xmap.URI, state.xmap.Limit, state.xmap.Offset}
if segment := p.Segments[p.last()]; segment != nil {
segment.Map = &Map{state.xmap.URI, state.xmap.Limit, state.xmap.Offset}
}
// First EXT-X-MAP may appeared in the header of the playlist and linked to first segment
// but for convenient playlist generation it also linked as default playlist map
if p.Map == nil {
Expand All @@ -557,7 +561,9 @@ func decodeLineOfMediaPlaylist(p *MediaPlaylist, wv *WV, state *decodingState, l

// if segment custom tag appeared before EXTINF then it links to this segment
if state.tagCustom {
p.Segments[p.last()].Custom = state.custom
if segment := p.Segments[p.last()]; segment != nil {
segment.Custom = state.custom
}
state.custom = make(map[string]CustomTag)
state.tagCustom = false
}
Expand Down
18 changes: 18 additions & 0 deletions reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,24 @@ func TestMellformedPanicIssue1(t *testing.T) {
}
}

// Test for https://github.com/khenarghot/m3u8/issues/2
func TestMellformedPanicIssue2(t *testing.T) {
bad := bytes.NewBuffer([]byte("#EXT-X-KEY:\n0"))
_, _, err := DecodeFrom(bad, false)
if err != nil {
t.Fail()
}
}

// Test for https://github.com/khenarghot/m3u8/issues/2
func TestMellformedPanicIssue2AltMAP(t *testing.T) {
bad := bytes.NewBuffer([]byte("#EXT-X-MAP:\n0"))
_, _, err := DecodeFrom(bad, false)
if err != nil {
t.Fail()
}
}

/****************
* Benchmarks *
****************/
Expand Down

0 comments on commit c28ec30

Please sign in to comment.