[php-src] master: standard: Improve `sprintf()` error message when providing an empty arg specifier (#23706)
| From: | Tim Düsterhus via GitHub | Date: | Wed, 16 Sep 2026 13:19:13 +0000 |
| Subject: | [php-src] master: standard: Improve `sprintf()` error message when providing an empty arg specifier (#23706) | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-141231@lists.php.net to get a copy of this message | ||
Author: Tim Düsterhus (TimWolla)
Committer: GitHub (web-flow)
Pusher: TimWolla
Date: 2026-09-16T15:19:10+02:00
Commit: https://github.com/php/php-src/commit/041b57689d7cbfef4aa4ba89e47b0848ad9c8964
Raw diff: https://github.com/php/php-src/commit/041b57689d7cbfef4aa4ba89e47b0848ad9c8964.diff
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 $.
Changed paths:
M ext/standard/formatted_print.c
M ext/standard/tests/strings/bug69751.phpt
M ext/standard/tests/strings/vfprintf_error4.phpt
Diff:
diff --git a/ext/standard/formatted_print.c b/ext/standard/formatted_print.c
index f35d196fb971..f5ac2ce58456 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 f75af897e0e5..0820142d304f 100644
--- a/ext/standard/tests/strings/bug69751.phpt
+++ b/ext/standard/tests/strings/bug69751.phpt
@@ -23,6 +23,6 @@ try {
?>
--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 7c4578ee4090..ac938c1ad609 100644
--- a/ext/standard/tests/strings/vfprintf_error4.phpt
+++ b/ext/standard/tests/strings/vfprintf_error4.phpt
@@ -35,4 +35,4 @@ unlink( $file );
--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.