Skip to content

Commit 26327bc

Browse files
committed
Throw "Unsupported operand types" error when using ** on arrays
1 parent 8f20b99 commit 26327bc

File tree

6 files changed

+62
-46
lines changed

6 files changed

+62
-46
lines changed

Zend/tests/bug74084.phpt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,14 @@ try {
2424
echo $e->getMessage(), "\n";
2525
}
2626
unset($$A);
27-
$$A **= $$B['a'] = &$$C;
28-
var_dump($$A);
27+
try {
28+
$$A **= $$B['a'] = &$$C;
29+
} catch (Error $e) {
30+
echo $e->getMessage(), "\n";
31+
}
2932
?>
3033
--EXPECT--
3134
Unsupported operand types
3235
Unsupported operand types
3336
Unsupported operand types
34-
int(0)
37+
Unsupported operand types

Zend/tests/pow_array_leak.phpt

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,30 @@ Memory leak on ** with result==op1 array
44
<?php
55

66
$x = [0];
7-
$x **= 1;
7+
try {
8+
$x **= 1;
9+
} catch (Error $e) {
10+
echo $e->getMessage(), "\n";
11+
}
812
var_dump($x);
913

1014
$x = [0];
11-
$x **= $x;
15+
try {
16+
$x **= $x;
17+
} catch (Error $e) {
18+
echo $e->getMessage(), "\n";
19+
}
1220
var_dump($x);
1321

1422
?>
1523
--EXPECT--
16-
int(0)
17-
int(0)
24+
Unsupported operand types
25+
array(1) {
26+
[0]=>
27+
int(0)
28+
}
29+
Unsupported operand types
30+
array(1) {
31+
[0]=>
32+
int(0)
33+
}

Zend/zend_operators.c

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,37 +1197,22 @@ ZEND_API int ZEND_FASTCALL pow_function(zval *result, zval *op1, zval *op2) /* {
11971197
} else if (!converted) {
11981198
ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_POW, pow_function);
11991199

1200-
if (EXPECTED(op1 != op2)) {
1201-
if (Z_TYPE_P(op1) == IS_ARRAY) {
1202-
if (op1 == result) {
1203-
zval_ptr_dtor(result);
1204-
}
1205-
ZVAL_LONG(result, 0);
1206-
return SUCCESS;
1207-
} else {
1208-
op1 = zendi_convert_scalar_to_number(op1, &op1_copy, result, 0);
1209-
}
1210-
if (Z_TYPE_P(op2) == IS_ARRAY) {
1211-
if (op1 == result) {
1212-
zval_ptr_dtor(result);
1213-
}
1214-
ZVAL_LONG(result, 1L);
1215-
return SUCCESS;
1216-
} else {
1217-
op2 = zendi_convert_scalar_to_number(op2, &op2_copy, result, 0);
1200+
if (Z_TYPE_P(op1) == IS_ARRAY || Z_TYPE_P(op2) == IS_ARRAY) {
1201+
if (result != op1) {
1202+
ZVAL_UNDEF(result);
12181203
}
1204+
zend_throw_error(NULL, "Unsupported operand types");
1205+
return FAILURE;
1206+
}
1207+
1208+
if (EXPECTED(op1 != op2)) {
1209+
op1 = zendi_convert_scalar_to_number(op1, &op1_copy, result, 0);
1210+
op2 = zendi_convert_scalar_to_number(op2, &op2_copy, result, 0);
12191211
} else {
1220-
if (Z_TYPE_P(op1) == IS_ARRAY) {
1221-
if (op1 == result) {
1222-
zval_ptr_dtor(result);
1223-
}
1224-
ZVAL_LONG(result, 0);
1225-
return SUCCESS;
1226-
} else {
1227-
op1 = zendi_convert_scalar_to_number(op1, &op1_copy, result, 0);
1228-
}
1212+
op1 = zendi_convert_scalar_to_number(op1, &op1_copy, result, 0);
12291213
op2 = op1;
12301214
}
1215+
12311216
if (EG(exception)) {
12321217
if (result != op1) {
12331218
ZVAL_UNDEF(result);

ext/standard/tests/math/pow_variation1.phpt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,13 @@ $inputs = array(
8484
// loop through each element of $inputs to check the behaviour of pow()
8585
$iterator = 1;
8686
foreach($inputs as $input) {
87-
echo "\n-- Iteration $iterator --\n";
88-
var_dump(pow($input, 3));
89-
$iterator++;
87+
echo "\n-- Iteration $iterator --\n";
88+
try {
89+
var_dump(pow($input, 3));
90+
} catch (Error $e) {
91+
echo $e->getMessage(), "\n";
92+
}
93+
$iterator++;
9094
};
9195
fclose($fp);
9296
?>
@@ -153,7 +157,7 @@ Warning: A non-numeric value encountered in %s on line %d
153157
int(0)
154158

155159
-- Iteration 19 --
156-
int(0)
160+
Unsupported operand types
157161

158162
-- Iteration 20 --
159163

ext/standard/tests/math/pow_variation1_64bit.phpt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,13 @@ $inputs = array(
8484
// loop through each element of $inputs to check the behaviour of pow()
8585
$iterator = 1;
8686
foreach($inputs as $input) {
87-
echo "\n-- Iteration $iterator --\n";
88-
var_dump(pow($input, 3));
89-
$iterator++;
87+
echo "\n-- Iteration $iterator --\n";
88+
try {
89+
var_dump(pow($input, 3));
90+
} catch (Error $e) {
91+
echo $e->getMessage(), "\n";
92+
}
93+
$iterator++;
9094
};
9195
fclose($fp);
9296
?>
@@ -153,7 +157,7 @@ Warning: A non-numeric value encountered in %s on line %d
153157
int(0)
154158

155159
-- Iteration 19 --
156-
int(0)
160+
Unsupported operand types
157161

158162
-- Iteration 20 --
159163

ext/standard/tests/math/pow_variation2.phpt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,13 @@ $inputs = array(
8080
// loop through each element of $inputs to check the behaviour of pow()
8181
$iterator = 1;
8282
foreach($inputs as $input) {
83-
echo "\n-- Iteration $iterator --\n";
84-
var_dump(pow(20.3, $input));
85-
$iterator++;
83+
echo "\n-- Iteration $iterator --\n";
84+
try {
85+
var_dump(pow(20.3, $input));
86+
} catch (Error $e) {
87+
echo $e->getMessage(), "\n";
88+
}
89+
$iterator++;
8690
};
8791
fclose($fp);
8892
?>
@@ -149,7 +153,7 @@ Warning: A non-numeric value encountered in %s on line %d
149153
float(1)
150154

151155
-- Iteration 19 --
152-
int(1)
156+
Unsupported operand types
153157

154158
-- Iteration 20 --
155159

0 commit comments

Comments
 (0)