Skip to content

Commit a55f10a

Browse files
committed
pes: detect truncated data and error
1 parent 473f66c commit a55f10a

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

data.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ func parseData(ps []*Packet, prs PacketsParser, pm *programMap) (ds []*DemuxerDa
8484
// Parse PES data
8585
var pesData *PESData
8686
if pesData, err = parsePESData(i); err != nil {
87+
if err == ErrTruncatedPESPackets {
88+
// The accumulator gave us all the packets available, but there's still not
89+
// enough data. Ignore and keep going.
90+
err = nil
91+
return
92+
}
8793
err = fmt.Errorf("astits: parsing PES data failed: %w", err)
8894
return
8995
}

data_pes.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package astits
22

33
import (
4+
"errors"
45
"fmt"
56

67
"github.com/asticode/go-astikit"
@@ -36,6 +37,11 @@ const (
3637
TrickModeControlSlowReverse = 4
3738
)
3839

40+
// Errors
41+
var (
42+
ErrTruncatedPESPackets = errors.New("astits: missing data in PES packets")
43+
)
44+
3945
const (
4046
pesHeaderLength = 6
4147
ptsOrDTSByteLength = 5
@@ -115,6 +121,8 @@ func (h *PESHeader) IsVideoStream() bool {
115121
}
116122

117123
// parsePESData parses a PES data
124+
// Returns ErrTruncatedPESPackets if the header was parsed, but the
125+
// advertised PacketLength was not available.
118126
func parsePESData(i *astikit.BytesIterator) (d *PESData, err error) {
119127
// Create data
120128
d = &PESData{}
@@ -129,6 +137,11 @@ func parsePESData(i *astikit.BytesIterator) (d *PESData, err error) {
129137
return
130138
}
131139

140+
if int(d.Header.PacketLength) > i.Len()-dataStart {
141+
err = ErrTruncatedPESPackets
142+
return
143+
}
144+
132145
// Seek to data
133146
i.Seek(dataStart)
134147

0 commit comments

Comments
 (0)