Added length check to IndexPid.

This commit is contained in:
scruzin 2019-07-11 18:16:33 +09:30
parent c8a0b7df07
commit c717595adc
1 changed files with 9 additions and 7 deletions

View File

@ -180,6 +180,7 @@ func FindPat(d []byte) ([]byte, int, error) {
var ( var (
errInvalidLen = errors.New("MPEG-TS data not of valid length") errInvalidLen = errors.New("MPEG-TS data not of valid length")
errCouldNotFind = errors.New("could not find packet with given PID") errCouldNotFind = errors.New("could not find packet with given PID")
errNotConsecutive = errors.New("could not find consecutive PIDs")
) )
// FindPid will take a clip of MPEG-TS and try to find a packet with given PID - if one // FindPid will take a clip of MPEG-TS and try to find a packet with given PID - if one
@ -223,10 +224,12 @@ func LastPid(d []byte, pid uint16) (pkt []byte, i int, err error) {
func IndexPid(d []byte, pids ...uint16) (idx int, m map[string]string, err error) { func IndexPid(d []byte, pids ...uint16) (idx int, m map[string]string, err error) {
idx = -1 idx = -1
for _, pid := range pids { for _, pid := range pids {
pkt, i, _err := FindPid(d, pid) if len(d) < PacketSize {
return idx, m, errInvalidLen
}
pkt, i, err := FindPid(d, pid)
if err != nil { if err != nil {
err = errors.Wrap(_err, "could not find PID") return idx, m, errCouldNotFind
return
} }
if pid == PmtPid { if pid == PmtPid {
m, _ = metaFromPMT(pkt) m, _ = metaFromPMT(pkt)
@ -234,8 +237,7 @@ func IndexPid(d []byte, pids ...uint16) (idx int, m map[string]string, err error
if idx == -1 { if idx == -1 {
idx = i idx = i
} else if i != 0 { } else if i != 0 {
err = errors.New("PIDs not consecutive") return idx, m, errNotConsecutive
return
} }
d = d[i+PacketSize:] d = d[i+PacketSize:]
} }