@@ -61,6 +61,11 @@ func CreateAVCDecConfRec(spsNalus [][]byte, ppsNalus [][]byte, includePS bool) (
6161
6262// DecodeAVCDecConfRec - decode an AVCDecConfRec
6363func DecodeAVCDecConfRec (data []byte ) (DecConfRec , error ) {
64+ // Check minimum length for fixed header (6 bytes)
65+ if len (data ) < 6 {
66+ return DecConfRec {}, fmt .Errorf ("data too short for AVC decoder configuration record: %d bytes" , len (data ))
67+ }
68+
6469 configurationVersion := data [0 ] // Should be 1
6570 if configurationVersion != 1 {
6671 return DecConfRec {}, fmt .Errorf ("AVC decoder configuration record version %d unknown" ,
@@ -75,29 +80,56 @@ func DecodeAVCDecConfRec(data []byte) (DecConfRec, error) {
7580 }
7681 numSPS := data [5 ] & 0x1f // 5 bits following 3 reserved bits
7782 pos := 6
83+
7884 spsNALUs := make ([][]byte , 0 , 1 )
7985 for i := 0 ; i < int (numSPS ); i ++ {
86+ // Check if we have enough bytes to read NALU length
87+ if pos + 2 > len (data ) {
88+ return DecConfRec {}, fmt .Errorf ("not enough data to read SPS NALU length at position %d" , pos )
89+ }
8090 naluLength := int (binary .BigEndian .Uint16 (data [pos : pos + 2 ]))
8191 pos += 2
92+
93+ // Check if we have enough bytes to read NALU
94+ if pos + naluLength > len (data ) {
95+ return DecConfRec {}, fmt .Errorf ("not enough data to read SPS NALU of length %d at position %d" , naluLength , pos )
96+ }
8297 spsNALUs = append (spsNALUs , data [pos :pos + naluLength ])
8398 pos += naluLength
8499 }
85- ppsNALUs := make ([][]byte , 0 , 1 )
100+
101+ // Check if we have enough bytes to read numPPS
102+ if pos >= len (data ) {
103+ return DecConfRec {}, fmt .Errorf ("not enough data to read number of PPS at position %d" , pos )
104+ }
86105 numPPS := data [pos ]
87106 pos ++
107+
108+ ppsNALUs := make ([][]byte , 0 , 1 )
88109 for i := 0 ; i < int (numPPS ); i ++ {
110+ // Check if we have enough bytes to read NALU length
111+ if pos + 2 > len (data ) {
112+ return DecConfRec {}, fmt .Errorf ("not enough data to read PPS NALU length at position %d" , pos )
113+ }
89114 naluLength := int (binary .BigEndian .Uint16 (data [pos : pos + 2 ]))
90115 pos += 2
116+
117+ // Check if we have enough bytes to read NALU
118+ if pos + naluLength > len (data ) {
119+ return DecConfRec {}, fmt .Errorf ("not enough data to read PPS NALU of length %d at position %d" , naluLength , pos )
120+ }
91121 ppsNALUs = append (ppsNALUs , data [pos :pos + naluLength ])
92122 pos += naluLength
93123 }
124+
94125 adcr := DecConfRec {
95126 AVCProfileIndication : AVCProfileIndication ,
96127 ProfileCompatibility : ProfileCompatibility ,
97128 AVCLevelIndication : AVCLevelIndication ,
98129 SPSnalus : spsNALUs ,
99130 PPSnalus : ppsNALUs ,
100131 }
132+
101133 // The rest of this structure may vary
102134 // ISO/IEC 14496-15 2017 says that
103135 // Compatible extensions to this record will extend it and
@@ -114,6 +146,10 @@ func DecodeAVCDecConfRec(data []byte) (DecConfRec, error) {
114146 adcr .NoTrailingInfo = true
115147 return adcr , nil
116148 }
149+ // Check if we have enough bytes for the trailing info
150+ if pos + 4 > len (data ) {
151+ return DecConfRec {}, fmt .Errorf ("not enough data for trailing info at position %d" , pos )
152+ }
117153 adcr .ChromaFormat = data [pos ] & 0x03
118154 adcr .BitDepthLumaMinus1 = data [pos + 1 ] & 0x07
119155 adcr .BitDepthChromaMinus1 = data [pos + 2 ] & 0x07
0 commit comments