Browse code

try to minimize the err cleanup path

Signed-off-by: Sebastian Andrzej Siewior <sebastian@breakpoint.cc>
Signed-off-by: Mickey Sola <msola@sourcefire.com>

Sebastian Andrzej Siewior authored on 2014/09/03 05:44:41
Showing 1 changed files
... ...
@@ -254,8 +254,9 @@ int unrar_open(int fd, const char *dirname, unrar_state_t *state)
254 254
 	int ofd, retval;
255 255
 	char filename[1024];
256 256
 	unpack_data_t *unpack_data;
257
-	unrar_main_header_t *main_hdr;
257
+	unrar_main_header_t *main_hdr = NULL;
258 258
 	off_t offset;
259
+	int ret = 0;
259 260
 
260 261
 
261 262
     if(!state)
... ...
@@ -274,34 +275,29 @@ int unrar_open(int fd, const char *dirname, unrar_state_t *state)
274 274
     unrar_dbgmsg("UNRAR: Head Size: %.4x\n", main_hdr->head_size);
275 275
 
276 276
     if(main_hdr->flags & MHD_PASSWORD) {
277
-	free(main_hdr);
278
-	return UNRAR_PASSWD;
277
+	ret = UNRAR_PASSWD;
278
+	goto err_mhdr;
279 279
     }
280 280
 
281 281
     snprintf(filename,1024,"%s"PATHSEP"comments", dirname);
282 282
     if(mkdir(filename,0700)) {
283 283
 	unrar_dbgmsg("UNRAR: Unable to create comment temporary directory\n");
284
-	free(main_hdr);
285
-	return UNRAR_ERR;
284
+	goto err_mhdr;
286 285
     }
287 286
     state->comment_dir = strdup(filename);
288 287
     if(!state->comment_dir) {
289
-	free(main_hdr);
290
-	return UNRAR_EMEM;
288
+	ret = UNRAR_EMEM;
289
+	goto err_mhdr;
291 290
     }
292 291
 
293
-    if(main_hdr->head_size < SIZEOF_NEWMHD) {
294
-	free(main_hdr);
295
-	free(state->comment_dir);
296
-	return UNRAR_ERR;
297
-    }
292
+    if(main_hdr->head_size < SIZEOF_NEWMHD)
293
+	goto err_cmt_dir;
298 294
 
299 295
     unpack_data = (unpack_data_t *) calloc(1, sizeof(unpack_data_t));
300 296
     if(!unpack_data) {
301
-	free(main_hdr);
302
-	free(state->comment_dir);
303 297
 	unrar_dbgmsg("UNRAR: malloc failed for unpack_data\n");
304
-	return UNRAR_EMEM;
298
+	ret = UNRAR_EMEM;
299
+	goto err_cmt_dir;
305 300
     }
306 301
     unpack_data->rarvm_data.mem = NULL;
307 302
     unpack_data->old_filter_lengths = NULL;
... ...
@@ -318,10 +314,7 @@ int unrar_open(int fd, const char *dirname, unrar_state_t *state)
318 318
 	offset = lseek(fd, 0, SEEK_CUR);
319 319
         if (offset == -1) {
320 320
             unrar_dbgmsg("UNRAR: seek: lseek() call failed in unrar_open\n");
321
-            free(main_hdr);
322
-            free(state->comment_dir);
323
-            free(unpack_data);
324
-            return UNRAR_ERR;
321
+	    goto err_unpack_data;
325 322
         }
326 323
 	unrar_dbgmsg("UNRAR: Offset: %x\n", offset);
327 324
 	comment_header = read_header(fd, COMM_HEAD);
... ...
@@ -336,13 +329,7 @@ int unrar_open(int fd, const char *dirname, unrar_state_t *state)
336 336
 	    if(ofd < 0) {
337 337
 		unrar_dbgmsg("UNRAR: ERROR: Failed to open output file\n");
338 338
 		free(comment_header);
339
-		free(main_hdr);
340
-		ppm_destructor(&unpack_data->ppm_data);
341
-		rar_init_filters(unpack_data);
342
-		unpack_free_data(unpack_data);
343
-		free(unpack_data);
344
-		free(state->comment_dir);
345
-		return UNRAR_ERR;
339
+		goto err_unpack_data;
346 340
 	    } else {
347 341
 		if(comment_header->method == 0x30) {
348 342
 		    unrar_dbgmsg("UNRAR: Copying stored comment (not packed)\n");
... ...
@@ -362,26 +349,13 @@ int unrar_open(int fd, const char *dirname, unrar_state_t *state)
362 362
 	}
363 363
         if (lseek(fd, offset, SEEK_SET) == -1) {
364 364
             unrar_dbgmsg("UNRAR: seek: call to lseek() failed in unrar_open: %ld\n", offset);
365
-            free(main_hdr);
366
-            ppm_destructor(&unpack_data->ppm_data);
367
-            rar_init_filters(unpack_data);
368
-            unpack_free_data(unpack_data);
369
-            free(unpack_data);
370
-            free(state->comment_dir);
371
-            return UNRAR_ERR;
365
+	    goto err_unpack_data;
372 366
         }
373 367
     }
374 368
 
375 369
     if(main_hdr->head_size > SIZEOF_NEWMHD) {
376
-	if(!lseek(fd, main_hdr->head_size - SIZEOF_NEWMHD, SEEK_CUR)) {
377
-	    free(main_hdr);
378
-	    ppm_destructor(&unpack_data->ppm_data);
379
-	    rar_init_filters(unpack_data);
380
-	    unpack_free_data(unpack_data);
381
-	    free(unpack_data);
382
-	    free(state->comment_dir);
383
-	    return UNRAR_ERR;
384
-	}
370
+	if (!lseek(fd, main_hdr->head_size - SIZEOF_NEWMHD, SEEK_CUR))
371
+	    goto err_unpack_data;
385 372
     }
386 373
 
387 374
     state->unpack_data = unpack_data;
... ...
@@ -391,6 +365,19 @@ int unrar_open(int fd, const char *dirname, unrar_state_t *state)
391 391
     state->fd = fd;
392 392
 
393 393
     return UNRAR_OK;
394
+
395
+err_unpack_data:
396
+    ppm_destructor(&unpack_data->ppm_data);
397
+    rar_init_filters(unpack_data);
398
+    unpack_free_data(unpack_data);
399
+    free(unpack_data);
400
+err_cmt_dir:
401
+    free(state->comment_dir);
402
+err_mhdr:
403
+    free(main_hdr);
404
+    if (!ret)
405
+	   return UNRAR_ERR;
406
+    return ret;
394 407
 }
395 408
 
396 409
 int unrar_extract_next_prepare(unrar_state_t *state, const char *dirname)