]> granicus.if.org Git - php/commitdiff
printf: Unify error case
authorNikita Popov <nikita.ppv@gmail.com>
Wed, 22 Apr 2020 08:13:25 +0000 (10:13 +0200)
committerNikita Popov <nikita.ppv@gmail.com>
Wed, 22 Apr 2020 08:18:03 +0000 (10:18 +0200)
There were a couple of places using efree() on result, which works,
but is very fishy. Unify error handling with goto.

ext/standard/formatted_print.c

index 52c7cc94d2eb41de119882d367e21ecbb26b4046..1385edf52d193bfca9e75e72a40072bd0ea94411 100644 (file)
@@ -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;
 }
 /* }}} */