Skip to content

Commit 8505e13

Browse files
committed
Unify all phar tar bail paths
1 parent ae4928a commit 8505e13

File tree

1 file changed

+16
-48
lines changed

1 file changed

+16
-48
lines changed

ext/phar/tar.c

Lines changed: 16 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -325,9 +325,7 @@ zend_result phar_parse_tarfile(php_stream* fp, char *fname, size_t fname_len, ch
325325
if (error) {
326326
spprintf(error, 4096, "phar error: \"%s\" is a corrupted tar file (truncated)", fname);
327327
}
328-
php_stream_close(fp);
329-
phar_destroy_phar_data(myphar);
330-
return FAILURE;
328+
goto bail;
331329
}
332330
}
333331

@@ -337,9 +335,7 @@ zend_result phar_parse_tarfile(php_stream* fp, char *fname, size_t fname_len, ch
337335
if (error) {
338336
spprintf(error, 4096, "phar error: \"%s\" is a corrupted tar file (truncated)", fname);
339337
}
340-
php_stream_close(fp);
341-
phar_destroy_phar_data(myphar);
342-
return FAILURE;
338+
goto bail;
343339
}
344340

345341
hdr = (tar_header*) buf;
@@ -365,9 +361,7 @@ zend_result phar_parse_tarfile(php_stream* fp, char *fname, size_t fname_len, ch
365361
if (error) {
366362
spprintf(error, 4096, "phar error: \"%s\" is a corrupted tar file (invalid entry size)", fname);
367363
}
368-
php_stream_close(fp);
369-
phar_destroy_phar_data(myphar);
370-
return FAILURE;
364+
goto bail;
371365
}
372366
entry.filename = zend_string_alloc(entry.uncompressed_filesize, myphar->is_persistent);
373367
if (myphar->is_persistent) {
@@ -381,9 +375,7 @@ zend_result phar_parse_tarfile(php_stream* fp, char *fname, size_t fname_len, ch
381375
if (error) {
382376
spprintf(error, 4096, "phar error: \"%s\" is a corrupted tar file (truncated)", fname);
383377
}
384-
php_stream_close(fp);
385-
phar_destroy_phar_data(myphar);
386-
return FAILURE;
378+
goto bail;
387379
}
388380

389381
/* skip blank stuff */
@@ -396,9 +388,7 @@ zend_result phar_parse_tarfile(php_stream* fp, char *fname, size_t fname_len, ch
396388
if (error) {
397389
spprintf(error, 4096, "phar error: \"%s\" is a corrupted tar file (truncated)", fname);
398390
}
399-
php_stream_close(fp);
400-
phar_destroy_phar_data(myphar);
401-
return FAILURE;
391+
goto bail;
402392
}
403393

404394
read = php_stream_read(fp, buf, sizeof(buf));
@@ -408,9 +398,7 @@ zend_result phar_parse_tarfile(php_stream* fp, char *fname, size_t fname_len, ch
408398
if (error) {
409399
spprintf(error, 4096, "phar error: \"%s\" is a corrupted tar file (truncated)", fname);
410400
}
411-
php_stream_close(fp);
412-
phar_destroy_phar_data(myphar);
413-
return FAILURE;
401+
goto bail;
414402
}
415403
continue;
416404
} else if (!last_was_longlink && !old && hdr->prefix[0] != 0) {
@@ -470,9 +458,7 @@ zend_result phar_parse_tarfile(php_stream* fp, char *fname, size_t fname_len, ch
470458
spprintf(error, 4096, "phar error: \"%s\" is a corrupted tar file (checksum mismatch of file \"%s\")", fname, ZSTR_VAL(entry.filename));
471459
}
472460
zend_string_release_ex(entry.filename, myphar->is_persistent);
473-
php_stream_close(fp);
474-
phar_destroy_phar_data(myphar);
475-
return FAILURE;
461+
goto bail;
476462
}
477463

