Skip to content

Fix misleading errors in printf() #18911

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 1 commit 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
6 changes: 3 additions & 3 deletions ext/standard/formatted_print.c
Original file line number Diff line number Diff line change
Expand Up @@ -530,15 +530,15 @@ php_formatted_print(char *format, size_t format_len, zval *args, int argc, int n
goto fail;
}
if (Z_LVAL_P(tmp) < 0 || Z_LVAL_P(tmp) > INT_MAX) {
zend_value_error("Width must be greater than zero and less than %d", INT_MAX);
zend_value_error("Width must be between 0 and %d", INT_MAX);
goto fail;
}
width = Z_LVAL_P(tmp);
adjusting |= ADJ_WIDTH;
} else if (isdigit((int)*format)) {
PRINTF_DEBUG(("sprintf: getting width\n"));
if ((width = php_sprintf_getnumber(&format, &format_len)) < 0) {
zend_value_error("Width must be greater than zero and less than %d", INT_MAX);
zend_value_error("Width must be between 0 and %d", INT_MAX);
goto fail;
}
adjusting |= ADJ_WIDTH;
Expand Down Expand Up @@ -582,7 +582,7 @@ php_formatted_print(char *format, size_t format_len, zval *args, int argc, int n
expprec = 1;
} else if (isdigit((int)*format)) {
if ((precision = php_sprintf_getnumber(&format, &format_len)) < 0) {
zend_value_error("Precision must be greater than zero and less than %d", INT_MAX);
zend_value_error("Precision must be between 0 and %d", INT_MAX);
goto fail;
}
adjusting |= ADJ_PRECISION;
Expand Down
16 changes: 15 additions & 1 deletion ext/standard/tests/strings/sprintf_star.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ try {
echo $e->getMessage(), "\n";
}

try {
printf("%9999999999999999999999.f\n", $f);
} catch (ValueError $e) {
echo $e->getMessage(), "\n";
}

try {
printf("%.9999999999999999999999f\n", $f);
} catch (ValueError $e) {
echo $e->getMessage(), "\n";
}

?>
--EXPECT--
float(1.2345678901234567)
Expand Down Expand Up @@ -95,4 +107,6 @@ foo
Precision must be an integer
Precision must be between -1 and 2147483647
Precision -1 is only supported for %g, %G, %h and %H
Width must be greater than zero and less than 2147483647
Width must be between 0 and 2147483647
Width must be between 0 and 2147483647
Precision must be between 0 and 2147483647
Loading