Skip to content

Fix #69751: Change Error message of sprintf/printf for missing/typo p… #7515

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ext/standard/formatted_print.c
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ int php_sprintf_get_argnum(char **format, size_t *format_len) {

int argnum = php_sprintf_getnumber(format, format_len);
if (argnum <= 0) {
zend_value_error("Argument number must be greater than zero");
zend_value_error("Argument number specifier must be greater than zero and less than %d", INT_MAX);
return ARG_NUM_INVALID;
}

Expand Down
28 changes: 28 additions & 0 deletions ext/standard/tests/strings/bug69751.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
--TEST--
Bug #69751: Change Error message of sprintf/printf for missing/typo position specifier.
--FILE--
<?php

try {
sprintf('%$s, %2$s %1$s', "a", "b");
} catch (ValueError $e) {
echo $e->getMessage(), "\n";
}

try {
sprintf('%3$s, %2$s %1$s', "a", "b");
} catch (ArgumentCountError $e) {
echo $e->getMessage(), "\n";
}

try {
sprintf('%2147483648$s, %2$s %1$s', "a", "b");
} catch (ValueError $e) {
echo $e->getMessage(), "\n";
}

?>
--EXPECTF--
Argument number specifier must be greater than zero and less than %d
4 arguments are required, 3 given
Argument number specifier must be greater than zero and less than %d
2 changes: 1 addition & 1 deletion ext/standard/tests/strings/vfprintf_error4.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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 must be greater than zero.
Error found: Argument number specifier must be greater than zero and less than 2147483647.
10 changes: 5 additions & 5 deletions tests/strings/002.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ printf("printf test 7:%010.2f\n", 2.5);
printf("printf test 8:<%20s>\n", "foo");
printf("printf test 9:<%-20s>\n", "bar");
printf("printf test 10: 123456789012345\n");
printf("printf test 10:<%15s>\n", "h�yesterettsjustitiarius");
printf("printf test 10:<%15s>\n", "hoyesterettsjustitiarius");
printf("printf test 11: 123456789012345678901234567890\n");
printf("printf test 11:<%30s>\n", "h�yesterettsjustitiarius");
printf("printf test 11:<%30s>\n", "hoyesterettsjustitiarius");
printf("printf test 12:%5.2f\n", -12.34);
printf("printf test 13:%5d\n", -12);
printf("printf test 14:%c\n", 64);
Expand Down Expand Up @@ -61,9 +61,9 @@ printf test 7:0000002.50
printf test 8:< foo>
printf test 9:<bar >
printf test 10: 123456789012345
printf test 10:<h�yesterettsjustitiarius>
printf test 10:<hoyesterettsjustitiarius>
printf test 11: 123456789012345678901234567890
printf test 11:< h�yesterettsjustitiarius>
printf test 11:< hoyesterettsjustitiarius>
printf test 12:-12.34
printf test 13: -12
printf test 14:@
Expand All @@ -82,5 +82,5 @@ printf test 26:2 1
printf test 27:3 1 2
printf test 28:02 1
printf test 29:2 1
printf test 30:Error found: Argument number must be greater than zero
printf test 30:Error found: Argument number specifier must be greater than zero and less than 2147483647
vprintf test 1:2 1