diff --git a/stream/mts/psi/psi.go b/stream/mts/psi/psi.go index 91d8fade..cc9dc8f9 100644 --- a/stream/mts/psi/psi.go +++ b/stream/mts/psi/psi.go @@ -52,6 +52,8 @@ const ( pmtID = 0x02 ) +const TotalSyntaxSecLen = 180 + // Consts relating to time description const ( TimeDescTag = 234 @@ -251,8 +253,8 @@ func (p *PSIBytes) AddDescriptor(tag int, data []byte) error { i, desc := p.HasDescriptor(tag) if desc == nil { - p.createDescriptor(tag, data) - return nil + err := p.createDescriptor(tag, data) + return err } oldDescLen := desc.len() @@ -308,9 +310,12 @@ func (p *PSIBytes) HasDescriptor(tag int) (int, Descriptor) { // createDescriptor creates a descriptor in a psi given a tag and data. It does so // by resizing the psi, shifting existing data down and copying in new descriptor // in new space. -func (p *PSIBytes) createDescriptor(tag int, data []byte) { +func (p *PSIBytes) createDescriptor(tag int, data []byte) error { curProgLen := p.ProgramInfoLen() oldSyntaxSectionLen := SyntaxSecLenFrom(*p) + if TotalSyntaxSecLen-(oldSyntaxSectionLen+2+len(data)) <= 0 { + return errors.New("Not enough space in psi to create descriptor.") + } dataLen := len(data) newDescIdx := DescriptorsIdx + curProgLen newDescLen := dataLen + 2 @@ -332,6 +337,8 @@ func (p *PSIBytes) createDescriptor(tag int, data []byte) { newSyntaxSectionLen := int(oldSyntaxSectionLen) + addedLen p.setSectionLen(newSyntaxSectionLen) UpdateCrc((*p)[1:]) + + return nil } // setProgInfoLen sets the program information length in a psi with a pmt.