gots icon indicating copy to clipboard operation
gots copied to clipboard

Panic when calling pes.NewPESHeader

Open itsjamie opened this issue 8 years ago • 2 comments

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!

itsjamie avatar Aug 02 '17 14:08 itsjamie

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.

guygrigsby avatar Aug 06 '17 04:08 guygrigsby

Ping

tmm1 avatar May 12 '18 19:05 tmm1