Skip to content

Commit 5a4832f

Browse files
committed
Fixed phpGH-17398: bcmul memory leak (php#17615)
Changed BCG memory allocation to be forcibly released in PHP_GSHUTDOWN_FUNCTION regardless of refcount. Fixes php#17398 Closes php#17615
1 parent ed1d51f commit 5a4832f

File tree

5 files changed

+24
-3
lines changed

5 files changed

+24
-3
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? ????, PHP 8.3.18
44

5+
- BCMath:
6+
. Fixed bug GH-17398 (bcmul memory leak). (SakiTakamachi)
57

68
13 Feb 2025, PHP 8.3.17
79

ext/bcmath/bcmath.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ static PHP_GINIT_FUNCTION(bcmath)
9696
/* {{{ PHP_GSHUTDOWN_FUNCTION */
9797
static PHP_GSHUTDOWN_FUNCTION(bcmath)
9898
{
99-
_bc_free_num_ex(&bcmath_globals->_zero_, 1);
100-
_bc_free_num_ex(&bcmath_globals->_one_, 1);
101-
_bc_free_num_ex(&bcmath_globals->_two_, 1);
99+
bc_force_free_number(&bcmath_globals->_zero_);
100+
bc_force_free_number(&bcmath_globals->_one_);
101+
bc_force_free_number(&bcmath_globals->_two_);
102102
}
103103
/* }}} */
104104

ext/bcmath/libbcmath/src/bcmath.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ typedef struct bc_struct {
8787

8888
void bc_init_numbers(void);
8989

90+
void bc_force_free_number(bc_num *num);
91+
9092
bc_num _bc_new_num_ex(size_t length, size_t scale, bool persistent);
9193

9294
void _bc_free_num_ex(bc_num *num, bool persistent);

ext/bcmath/libbcmath/src/init.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ void bc_init_numbers(void)
8282
BCG(_two_)->n_value[0] = 2;
8383
}
8484

85+
void bc_force_free_number(bc_num *num)
86+
{
87+
pefree((*num)->n_ptr, 1);
88+
pefree(*num, 1);
89+
*num = NULL;
90+
}
91+
8592

8693
/* Make a copy of a number! Just increments the reference count! */
8794
bc_num bc_copy_num(bc_num num)

ext/bcmath/tests/gh17398.phpt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
GH-17398 (bcmul memory leak)
3+
--EXTENSIONS--
4+
bcmath
5+
--FILE--
6+
<?php
7+
bcmul('0', '0', 2147483647);
8+
?>
9+
--EXPECTF--
10+
Fatal error: Allowed memory size of %d bytes exhausted%s(tried to allocate %d bytes) in %s on line %d

0 commit comments

Comments
 (0)