Skip to content

Commit eb78a0b

Browse files
committed
Merge branch 'PHP-8.3' into PHP-8.4
* PHP-8.3: Fix misleading errors in printf() Unbreak PRINTF_DEBUG macro usages
2 parents fe504d3 + 799ec7b commit eb78a0b

File tree

3 files changed

+46
-25
lines changed

3 files changed

+46
-25
lines changed

NEWS

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

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

ext/standard/formatted_print.c

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ inline static void
4949
php_sprintf_appendchar(zend_string **buffer, size_t *pos, char add)
5050
{
5151
if ((*pos + 1) >= ZSTR_LEN(*buffer)) {
52-
PRINTF_DEBUG(("%s(): ereallocing buffer to %d bytes\n", get_active_function_name(), ZSTR_LEN(*buffer)));
52+
PRINTF_DEBUG(("%s(): ereallocing buffer to %zu bytes\n", get_active_function_name(), ZSTR_LEN(*buffer)));
5353
*buffer = zend_string_extend(*buffer, ZSTR_LEN(*buffer) << 1, 0);
5454
}
55-
PRINTF_DEBUG(("sprintf: appending '%c', pos=\n", add, *pos));
55+
PRINTF_DEBUG(("sprintf: appending '%c', pos=%zu\n", add, *pos));
5656
ZSTR_VAL(*buffer)[(*pos)++] = add;
5757
}
5858
/* }}} */
@@ -64,13 +64,13 @@ php_sprintf_appendchars(zend_string **buffer, size_t *pos, char *add, size_t len
6464
if ((*pos + len) >= ZSTR_LEN(*buffer)) {
6565
size_t nlen = ZSTR_LEN(*buffer);
6666

67-
PRINTF_DEBUG(("%s(): ereallocing buffer to %d bytes\n", get_active_function_name(), ZSTR_LEN(*buffer)));
67+
PRINTF_DEBUG(("%s(): ereallocing buffer to %zu bytes\n", get_active_function_name(), ZSTR_LEN(*buffer)));
6868
do {
6969
nlen = nlen << 1;
7070
} while ((*pos + len) >= nlen);
7171
*buffer = zend_string_extend(*buffer, nlen, 0);
7272
}
73-
PRINTF_DEBUG(("sprintf: appending \"%s\", pos=\n", add, *pos));
73+
PRINTF_DEBUG(("sprintf: appending \"%s\", pos=%zu\n", add, *pos));
7474
memcpy(ZSTR_VAL(*buffer) + (*pos), add, len);
7575
*pos += len;
7676
}
@@ -90,7 +90,7 @@ php_sprintf_appendstring(zend_string **buffer, size_t *pos, char *add,
9090
copy_len = (expprec ? MIN(max_width, len) : len);
9191
npad = (min_width < copy_len) ? 0 : min_width - copy_len;
9292

93-
PRINTF_DEBUG(("sprintf: appendstring(%x, %d, %d, \"%s\", %d, '%c', %d)\n",
93+
PRINTF_DEBUG(("sprintf: appendstring(%p, %zu, %zu, \"%s\", %zu, '%c', %zu)\n",
9494
*buffer, *pos, ZSTR_LEN(*buffer), add, min_width, padding, alignment));
9595
m_width = MAX(min_width, copy_len);
9696

@@ -108,7 +108,7 @@ php_sprintf_appendstring(zend_string **buffer, size_t *pos, char *add,
108108
}
109109
size <<= 1;
110110
}
111-
PRINTF_DEBUG(("sprintf ereallocing buffer to %d bytes\n", size));
111+
PRINTF_DEBUG(("sprintf ereallocing buffer to %zu bytes\n", size));
112112
*buffer = zend_string_extend(*buffer, size, 0);
113113
}
114114
if (alignment == ALIGN_RIGHT) {
@@ -143,8 +143,8 @@ php_sprintf_appendint(zend_string **buffer, size_t *pos, zend_long number,
143143
zend_ulong magn, nmagn;
144144
unsigned int i = NUM_BUF_SIZE - 1, neg = 0;
145145

146-
PRINTF_DEBUG(("sprintf: appendint(%x, %x, %x, %d, %d, '%c', %d)\n",
147-
*buffer, pos, &ZSTR_LEN(*buffer), number, width, padding, alignment));
146+
PRINTF_DEBUG(("sprintf: appendint(%p, %zu, %zu, " ZEND_LONG_FMT ", %zu, '%c', %zu)\n",
147+
*buffer, *pos, ZSTR_LEN(*buffer), number, width, padding, alignment));
148148
if (number < 0) {
149149
neg = 1;
150150
magn = ((zend_ulong) -(number + 1)) + 1;
@@ -169,7 +169,7 @@ php_sprintf_appendint(zend_string **buffer, size_t *pos, zend_long number,
169169
} else if (always_sign) {
170170
numbuf[--i] = '+';
171171
}
172-
PRINTF_DEBUG(("sprintf: appending %d as \"%s\", i=%d\n",
172+
PRINTF_DEBUG(("sprintf: appending " ZEND_LONG_FMT " as \"%s\", i=%u\n",
173173
number, &numbuf[i], i));
174174
php_sprintf_appendstring(buffer, pos, &numbuf[i], width, 0,
175175
padding, alignment, (NUM_BUF_SIZE - 1) - i,
@@ -187,8 +187,8 @@ php_sprintf_appenduint(zend_string **buffer, size_t *pos,
187187
zend_ulong magn, nmagn;
188188
unsigned int i = NUM_BUF_SIZE - 1;
189189

190-
PRINTF_DEBUG(("sprintf: appenduint(%x, %x, %x, %d, %d, '%c', %d)\n",
191-
*buffer, pos, &ZSTR_LEN(*buffer), number, width, padding, alignment));
190+
PRINTF_DEBUG(("sprintf: appenduint(%p, %zu, %zu, " ZEND_LONG_FMT ", %zu, '%c', %zu)\n",
191+
*buffer, *pos, ZSTR_LEN(*buffer), number, width, padding, alignment));
192192
magn = (zend_ulong) number;
193193

194194
/* Can't right-pad 0's on integers */
@@ -203,7 +203,7 @@ php_sprintf_appenduint(zend_string **buffer, size_t *pos,
203203
magn = nmagn;
204204
} while (magn > 0 && i > 0);
205205

206-
PRINTF_DEBUG(("sprintf: appending %d as \"%s\", i=%d\n", number, &numbuf[i], i));
206+
PRINTF_DEBUG(("sprintf: appending " ZEND_LONG_FMT " as \"%s\", i=%d\n", number, &numbuf[i], i));
207207
php_sprintf_appendstring(buffer, pos, &numbuf[i], width, 0,
208208
padding, alignment, (NUM_BUF_SIZE - 1) - i, /* neg */ false, 0, 0);
209209
}
@@ -229,8 +229,8 @@ php_sprintf_appenddouble(zend_string **buffer, size_t *pos,
229229
struct lconv *lconv;
230230
#endif
231231

232-
PRINTF_DEBUG(("sprintf: appenddouble(%x, %x, %x, %f, %d, '%c', %d, %c)\n",
233-
*buffer, pos, &ZSTR_LEN(*buffer), number, width, padding, alignment, fmt));
232+
PRINTF_DEBUG(("sprintf: appenddouble(%p, %zu, %zu, %f, %zu, '%c', %zu, %c)\n",
233+
*buffer, *pos, ZSTR_LEN(*buffer), number, width, padding, alignment, fmt));
234234
if ((adjust & ADJ_PRECISION) == 0) {
235235
precision = FLOAT_PRECISION;
236236
} else if (precision > MAX_FLOAT_PRECISION) {
@@ -328,8 +328,8 @@ php_sprintf_append2n(zend_string **buffer, size_t *pos, zend_long number,
328328
zend_ulong i = NUM_BUF_SIZE - 1;
329329
int andbits = (1 << n) - 1;
330330

331-
PRINTF_DEBUG(("sprintf: append2n(%x, %x, %x, %d, %d, '%c', %d, %d, %x)\n",
332-
*buffer, pos, &ZSTR_LEN(*buffer), number, width, padding, alignment, n,
331+
PRINTF_DEBUG(("sprintf: append2n(%p, %zu, %zu, " ZEND_LONG_FMT ", %zu, '%c', %zu, %d, %p)\n",
332+
*buffer, *pos, ZSTR_LEN(*buffer), number, width, padding, alignment, n,
333333
chartable));
334334
PRINTF_DEBUG(("sprintf: append2n 2^%d andbits=%x\n", n, andbits));
335335

@@ -361,7 +361,7 @@ php_sprintf_getnumber(char **buffer, size_t *len)
361361
*len -= i;
362362
*buffer = endptr;
363363
}
364-
PRINTF_DEBUG(("sprintf_getnumber: number was %d bytes long\n", i));
364+
PRINTF_DEBUG(("sprintf_getnumber: number was %zu bytes long\n", i));
365365

366366
if (num >= INT_MAX || num < 0) {
367367
return -1;
@@ -429,6 +429,10 @@ php_formatted_print(char *format, size_t format_len, zval *args, int argc, int n
429429
int always_sign;
430430
int max_missing_argnum = -1;
431431

432+
/* For debugging */
433+
const char *format_orig = format;
434+
ZEND_IGNORE_VALUE(format_orig);
435+
432436
result = zend_string_alloc(size, 0);
433437

434438
currarg = 0;
@@ -462,8 +466,8 @@ php_formatted_print(char *format, size_t format_len, zval *args, int argc, int n
462466
always_sign = 0;
463467
expprec = 0;
464468

465-
PRINTF_DEBUG(("sprintf: first looking at '%c', inpos=%d\n",
466-
*format, format - Z_STRVAL_P(z_format)));
469+
PRINTF_DEBUG(("sprintf: first looking at '%c', inpos=%zu\n",
470+
*format, format - format_orig));
467471
if (isalpha((int)*format)) {
468472
width = precision = 0;
469473
argnum = ARG_NUM_NEXT;
@@ -476,8 +480,8 @@ php_formatted_print(char *format, size_t format_len, zval *args, int argc, int n
476480

477481
/* after argnum comes modifiers */
478482
PRINTF_DEBUG(("sprintf: looking for modifiers\n"
479-
"sprintf: now looking at '%c', inpos=%d\n",
480-
*format, format - Z_STRVAL_P(z_format)));
483+
"sprintf: now looking at '%c', inpos=%zu\n",
484+
*format, format - format_orig));
481485
for (;; format++, format_len--) {
482486
if (*format == ' ' || *format == '0') {
483487
padding = *format;
@@ -528,15 +532,15 @@ php_formatted_print(char *format, size_t format_len, zval *args, int argc, int n
528532
goto fail;
529533
}
530534
if (Z_LVAL_P(tmp) < 0 || Z_LVAL_P(tmp) > INT_MAX) {
531-
zend_value_error("Width must be greater than zero and less than %d", INT_MAX);
535+
zend_value_error("Width must be between 0 and %d", INT_MAX);
532536
goto fail;
533537
}
534538
width = Z_LVAL_P(tmp);
535539
adjusting |= ADJ_WIDTH;
536540
} else if (isdigit((int)*format)) {
537541
PRINTF_DEBUG(("sprintf: getting width\n"));
538542
if ((width = php_sprintf_getnumber(&format, &format_len)) < 0) {
539-
zend_value_error("Width must be greater than zero and less than %d", INT_MAX);
543+
zend_value_error("Width must be between 0 and %d", INT_MAX);
540544
goto fail;
541545
}
542546
adjusting |= ADJ_WIDTH;
@@ -580,7 +584,7 @@ php_formatted_print(char *format, size_t format_len, zval *args, int argc, int n
580584
expprec = 1;
581585
} else if (isdigit((int)*format)) {
582586
if ((precision = php_sprintf_getnumber(&format, &format_len)) < 0) {
583-
zend_value_error("Precision must be greater than zero and less than %d", INT_MAX);
587+
zend_value_error("Precision must be between 0 and %d", INT_MAX);
584588
goto fail;
585589
}
586590
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)