Skip to content

Commit 799ec7b

Browse files
committed
Fix misleading errors in printf()
The precision and width _can_ be zero. Closes GH-18911.
1 parent b508988 commit 799ec7b

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ PHP NEWS
2020
- MbString:
2121
. Fixed bug GH-18901 (integer overflow mb_split). (nielsdos)
2222

23+
- Standard:
24+
. Fix misleading errors in printf(). (nielsdos)
25+
2326
- Streams:
2427
. Fixed GH-13264 (fgets() and stream_get_line() do not return false on filter
2528
fatal error). (Jakub Zelenka)

ext/standard/formatted_print.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -534,15 +534,15 @@ php_formatted_print(char *format, size_t format_len, zval *args, int argc, int n
534534
goto fail;
535535
}
536536
if (Z_LVAL_P(tmp) < 0 || Z_LVAL_P(tmp) > INT_MAX) {
537-
zend_value_error("Width must be greater than zero and less than %d", INT_MAX);
537+
zend_value_error("Width must be between 0 and %d", INT_MAX);
538538
goto fail;
539539
}
540540
width = Z_LVAL_P(tmp);
541541
adjusting |= ADJ_WIDTH;
542542
} else if (isdigit((int)*format)) {
543543
PRINTF_DEBUG(("sprintf: getting width\n"));
544544
if ((width = php_sprintf_getnumber(&format, &format_len)) < 0) {
545-
zend_value_error("Width must be greater than zero and less than %d", INT_MAX);
545+
zend_value_error("Width must be between 0 and %d", INT_MAX);
546546
goto fail;
547547
}
548548
adjusting |= ADJ_WIDTH;
@@ -586,7 +586,7 @@ php_formatted_print(char *format, size_t format_len, zval *args, int argc, int n
586586
expprec = 1;
587587
} else if (isdigit((int)*format)) {
588588
if ((precision = php_sprintf_getnumber(&format, &format_len)) < 0) {
589-
zend_value_error("Precision must be greater than zero and less than %d", INT_MAX);
589+
zend_value_error("Precision must be between 0 and %d", INT_MAX);
590590
goto fail;
591591
}
592592
adjusting |= ADJ_PRECISION;

ext/standard/tests/strings/sprintf_star.phpt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,18 @@ try {
6262
echo $e->getMessage(), "\n";
6363
}
6464

65+
try {
66+
printf("%9999999999999999999999.f\n", $f);
67+
} catch (ValueError $e) {
68+
echo $e->getMessage(), "\n";
69+
}
70+
71+
try {
72+
printf("%.9999999999999999999999f\n", $f);
73+
} catch (ValueError $e) {
74+
echo $e->getMessage(), "\n";
75+
}
76+
6577
?>
6678
--EXPECT--
6779
float(1.2345678901234567)
@@ -95,4 +107,6 @@ foo
95107
Precision must be an integer
96108
Precision must be between -1 and 2147483647
97109
Precision -1 is only supported for %g, %G, %h and %H
98-
Width must be greater than zero and less than 2147483647
110+
Width must be between 0 and 2147483647
111+
Width must be between 0 and 2147483647
112+
Precision must be between 0 and 2147483647

0 commit comments

Comments
 (0)