Skip to content

Commit d061a97

Browse files
committed
Fix misleading errors in printf()
The precision and with _can_ be zero.
1 parent a5f21ca commit d061a97

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

ext/standard/formatted_print.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -530,15 +530,15 @@ php_formatted_print(char *format, size_t format_len, zval *args, int argc, int n
530530
goto fail;
531531
}
532532
if (Z_LVAL_P(tmp) < 0 || Z_LVAL_P(tmp) > INT_MAX) {
533-
zend_value_error("Width must be greater than zero and less than %d", INT_MAX);
533+
zend_value_error("Width must be between 0 and %d", INT_MAX);
534534
goto fail;
535535
}
536536
width = Z_LVAL_P(tmp);
537537
adjusting |= ADJ_WIDTH;
538538
} else if (isdigit((int)*format)) {
539539
PRINTF_DEBUG(("sprintf: getting width\n"));
540540
if ((width = php_sprintf_getnumber(&format, &format_len)) < 0) {
541-
zend_value_error("Width must be greater than zero and less than %d", INT_MAX);
541+
zend_value_error("Width must be between 0 and %d", INT_MAX);
542542
goto fail;
543543
}
544544
adjusting |= ADJ_WIDTH;
@@ -582,7 +582,7 @@ php_formatted_print(char *format, size_t format_len, zval *args, int argc, int n
582582
expprec = 1;
583583
} else if (isdigit((int)*format)) {
584584
if ((precision = php_sprintf_getnumber(&format, &format_len)) < 0) {
585-
zend_value_error("Precision must be greater than zero and less than %d", INT_MAX);
585+
zend_value_error("Precision must be between 0 and %d", INT_MAX);
586586
goto fail;
587587
}
588588
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)