Skip to content

Commit 98c2cea

Browse files
committed
Fix leak for compound shift self-assign error cases
1 parent 8c9415e commit 98c2cea

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
Error cases of compound shift assignment on strings
3+
--FILE--
4+
<?php
5+
6+
$n = "65";
7+
$n <<= $n;
8+
var_dump($n);
9+
10+
$n = "-1";
11+
$n <<= $n;
12+
var_dump($n);
13+
14+
$n = "65";
15+
$n >>= $n;
16+
var_dump($n);
17+
18+
$n = "-1";
19+
$n >>= $n;
20+
var_dump($n);
21+
22+
?>
23+
--EXPECTF--
24+
int(0)
25+
26+
Warning: Bit shift by negative number in %s on line %d
27+
bool(false)
28+
int(0)
29+
30+
Warning: Bit shift by negative number in %s on line %d
31+
bool(false)

Zend/zend_operators.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,6 +1451,10 @@ ZEND_API int shift_left_function(zval *result, zval *op1, zval *op2) /* {{{ */
14511451

14521452
convert_op1_op2_long(op1, op1_lval, op2, op2_lval, ZEND_SL, shift_left_function);
14531453

1454+
if (op1 == result) {
1455+
zval_dtor(result);
1456+
}
1457+
14541458
/* prevent wrapping quirkiness on some processors where << 64 + x == << x */
14551459
if (UNEXPECTED((zend_ulong)op2_lval >= SIZEOF_ZEND_LONG * 8)) {
14561460
if (EXPECTED(op2_lval > 0)) {
@@ -1463,9 +1467,6 @@ ZEND_API int shift_left_function(zval *result, zval *op1, zval *op2) /* {{{ */
14631467
}
14641468
}
14651469

1466-
if (op1 == result) {
1467-
zval_dtor(result);
1468-
}
14691470
ZVAL_LONG(result, op1_lval << op2_lval);
14701471
return SUCCESS;
14711472
}
@@ -1477,6 +1478,10 @@ ZEND_API int shift_right_function(zval *result, zval *op1, zval *op2) /* {{{ */
14771478

14781479
convert_op1_op2_long(op1, op1_lval, op2, op2_lval, ZEND_SR, shift_right_function);
14791480

1481+
if (op1 == result) {
1482+
zval_dtor(result);
1483+
}
1484+
14801485
/* prevent wrapping quirkiness on some processors where >> 64 + x == >> x */
14811486
if (UNEXPECTED((zend_ulong)op2_lval >= SIZEOF_ZEND_LONG * 8)) {
14821487
if (EXPECTED(op2_lval > 0)) {
@@ -1489,9 +1494,6 @@ ZEND_API int shift_right_function(zval *result, zval *op1, zval *op2) /* {{{ */
14891494
}
14901495
}
14911496

1492-
if (op1 == result) {
1493-
zval_dtor(result);
1494-
}
14951497
ZVAL_LONG(result, op1_lval >> op2_lval);
14961498
return SUCCESS;
14971499
}

0 commit comments

Comments
 (0)