Skip to content

Commit a3bcc86

Browse files
authored
Fix GH-22395: Avoid truncating base_convert() output at 64 characters (#22406)
By using `ZEND_DOUBLE_MAX_LENGTH` instead of `(sizeof(double) << 3) + 1` we can fix the bug that base_convert() truncates output at 64 characters. Fixes #22395
1 parent 087a08d commit a3bcc86

3 files changed

Lines changed: 15 additions & 1 deletion

File tree

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ PHP NEWS
1515
- Standard:
1616
. Fixed bug GH-22360 (convert.base64-encode corruption on
1717
incremental flush). (David Carlier)
18+
. Fixed bug GH-22395 (base_convert() outputs at most 64 characters).
19+
(Weilin Du)
1820

1921
02 Jul 2026, PHP 8.4.23
2022

ext/standard/math.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "zend_exceptions.h"
2525
#include "zend_multiply.h"
2626
#include "zend_portability.h"
27+
#include "zend_strtod.h"
2728

2829
#include <float.h>
2930
#include <math.h>
@@ -949,7 +950,7 @@ PHPAPI zend_string * _php_math_zvaltobase(zval *arg, int base)
949950
if (Z_TYPE_P(arg) == IS_DOUBLE) {
950951
double fvalue = floor(Z_DVAL_P(arg)); /* floor it just in case */
951952
char *ptr, *end;
952-
char buf[(sizeof(double) << 3) + 1];
953+
char buf[ZEND_DOUBLE_MAX_LENGTH];
953954

954955
/* Don't try to convert +/- infinity */
955956
if (fvalue == ZEND_INFINITY || fvalue == -ZEND_INFINITY) {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--TEST--
2+
GH-22395 (base_convert outputs at most 64 characters)
3+
--FILE--
4+
<?php
5+
$result = base_convert(str_repeat("1", 61), 36, 16);
6+
var_dump(strlen($result));
7+
var_dump(substr($result, 0, 13));
8+
?>
9+
--EXPECT--
10+
int(78)
11+
string(13) "4b61b5e0639ff"

0 commit comments

Comments
 (0)