Skip to content

Commit 30f5862

Browse files
committed
ext/gmp: Emit deprecation for precision-losing float RHS in ** / << / >>
1 parent 87258eb commit 30f5862

4 files changed

Lines changed: 16 additions & 3 deletions

File tree

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ PHP NEWS
5151
. gmp_fact() reject values larger than unsigned long. (David Carlier)
5252
. gmp_pow/binomial/root/rootrem and shift/pow operators reject values
5353
larger than unsigned long. (David Carlier)
54+
. GMP exponentiation and shift operators now emit a deprecation warning
55+
when converting a float right operand to int loses precision. (Weilin Du)
5456

5557
- Hash:
5658
. Upgrade xxHash to 0.8.2. (timwolla)

UPGRADING

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,11 @@ PHP 8.6 UPGRADE NOTES
174174
4. Deprecated Functionality
175175
========================================
176176

177+
- New warnings and exceptions:
178+
. The shift (<<, >>) and exponentiation (**) operators on GMP objects now
179+
emit a deprecation warning when converting a float right operand to int
180+
loses precision.
181+
177182
- Mbstring:
178183
. Mbregex has been deprecated, because the underlying Oniguruma library
179184
is no longer maintained.

ext/gmp/gmp.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -332,11 +332,11 @@ static zend_result shift_operator_helper(gmp_binary_ui_op_t op, zval *return_val
332332

333333
if (UNEXPECTED(Z_TYPE_P(op2) != IS_LONG)) {
334334
if (UNEXPECTED(!IS_GMP(op2))) {
335-
// For PHP 8.3 and up use zend_try_get_long()
335+
bool failed;
336336
switch (Z_TYPE_P(op2)) {
337337
case IS_DOUBLE:
338-
shift = zval_get_long(op2);
339-
if (UNEXPECTED(EG(exception))) {
338+
shift = zval_try_get_long(op2, &failed);
339+
if (UNEXPECTED(failed)) {
340340
return FAILURE;
341341
}
342342
break;

ext/gmp/tests/overloading_with_float_fractional.phpt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ object(GMP)#2 (1) {
100100
["num"]=>
101101
string(1) "0"
102102
}
103+
104+
Deprecated: Implicit conversion from float 42.5 to int loses precision in %s on line %d
103105
object(GMP)#2 (1) {
104106
["num"]=>
105107
string(69) "150130937545296572356771972164254457814047970568738777235893533016064"
@@ -122,10 +124,14 @@ object(GMP)#2 (1) {
122124
["num"]=>
123125
string(1) "0"
124126
}
127+
128+
Deprecated: Implicit conversion from float 42.5 to int loses precision in %s on line %d
125129
object(GMP)#2 (1) {
126130
["num"]=>
127131
string(15) "184717953466368"
128132
}
133+
134+
Deprecated: Implicit conversion from float 42.5 to int loses precision in %s on line %d
129135
object(GMP)#2 (1) {
130136
["num"]=>
131137
string(1) "0"

0 commit comments

Comments
 (0)