Commit 041b57689d7 for php.net
commit 041b57689d7cbfef4aa4ba89e47b0848ad9c8964
Author: Tim Düsterhus <tim@tideways-gmbh.com>
Date: Wed Sep 16 15:19:10 2026 +0200
standard: Improve `sprintf()` error message when providing an empty arg specifier (#23706)
I misremembered the correct syntax for specifying the argnum in an `sprintf()`
format string and placed the number after the `$` instead of before. This lead
to `sprintf()` seeing an empty number and reporting:
> Argument number specifier must be greater than zero and less than 2147483647
which was misleading. Had the error message shown that the argnum is empty, it
would have been much clearer that it expect the number before the `$`.
diff --git a/ext/standard/formatted_print.c b/ext/standard/formatted_print.c
index f35d196fb97..f5ac2ce5845 100644
--- a/ext/standard/formatted_print.c
+++ b/ext/standard/formatted_print.c
@@ -379,6 +379,11 @@ int php_sprintf_get_argnum(char **format, size_t *format_len) {
return ARG_NUM_NEXT;
}
+ if (UNEXPECTED(temppos == *format)) {
+ zend_value_error("Argument number specifier must not be empty");
+ return ARG_NUM_INVALID;
+ }
+
int argnum = php_sprintf_getnumber(format, format_len);
if (argnum <= 0) {
zend_value_error("Argument number specifier must be greater than zero and less than %d", INT_MAX);
diff --git a/ext/standard/tests/strings/bug69751.phpt b/ext/standard/tests/strings/bug69751.phpt
index f75af897e0e..0820142d304 100644
--- a/ext/standard/tests/strings/bug69751.phpt
+++ b/ext/standard/tests/strings/bug69751.phpt
@@ -23,6 +23,6 @@
?>
--EXPECTF--
-Argument number specifier must be greater than zero and less than %d
+Argument number specifier must not be empty
4 arguments are required, 3 given
Argument number specifier must be greater than zero and less than %d
diff --git a/ext/standard/tests/strings/vfprintf_error4.phpt b/ext/standard/tests/strings/vfprintf_error4.phpt
index 7c4578ee409..ac938c1ad60 100644
--- a/ext/standard/tests/strings/vfprintf_error4.phpt
+++ b/ext/standard/tests/strings/vfprintf_error4.phpt
@@ -35,4 +35,4 @@
--EXPECT--
-- Testing vfprintf() function with other strangeties --
vfprintf(): Argument #1 ($stream) must be of type resource, string given
-Error found: Argument number specifier must be greater than zero and less than 2147483647.
+Error found: Argument number specifier must not be empty.