@@ -150,13 +150,14 @@ static uint32_t parse_ascii_hex32(const char *s)
150150/**
151151 * decode_programmer_archive() - Attempt to decode a programmer CPIO archive
152152 * @blob: Loaded image to be decoded as archive
153- * @images: List of Sahara images, with @images[0] populated
153+ * @images: List of Sahara images to populate
154154 *
155- * The single blob provided in @images[0] might be a CPIO archive containing
156- * Sahara images, in files with names in the format "<id>:<filename>". Load
157- * each such Sahara image into the relevant spot in the @images array.
155+ * The blob might be a CPIO archive containing Sahara images, in files with
156+ * names in the format "<id>:<filename>". Load each such Sahara image into the
157+ * relevant spot in the @images array.
158158 *
159- * The original blob (in @images[0]) is freed once it has been consumed.
159+ * The blob is always consumed (freed) on both success and error paths.
160+ * On error, any partially-populated @images entries are also freed.
160161 *
161162 * Returns: 0 if no archive was found, 1 if archive was decoded, -1 on error
162163 */
@@ -178,13 +179,13 @@ static int decode_programmer_archive(struct sahara_image *blob, struct sahara_im
178179 for (;;) {
179180 if (ptr + sizeof (* hdr ) > end ) {
180181 ux_err ("programmer archive is truncated\n" );
181- return -1 ;
182+ goto err ;
182183 }
183184 hdr = ptr ;
184185
185186 if (memcmp (hdr -> c_magic , "070701" , 6 )) {
186187 ux_err ("expected cpio header in programmer archive\n" );
187- return -1 ;
188+ goto err ;
188189 }
189190
190191 filesize = parse_ascii_hex32 (hdr -> c_filesize );
@@ -193,12 +194,12 @@ static int decode_programmer_archive(struct sahara_image *blob, struct sahara_im
193194 ptr += sizeof (* hdr );
194195 if (ptr + namesize > end || ptr + filesize + namesize > end ) {
195196 ux_err ("programmer archive is truncated\n" );
196- return -1 ;
197+ goto err ;
197198 }
198199
199200 if (namesize > sizeof (name )) {
200201 ux_err ("unexpected filename length in progammer archive\n" );
201- return -1 ;
202+ goto err ;
202203 }
203204 memcpy (name , ptr , namesize );
204205
@@ -209,7 +210,7 @@ static int decode_programmer_archive(struct sahara_image *blob, struct sahara_im
209210 id = strtoul (tok , NULL , 0 );
210211 if (id == 0 || id >= MAPPING_SZ ) {
211212 ux_err ("invalid image id \"%s\" in programmer archive\n" , tok );
212- return -1 ;
213+ goto err ;
213214 }
214215
215216 ptr += namesize ;
@@ -231,6 +232,13 @@ static int decode_programmer_archive(struct sahara_image *blob, struct sahara_im
231232 blob -> len = 0 ;
232233
233234 return 1 ;
235+
236+ err :
237+ sahara_images_free (images , MAPPING_SZ );
238+ free (blob -> ptr );
239+ blob -> ptr = NULL ;
240+ blob -> len = 0 ;
241+ return -1 ;
234242}
235243
236244/**
@@ -335,6 +343,10 @@ static int decode_sahara_config(struct sahara_image *blob, struct sahara_image *
335343 return 1 ;
336344
337345err_free_doc :
346+ sahara_images_free (images , MAPPING_SZ );
347+ free (blob -> ptr );
348+ blob -> ptr = NULL ;
349+ blob -> len = 0 ;
338350 xmlFreeDoc (doc );
339351 free (blob_name_buf );
340352 return -1 ;
@@ -747,6 +759,9 @@ static int qdl_flash(int argc, char **argv)
747759 vip_gen_finalize (qdl );
748760
749761 qdl_close (qdl );
762+
763+ sahara_images_free (sahara_images , MAPPING_SZ );
764+
750765 free_programs ();
751766 free_patches ();
752767
0 commit comments