Comcast / gots

MPEG Transport Stream handling in Go
Other
305 stars 88 forks source link

Panic when calling pes.NewPESHeader #52

Open itsjamie opened 7 years ago

itsjamie commented 7 years ago

While building a tool to do an analysis of MPEGTS for HLS delivery, ran into a panic while calling pes.NewPESHeader.

pes := new(pESHeader)
var err error

if CheckLength(pesBytes, "PES", 6) {

    pes.packetStartCodePrefix = uint32(pesBytes[0])<<16 | uint32(pesBytes[1])<<8 | uint32(pesBytes[2])

    pes.streamId = uint8(pesBytes[3])
    pes.pesPacketLength = uint16(pesBytes[4])<<8 | uint16(pesBytes[5])
--->    pes.dataAlignment = pesBytes[6]&0x04 != 0
    dataStartIndex := 6

The code panics on the pesBytes[6] check being out of bounds.

I think that CheckLength should be called with a value of seven in this case since it's checking the length of the byte array which is one-based, but the index is zero based.

Thank you for this code, it made getting started much easier!

guygrigsby commented 7 years ago

Thanks for the bug report and I am glad that gots has helped you get started.

There are 6 bytes that are shared at the beginning of all PES packets. packet_start_code_prefix is 3 bytes, stream_id is 1 byte, and PES_packet_length is 2 bytes. data_alignment_indicator is not always present. I think the right thing to do is to move pes.dataAlignment = pesBytes[6]&0x04 != 0 down into the conditional that checks if pes.optionalFieldsExist(). I'll look more into it soon.

tmm1 commented 6 years ago

Ping