From: Nikita Popov Date: Wed, 22 Apr 2020 08:13:25 +0000 (+0200) Subject: printf: Unify error case X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=02e553963718a5d4d0462c82a9994f147516a007;p=php 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. --- diff --git a/ext/standard/formatted_print.c b/ext/standard/formatted_print.c index 52c7cc94d2..1385edf52d 100644 --- a/ext/standard/formatted_print.c +++ b/ext/standard/formatted_print.c @@ -447,9 +447,8 @@ php_formatted_print(char *format, size_t format_len, zval *args, int argc, int n argnum = php_sprintf_getnumber(&format, &format_len); if (argnum <= 0) { - zend_string_efree(result); zend_value_error("Argument number must be greater than zero"); - return NULL; + goto fail; } argnum--; format++; /* skip the '$' */ @@ -477,8 +476,7 @@ php_formatted_print(char *format, size_t format_len, zval *args, int argc, int n padding = *format; } else { zend_value_error("Missing padding character"); - zend_string_efree(result); - return NULL; + goto fail; } } else { 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 if (isdigit((int)*format)) { PRINTF_DEBUG(("sprintf: getting width\n")); if ((width = php_sprintf_getnumber(&format, &format_len)) < 0) { - efree(result); zend_value_error("Width must be greater than zero and less than %d", INT_MAX); - return NULL; + goto fail; } adjusting |= ADJ_WIDTH; } else { @@ -511,9 +508,8 @@ php_formatted_print(char *format, size_t format_len, zval *args, int argc, int n PRINTF_DEBUG(("sprintf: getting precision\n")); if (isdigit((int)*format)) { if ((precision = php_sprintf_getnumber(&format, &format_len)) < 0) { - efree(result); zend_value_error("Precision must be greater than zero and less than %d", INT_MAX); - return NULL; + goto fail; } adjusting |= ADJ_PRECISION; expprec = 1; @@ -633,13 +629,12 @@ php_formatted_print(char *format, size_t format_len, zval *args, int argc, int n } if (max_missing_argnum >= 0) { - efree(result); if (nb_additional_parameters == -1) { zend_value_error("The arguments array must contain %d items, %d given", max_missing_argnum + 1, argc); } else { zend_argument_count_error("%d parameters are required, %d given", max_missing_argnum + nb_additional_parameters + 1, argc + nb_additional_parameters); } - return NULL; + goto fail; } exit: @@ -647,6 +642,10 @@ exit: ZSTR_VAL(result)[outpos]=0; ZSTR_LEN(result) = outpos; return result; + +fail: + zend_string_efree(result); + return NULL; } /* }}} */