Skip to content

Commit 24b9ef2

Browse files
committed
Fixed the behavior when construct/unserialize is executed multiple times.
1 parent a7c6402 commit 24b9ef2

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

ext/bcmath/bcmath.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,14 +1355,19 @@ PHP_METHOD(BcMath_Number, __construct)
13551355
Z_PARAM_STR_OR_LONG(str, lval);
13561356
ZEND_PARSE_PARAMETERS_END();
13571357

1358+
bcmath_number_obj_t *intern = get_bcmath_number_from_zval(ZEND_THIS);
1359+
if (UNEXPECTED(intern->num != NULL)) {
1360+
zend_readonly_property_modification_error_ex(ZSTR_VAL(bcmath_number_ce->name), "value");
1361+
RETURN_THROWS();
1362+
}
1363+
13581364
bc_num num = NULL;
13591365
size_t scale;
13601366
if (bc_num_from_obj_or_str_or_long_with_err(&num, &scale, NULL, str, lval, 1) == FAILURE) {
13611367
bc_free_num(&num);
13621368
RETURN_THROWS();
13631369
}
13641370

1365-
bcmath_number_obj_t *intern = get_bcmath_number_from_zval(ZEND_THIS);
13661371
intern->num = num;
13671372
intern->scale = scale;
13681373
}
@@ -1737,15 +1742,19 @@ PHP_METHOD(BcMath_Number, __unserialize)
17371742
goto fail;
17381743
}
17391744

1745+
bcmath_number_obj_t *intern = get_bcmath_number_from_zval(ZEND_THIS);
1746+
if (UNEXPECTED(intern->num != NULL)) {
1747+
zend_readonly_property_modification_error_ex(ZSTR_VAL(bcmath_number_ce->name), "value");
1748+
RETURN_THROWS();
1749+
}
1750+
17401751
bc_num num = NULL;
17411752
size_t scale;
17421753
if (php_str2num_ex(&num, Z_STR_P(zv), &scale) == FAILURE) {
17431754
bc_free_num(&num);
17441755
goto fail;
17451756
}
17461757

1747-
bcmath_number_obj_t *intern = get_bcmath_number_from_zval(ZEND_THIS);
1748-
17491758
intern->num = num;
17501759
intern->scale = scale;
17511760

ext/bcmath/tests/number/construct_error.phpt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ BcMath\Number construct error
44
bcmath
55
--FILE--
66
<?php
7+
try {
8+
$num = new BcMath\Number('1');
9+
$num->__construct(5);
10+
} catch (Error $e) {
11+
echo $e->getMessage() . "\n";
12+
}
13+
714
try {
815
$num = new BcMath\Number('a');
916
var_dump($num);
@@ -12,4 +19,5 @@ try {
1219
}
1320
?>
1421
--EXPECT--
22+
Cannot modify readonly property BcMath\Number::$value
1523
BcMath\Number::__construct(): Argument #1 ($num) is not well-formed

ext/bcmath/tests/number/unserialize_error.phpt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,19 @@ BcMath\Number unserialize error
44
bcmath
55
--FILE--
66
<?php
7+
try {
8+
$num = new BcMath\Number(1);
9+
$num->__unserialize(['value' => '5']);
10+
} catch (Error $e) {
11+
echo $e->getMessage() . "\n";
12+
}
13+
714
try {
815
unserialize('O:13:"BcMath\Number":1:{s:5:"value";s:1:"a";}');
916
} catch (Exception $e) {
1017
echo $e->getMessage();
1118
}
1219
?>
1320
--EXPECT--
21+
Cannot modify readonly property BcMath\Number::$value
1422
Invalid serialization data for BcMath\Number object

0 commit comments

Comments
 (0)