Skip to content

Commit

Permalink
improve check on H264 padding
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 committed May 5, 2024
1 parent 3f62e11 commit d773e6c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
13 changes: 11 additions & 2 deletions pkg/format/rtph264/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ func joinFragments(fragments [][]byte, size int) []byte {
return ret
}

func isAllZero(buf []byte) bool {
for _, b := range buf {
if b != 0 {
return false
}
}
return true
}

// Decoder is a RTP/H264 decoder.
// Specification: https://datatracker.ietf.org/doc/html/rfc6184
type Decoder struct {
Expand Down Expand Up @@ -125,8 +134,8 @@ func (d *Decoder) decodeNALUs(pkt *rtp.Packet) ([][]byte, error) {
size := uint16(payload[0])<<8 | uint16(payload[1])
payload = payload[2:]

// avoid final padding
if size == 0 {
// discard padding
if size == 0 && isAllZero(payload) {
break
}

Expand Down
3 changes: 3 additions & 0 deletions pkg/format/rtph264/testdata/fuzz/FuzzDecoder/3b0600afabf53c93
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
go test fuzz v1
[]byte("0")
[]byte("8\x00\x000")
4 changes: 0 additions & 4 deletions pkg/format/rtph265/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,6 @@ func (d *Decoder) decodeNALUs(pkt *rtp.Packet) ([][]byte, error) {
size := uint16(payload[0])<<8 | uint16(payload[1])
payload = payload[2:]

if size == 0 {
break
}

if int(size) > len(payload) {
return nil, fmt.Errorf("invalid aggregation unit (invalid size)")
}
Expand Down

0 comments on commit d773e6c

Please sign in to comment.