grafov / m3u8

Parser and generator of M3U8-playlists for Apple HLS. Library for Go language. :cinema:
http://tools.ietf.org/html/draft-pantos-http-live-streaming
BSD 3-Clause "New" or "Revised" License
1.23k stars 315 forks source link

Using for range loop to MediaPlaylist.Segments would lead to nil pointer dereference #140

Closed NitroCao closed 5 years ago

NitroCao commented 5 years ago

code for reproduce:

package main

import (
    "fmt"
    "os"
    "github.com/grafov/m3u8"
    "log"
    "bufio"
)

func main() {
    f, err := os.Open("test.m3u")
    if err != nil {
        log.Fatalln(err)
    }

    p, listType, err := m3u8.DecodeFrom(bufio.NewReader(f), false)
    if err != nil {
        log.Fatalln(err)
    }

    switch listType {
    case m3u8.MEDIA:
        mediapl := p.(*m3u8.MediaPlaylist)
        segments := mediapl.Segments
        for _, entry := range segments {
            fmt.Println(entry.URI)
        }
    }
}

m3u8 file for test:

#EXTM3U
#EXT-X-VERSION:3
#EXT-X-MEDIA-SEQUENCE:0
#EXT-X-ALLOW-CACHE:YES
#EXT-X-TARGETDURATION:17
#EXTINF:10.041667,
https://mydomain.com/aaa0.ts
#EXT-X-DISCONTINUITY
#EXTINF:11.166667,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video000.ts
#EXTINF:11.033333,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video001.ts
#EXTINF:8.333333,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video002.ts
#EXTINF:10.666667,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video003.ts
#EXTINF:14.233333,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video004.ts
#EXTINF:5.800000,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video005.ts
#EXTINF:16.033333,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video006.ts
#EXTINF:8.333333,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video007.ts
#EXTINF:8.333333,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video008.ts
#EXTINF:8.333333,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video009.ts
#EXTINF:8.333333,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video010.ts
#EXTINF:11.900000,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video011.ts
#EXTINF:7.633333,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video012.ts
#EXTINF:14.100000,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video013.ts
#EXTINF:8.333333,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video014.ts
#EXTINF:8.333333,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video015.ts
#EXTINF:9.433333,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video016.ts
#EXTINF:13.600000,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video017.ts
#EXTINF:8.333333,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video018.ts
#EXTINF:8.333333,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video019.ts
#EXTINF:16.666667,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video020.ts
#EXTINF:8.333333,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video021.ts
#EXTINF:8.333333,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video022.ts
#EXTINF:8.333333,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video023.ts
#EXTINF:8.333333,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video024.ts
#EXTINF:16.666667,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video025.ts
#EXTINF:8.333333,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video026.ts
#EXTINF:8.333333,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video027.ts
#EXTINF:8.333333,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video028.ts
#EXTINF:8.333333,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video029.ts
#EXTINF:9.833333,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video030.ts
#EXTINF:16.666667,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video031.ts
#EXTINF:8.333333,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video032.ts
#EXTINF:8.333333,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video033.ts
#EXTINF:8.333333,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video034.ts
#EXTINF:8.333333,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video035.ts
#EXTINF:16.666667,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video036.ts
#EXTINF:8.333333,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video037.ts
#EXTINF:8.333333,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video038.ts
#EXTINF:8.333333,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video039.ts
#EXTINF:8.333333,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video040.ts
#EXTINF:16.666667,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video041.ts
#EXTINF:8.333333,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video042.ts
#EXTINF:8.333333,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video043.ts
#EXTINF:8.333333,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video044.ts
#EXTINF:8.333333,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video045.ts
#EXTINF:16.300000,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video046.ts
#EXTINF:8.333333,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video047.ts
#EXTINF:8.333333,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video048.ts
#EXTINF:8.333333,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video049.ts
#EXTINF:8.333333,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video050.ts
#EXTINF:16.666667,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video051.ts
#EXTINF:8.333333,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video052.ts
#EXTINF:8.333333,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video053.ts
#EXTINF:8.333333,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video054.ts
#EXTINF:8.333333,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video055.ts
#EXTINF:16.666667,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video056.ts
#EXTINF:8.333333,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video057.ts
#EXTINF:8.333333,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video058.ts
#EXTINF:8.333333,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video059.ts
#EXTINF:8.333333,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video060.ts
#EXTINF:16.666667,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video061.ts
#EXTINF:8.333333,
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video062.ts
#EXT-X-ENDLIST

result:

https://mydomain.com/aaa0.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video000.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video001.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video002.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video003.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video004.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video005.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video006.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video007.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video008.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video009.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video010.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video011.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video012.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video013.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video014.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video015.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video016.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video017.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video018.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video019.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video020.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video021.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video022.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video023.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video024.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video025.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video026.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video027.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video028.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video029.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video030.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video031.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video032.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video033.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video034.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video035.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video036.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video037.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video038.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video039.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video040.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video041.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video042.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video043.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video044.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video045.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video046.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video047.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video048.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video049.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video050.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video051.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video052.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video053.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video054.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video055.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video056.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video057.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video058.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video059.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video060.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video061.ts
http://mydomain.com/video/20190524/d87357180db6ddbe3b65338298d72975/GQ/video062.ts
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x4d1f2a]

goroutine 1 [running]:
main.main()
        /home/jaycecao/UNIX_Programming/go/toolkit/main.go:27 +0x29a
mjneil commented 5 years ago

This seems to be intentional so that you can manipulate the segment window without removing references to segments, but head and tail aren't exposed (#127), so it doesn't seem very useful.

DecodeFrom initializes a playlist with a 1024 length slice, so you'll just have to make a nil check at the start of your loop

leikao commented 5 years ago

Agree with @mjneil . Here is an example to loop the media segments: https://github.com/grafov/m3u8/blob/920643e7fcabbe6078c26b65e6b38ae9306f5b33/reader_test.go#L653-L655

Closing this issue, you can re-open it if you need more assistance.