Skip to content

Commit 02e5539

Browse files
committed
printf: Unify error case
There were a couple of places using efree() on result, which works, but is very fishy. Unify error handling with goto.
1 parent 30a5f3d commit 02e5539

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

ext/standard/formatted_print.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -447,9 +447,8 @@ php_formatted_print(char *format, size_t format_len, zval *args, int argc, int n
447447
argnum = php_sprintf_getnumber(&format, &format_len);
448448

449449
if (argnum <= 0) {
450-
zend_string_efree(result);
451450
zend_value_error("Argument number must be greater than zero");
452-
return NULL;
451+
goto fail;
453452
}
454453
argnum--;
455454
format++; /* skip the '$' */
@@ -477,8 +476,7 @@ php_formatted_print(char *format, size_t format_len, zval *args, int argc, int n
477476
padding = *format;
478477
} else {
479478
zend_value_error("Missing padding character");
480-
zend_string_efree(result);
481-
return NULL;
479+
goto fail;
482480
}
483481
} else {
484482
PRINTF_DEBUG(("sprintf: end of modifiers\n"));
@@ -494,9 +492,8 @@ php_formatted_print(char *format, size_t format_len, zval *args, int argc, int n
494492
if (isdigit((int)*format)) {
495493
PRINTF_DEBUG(("sprintf: getting width\n"));
496494
if ((width = php_sprintf_getnumber(&format, &format_len)) < 0) {
497-
efree(result);
498495
zend_value_error("Width must be greater than zero and less than %d", INT_MAX);
499-
return NULL;
496+
goto fail;
500497
}
501498
adjusting |= ADJ_WIDTH;
502499
} else {
@@ -511,9 +508,8 @@ php_formatted_print(char *format, size_t format_len, zval *args, int argc, int n
511508
PRINTF_DEBUG(("sprintf: getting precision\n"));
512509
if (isdigit((int)*format)) {
513510
if ((precision = php_sprintf_getnumber(&format, &format_len)) < 0) {
514-
efree(result);
515511
zend_value_error("Precision must be greater than zero and less than %d", INT_MAX);
516-
return NULL;
512+
goto fail;
517513
}
518514
adjusting |= ADJ_PRECISION;
519515
expprec = 1;
@@ -633,20 +629,23 @@ php_formatted_print(char *format, size_t format_len, zval *args, int argc, int n
633629
}
634630

635631
if (max_missing_argnum >= 0) {
636-
efree(result);
637632
if (nb_additional_parameters == -1) {
638633
zend_value_error("The arguments array must contain %d items, %d given", max_missing_argnum + 1, argc);
639634
} else {
640635
zend_argument_count_error("%d parameters are required, %d given", max_missing_argnum + nb_additional_parameters + 1, argc + nb_additional_parameters);
641636
}
642-
return NULL;
637+
goto fail;
643638
}
644639

645640
exit:
646641
/* possibly, we have to make sure we have room for the terminating null? */
647642
ZSTR_VAL(result)[outpos]=0;
648643
ZSTR_LEN(result) = outpos;
649644
return result;
645+
646+
fail:
647+
zend_string_efree(result);
648+
return NULL;
650649
}
651650
/* }}} */
652651

0 commit comments

Comments
 (0)