Skip to content

Commit 40ceafc

Browse files
committed
Fix number of required parameters in printf
If n$ references are involved, the maximum argnum referenced may not the one at the end. Store it explicitly.
1 parent 94fba02 commit 40ceafc

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

ext/standard/formatted_print.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ php_formatted_print(char *format, size_t format_len, zval *args, int argc, int n
399399
char *temppos, padding;
400400
zend_string *result;
401401
int always_sign;
402-
int bad_arg_number = 0;
402+
int max_missing_argnum = -1;
403403

404404
result = zend_string_alloc(size, 0);
405405

@@ -527,7 +527,7 @@ php_formatted_print(char *format, size_t format_len, zval *args, int argc, int n
527527
PRINTF_DEBUG(("sprintf: format character='%c'\n", *format));
528528

529529
if (argnum >= argc) {
530-
bad_arg_number = 1;
530+
max_missing_argnum = MAX(max_missing_argnum, argnum);
531531
continue;
532532
}
533533

@@ -626,12 +626,12 @@ php_formatted_print(char *format, size_t format_len, zval *args, int argc, int n
626626
}
627627
}
628628

629-
if (bad_arg_number == 1) {
629+
if (max_missing_argnum >= 0) {
630630
efree(result);
631631
if (nb_additional_parameters == -1) {
632-
zend_value_error("The arguments array must contain %d items, %d given", argnum + 1, argc);
632+
zend_value_error("The arguments array must contain %d items, %d given", max_missing_argnum + 1, argc);
633633
} else {
634-
zend_argument_count_error("%d parameters are required, %d given", argnum + nb_additional_parameters + 1, argc + nb_additional_parameters);
634+
zend_argument_count_error("%d parameters are required, %d given", max_missing_argnum + nb_additional_parameters + 1, argc + nb_additional_parameters);
635635
}
636636
return NULL;
637637
}

ext/standard/tests/strings/sprintf_error.phpt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ try {
6060
echo $e->getMessage(), "\n";
6161
}
6262

63+
try {
64+
var_dump(sprintf('%100$d %d'));
65+
} catch (\ArgumentCountError $e) {
66+
echo $e->getMessage(), "\n";
67+
}
68+
6369
echo "Done";
6470
?>
6571
--EXPECTF--
@@ -75,4 +81,5 @@ sprintf() expects at least %d parameter, %d given
7581
3 parameters are required, 1 given
7682
4 parameters are required, 2 given
7783
4 parameters are required, 1 given
84+
101 parameters are required, 1 given
7885
Done

0 commit comments

Comments
 (0)