Skip to content

Commit 43095a5

Browse files
committed
Promote warning related to operator overloading in ext/gmp to an exception
1 parent d609882 commit 43095a5

File tree

6 files changed

+82
-25
lines changed

6 files changed

+82
-25
lines changed

ext/gmp/gmp.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -332,12 +332,16 @@ static zend_object *gmp_clone_obj(zend_object *obj) /* {{{ */
332332
}
333333
/* }}} */
334334

335-
static void shift_operator_helper(gmp_binary_ui_op_t op, zval *return_value, zval *op1, zval *op2) {
335+
static void shift_operator_helper(gmp_binary_ui_op_t op, zval *return_value, zval *op1, zval *op2, zend_uchar opcode) {
336336
zend_long shift = zval_get_long(op2);
337337

338338
if (shift < 0) {
339-
php_error_docref(NULL, E_WARNING, "Shift cannot be negative");
340-
RETVAL_FALSE;
339+
zend_throw_error(
340+
zend_ce_value_error, "%s must be greater than or equal to 0",
341+
opcode == ZEND_POW ? "Exponent" : "Shift"
342+
);
343+
ZVAL_UNDEF(return_value);
344+
return;
341345
} else {
342346
mpz_ptr gmpnum_op, gmpnum_result;
343347
gmp_temp_t temp;
@@ -372,17 +376,17 @@ static int gmp_do_operation_ex(zend_uchar opcode, zval *result, zval *op1, zval
372376
case ZEND_MUL:
373377
DO_BINARY_UI_OP(mpz_mul);
374378
case ZEND_POW:
375-
shift_operator_helper(mpz_pow_ui, result, op1, op2);
379+
shift_operator_helper(mpz_pow_ui, result, op1, op2, opcode);
376380
return SUCCESS;
377381
case ZEND_DIV:
378382
DO_BINARY_UI_OP_EX(mpz_tdiv_q, gmp_mpz_tdiv_q_ui, 1);
379383
case ZEND_MOD:
380384
DO_BINARY_UI_OP_EX(mpz_mod, gmp_mpz_mod_ui, 1);
381385
case ZEND_SL:
382-
shift_operator_helper(mpz_mul_2exp, result, op1, op2);
386+
shift_operator_helper(mpz_mul_2exp, result, op1, op2, opcode);
383387
return SUCCESS;
384388
case ZEND_SR:
385-
shift_operator_helper(mpz_fdiv_q_2exp, result, op1, op2);
389+
shift_operator_helper(mpz_fdiv_q_2exp, result, op1, op2, opcode);
386390
return SUCCESS;
387391
case ZEND_BW_OR:
388392
DO_BINARY_OP(mpz_ior);
@@ -520,7 +524,7 @@ static ZEND_GINIT_FUNCTION(gmp)
520524
ZEND_MINIT_FUNCTION(gmp)
521525
{
522526
zend_class_entry tmp_ce;
523-
INIT_CLASS_ENTRY(tmp_ce, "GMP", NULL);
527+
INIT_CLASS_ENTRY(tmp_ce, "GMP", class_GMP_methods);
524528
gmp_ce = zend_register_internal_class(&tmp_ce);
525529
gmp_ce->create_object = gmp_create_object;
526530
gmp_ce->serialize = gmp_serialize;
@@ -1261,8 +1265,8 @@ ZEND_FUNCTION(gmp_pow)
12611265
}
12621266

12631267
if (exp < 0) {
1264-
php_error_docref(NULL, E_WARNING, "Negative exponent not supported");
1265-
RETURN_FALSE;
1268+
zend_argument_value_error(2, "must be greater than or equal to 0");
1269+
RETURN_THROWS();
12661270
}
12671271

12681272
if (Z_TYPE_P(base_arg) == IS_LONG && Z_LVAL_P(base_arg) >= 0) {

ext/gmp/gmp.stub.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
/** @generate-function-entries */
44

5+
class GMP
6+
{
7+
}
8+
59
/** @param int|bool|string $number */
610
function gmp_init($number, int $base = 0): GMP|false {}
711

ext/gmp/gmp_arginfo.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,8 @@ static const zend_function_entry ext_functions[] = {
289289
ZEND_FE(gmp_binomial, arginfo_gmp_binomial)
290290
ZEND_FE_END
291291
};
292+
293+
294+
static const zend_function_entry class_GMP_methods[] = {
295+
ZEND_FE_END
296+
};

ext/gmp/tests/gmp_pow.phpt

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,19 @@ var_dump(gmp_strval(gmp_pow(-2,10)));
1010
var_dump(gmp_strval(gmp_pow(-2,11)));
1111
var_dump(gmp_strval(gmp_pow("2",10)));
1212
var_dump(gmp_strval(gmp_pow("2",0)));
13-
var_dump(gmp_strval(gmp_pow("2",-1)));
13+
try {
14+
gmp_pow("2", -1);
15+
} catch (ValueError $exception) {
16+
echo $exception->getMessage() . "\n";
17+
}
1418
var_dump(gmp_strval(gmp_pow("-2",10)));
1519
var_dump(gmp_strval(gmp_pow(20,10)));
1620
var_dump(gmp_strval(gmp_pow(50,10)));
17-
var_dump(gmp_strval(gmp_pow(50,-5)));
21+
try {
22+
gmp_pow(50,-5);
23+
} catch (ValueError $exception) {
24+
echo $exception->getMessage() . "\n";
25+
}
1826

1927
$n = gmp_init("20");
2028
var_dump(gmp_strval(gmp_pow($n,10)));
@@ -36,15 +44,11 @@ string(4) "1024"
3644
string(5) "-2048"
3745
string(4) "1024"
3846
string(1) "1"
39-
40-
Warning: gmp_pow(): Negative exponent not supported in %s on line %d
41-
string(1) "0"
47+
gmp_pow(): Argument #2 ($exp) must be greater than or equal to 0
4248
string(4) "1024"
4349
string(14) "10240000000000"
4450
string(17) "97656250000000000"
45-
46-
Warning: gmp_pow(): Negative exponent not supported in %s on line %d
47-
string(1) "0"
51+
gmp_pow(): Argument #2 ($exp) must be greater than or equal to 0
4852
string(14) "10240000000000"
4953
string(14) "10240000000000"
5054
gmp_pow(): Argument #2 ($exp) must be of type int, array given

ext/gmp/tests/gmp_pow2.phpt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--TEST--
2+
Test pow() with gmp object
3+
--SKIPIF--
4+
<?php if (!extension_loaded("gmp")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
8+
$n = gmp_init(2);
9+
var_dump(pow($n, 10));
10+
var_dump($n ** 10);
11+
12+
try {
13+
pow($n, -10);
14+
} catch (ValueError $exception) {
15+
echo $exception->getMessage() . "\n";
16+
}
17+
18+
try {
19+
$n ** -10;
20+
} catch (ValueError $exception) {
21+
echo $exception->getMessage() . "\n";
22+
}
23+
24+
?>
25+
--EXPECTF--
26+
object(GMP)#%d (1) {
27+
["num"]=>
28+
string(4) "1024"
29+
}
30+
object(GMP)#%d (1) {
31+
["num"]=>
32+
string(4) "1024"
33+
}
34+
Exponent must be greater than or equal to 0
35+
Exponent must be greater than or equal to 0

ext/gmp/tests/overloading.phpt

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,17 @@ var_dump(42 << $b);
5353
var_dump($a >> 2);
5454
var_dump(-$a >> 2);
5555

56-
var_dump($a << -1);
57-
var_dump($a >> -1);
56+
try {
57+
$a << -1;
58+
} catch (ValueError $exception) {
59+
echo $exception->getMessage() . "\n";
60+
}
61+
62+
try {
63+
$a >> -1;
64+
} catch (ValueError $exception) {
65+
echo $exception->getMessage() . "\n";
66+
}
5867

5968
var_dump(~$a);
6069
var_dump(-$a);
@@ -237,12 +246,8 @@ object(GMP)#%d (1) {
237246
["num"]=>
238247
string(3) "-11"
239248
}
240-
241-
Warning: main(): Shift cannot be negative in %s on line %d
242-
bool(false)
243-
244-
Warning: main(): Shift cannot be negative in %s on line %d
245-
bool(false)
249+
Shift must be greater than or equal to 0
250+
Shift must be greater than or equal to 0
246251
object(GMP)#%d (1) {
247252
["num"]=>
248253
string(3) "-43"

0 commit comments

Comments
 (0)