@@ -21,17 +21,17 @@ class IMG4PayloadTransform : public Transform
2121
2222 virtual bool Decode (const DataBuffer& input, DataBuffer& output, const std::map<std::string, DataBuffer>& params) override
2323 {
24- DERItem* item = new DERItem ;
25- item-> data = (DERByte *)input.GetData ();
26- item-> length = input.GetLength ();
24+ DERItem item = {} ;
25+ item. data = (DERByte *)input.GetData ();
26+ item. length = input.GetLength ();
2727
28- Img4Payload * payload = new Img4Payload ;
29- DERImg4DecodePayload (item, payload);
30-
31- if (!payload-> payload .data || !payload-> payload .length )
28+ Img4Payload payload = {} ;
29+ if ( auto result = DERImg4DecodePayload (& item, & payload); (result != DR_Success) && (result != DR_DecodeError))
30+ return false ;
31+ if (!payload. payload .data || !payload. payload .length )
3232 return false ;
3333
34- output = DataBuffer (payload-> payload .data , payload-> payload .length );
34+ output = DataBuffer (payload. payload .data , payload. payload .length );
3535
3636 return true ;
3737 }
@@ -45,7 +45,7 @@ class IMG4PayloadTransform : public Transform
4545 }
4646
4747 static void der_put_ia5 (std::vector<uint8_t >& v, const void * s, size_t len) {
48- v.push_back (0x16 ); // IA5String
48+ v.push_back (0x16 ); // IA5String
4949 der_put_len (v, len);
5050 const uint8_t * p = static_cast <const uint8_t *>(s);
5151 v.insert (v.end (), p, p + len);
@@ -153,15 +153,24 @@ class IMG4PayloadTransform : public Transform
153153 // parse up to the first 5 elements to find the magic "IM4P"
154154 for (int i = 0 ; i < 5 && offset < seqEnd; ++i)
155155 {
156+ if (offset >= headerLength)
157+ return false ;
158+
156159 if (seqEnd - offset < 2 )
157160 return false ;
158161 uint8_t tag = data[offset++];
159- auto [elementLen, elementLenHdr] = parseDerLen (data + offset, seqEnd - offset);
162+ if (offset >= headerLength)
163+ return false ;
164+
165+ auto [elementLen, elementLenHdr] = parseDerLen (data + offset, std::min (seqEnd - offset, headerLength - offset));
160166 if (!elementLen || !elementLenHdr || (elementLen > (seqEnd - offset - elementLenHdr)))
161167 return false ;
162168 offset += elementLenHdr;
169+ if (offset + elementLen > headerLength)
170+ return false ;
163171 if ((tag == 0x16 ) && (elementLen == 4 ) && memcmp (data + offset, " IM4P" , 4 ) == 0 )
164172 return true ;
173+ offset += elementLen;
165174 }
166175
167176 return false ;
0 commit comments