1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 00:02:20 +01:00

Fix misleading errors in printf()

The precision and width _can_ be zero.

Closes GH-18911.
This commit is contained in:
Niels Dossche
2025-06-22 16:29:19 +02:00
parent b50898894d
commit 799ec7b8c5
3 changed files with 21 additions and 4 deletions

3
NEWS
View File

@@ -20,6 +20,9 @@ PHP NEWS
- MbString:
. Fixed bug GH-18901 (integer overflow mb_split). (nielsdos)
- Standard:
. Fix misleading errors in printf(). (nielsdos)
- Streams:
. Fixed GH-13264 (fgets() and stream_get_line() do not return false on filter
fatal error). (Jakub Zelenka)

View File

@@ -534,7 +534,7 @@ 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);
@@ -542,7 +542,7 @@ php_formatted_print(char *format, size_t format_len, zval *args, int argc, int n
} 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;
@@ -586,7 +586,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;

View File

@@ -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)
@@ -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