478464
uint32_t entry_mode = phar_tar_oct_number(hdr->mode, sizeof(hdr->mode));
@@ -503,9 +489,7 @@ zend_result phar_parse_tarfile(php_stream* fp, char *fname, size_t fname_len, ch
503489
spprintf(error, 4096, "phar error: \"%s\" is a corrupted tar file - hard link to non-existent file \"%.*s\"", fname, (int)linkname_len, hdr->linkname);
504490
}
505491
zend_string_release_ex(entry.filename, myphar->is_persistent);
506-
php_stream_close(fp);
507-
phar_destroy_phar_data(myphar);
508-
return FAILURE;
492+
goto bail;
509493
}
510494
entry.link = estrndup(hdr->linkname, linkname_len);
511495
} else if (entry.tar_type == TAR_SYMLINK) {
@@ -525,9 +509,7 @@ zend_result phar_parse_tarfile(php_stream* fp, char *fname, size_t fname_len, ch
525509
if (error) {
526510
spprintf(error, 4096, "phar error: tar-based phar \"%s\" has invalid metadata in magic file \"%s\"", fname, ZSTR_VAL(entry.filename));
527511
}
528-
php_stream_close(fp);
529-
phar_destroy_phar_data(myphar);
530-
return FAILURE;
512+
goto bail;
531513
}
532514
}
533515

@@ -537,9 +519,7 @@ zend_result phar_parse_tarfile(php_stream* fp, char *fname, size_t fname_len, ch
537519
if (error) {
538520
spprintf(error, 4096, "phar error: tar-based phar \"%s\" has alias that is larger than 511 bytes, cannot process", fname);
539521
}
540-
php_stream_close(fp);
541-
phar_destroy_phar_data(myphar);
542-
return FAILURE;
522+
goto bail;
543523
}
544524

545525
read = php_stream_read(fp, buf, size);
@@ -558,9 +538,7 @@ zend_result phar_parse_tarfile(php_stream* fp, char *fname, size_t fname_len, ch
558538
spprintf(error, 4096, "phar error: invalid alias \"%s\" in tar-based phar \"%s\"", buf, fname);
559539
}
560540

561-
php_stream_close(fp);
562-
phar_destroy_phar_data(myphar);
563-
return FAILURE;
541+
goto bail;
564542
}
565543

566544
actual_alias = pestrndup(buf, size, myphar->is_persistent);
@@ -572,9 +550,7 @@ zend_result phar_parse_tarfile(php_stream* fp, char *fname, size_t fname_len, ch
572550
spprintf(error, 4096, "phar error: Unable to read alias from tar-based phar \"%s\"", fname);
573551
}
574552

575-
php_stream_close(fp);
576-
phar_destroy_phar_data(myphar);
577-
return FAILURE;
553+
goto bail;
578554
}
579555
}
580556

@@ -588,9 +564,7 @@ zend_result phar_parse_tarfile(php_stream* fp, char *fname, size_t fname_len, ch
588564
if (error) {
589565
spprintf(error, 4096, "phar error: \"%s\" is a corrupted tar file (truncated)", fname);
590566
}
591-
php_stream_close(fp);
592-
phar_destroy_phar_data(myphar);
593-
return FAILURE;
567+
goto bail;
594568
}
595569
}
596570

@@ -605,9 +579,7 @@ zend_result phar_parse_tarfile(php_stream* fp, char *fname, size_t fname_len, ch
605579
if (error) {
606580
spprintf(error, 4096, "phar error: \"%s\" is a corrupted tar file (truncated)", fname);
607581
}
608-
php_stream_close(fp);
609-
phar_destroy_phar_data(myphar);
610-
return FAILURE;
582+
goto bail;
611583
}
612584
}
613585

@@ -619,12 +591,10 @@ zend_result phar_parse_tarfile(php_stream* fp, char *fname, size_t fname_len, ch
619591

620592
/* ensure signature set */
621593
if (!myphar->is_data && PHAR_G(require_hash) && !myphar->signature) {
622-
php_stream_close(fp);
623-
phar_destroy_phar_data(myphar);
624594
if (error) {
625595
spprintf(error, 0, "tar-based phar \"%s\" does not have a signature", fname);
626596
}
627-
return FAILURE;
597+
goto bail;
628598
}
629599

630600
myphar->fname = pestrndup(fname, fname_len, myphar->is_persistent);
@@ -651,9 +621,7 @@ zend_result phar_parse_tarfile(php_stream* fp, char *fname, size_t fname_len, ch
651621
if (error) {
652622
spprintf(error, 4096, "phar error: Unable to add tar-based phar \"%s\" to phar registry", fname);
653623
}
654-
php_stream_close(fp);
655-
phar_destroy_phar_data(myphar);
656-
return FAILURE;
624+
goto bail;
657625
}
658626

659627
myphar = actual;

0 commit comments

Comments
 (0)