Skip to content

Commit 8f1fee6

Browse files
author
Ard Biesheuvel
committed
Zend: fix overflow handling bug in non-x86 fast_add_function()
The 'result' argument of fast_add_function() may alias with either of its operands (or both). Take care not to write to 'result' before reading op1 and op2.
1 parent b1b23ab commit 8f1fee6

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

Zend/zend_operators.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -593,13 +593,18 @@ static zend_always_inline int fast_add_function(zval *result, zval *op1, zval *o
593593
"r"(op2)
594594
: "rax");
595595
#else
596-
Z_LVAL_P(result) = Z_LVAL_P(op1) + Z_LVAL_P(op2);
596+
/*
597+
* 'result' may alias with op1 or op2, so we need to
598+
* ensure that 'result' is not updated until after we
599+
* have read the values of op1 and op2.
600+
*/
597601

598602
if (UNEXPECTED((Z_LVAL_P(op1) & LONG_SIGN_MASK) == (Z_LVAL_P(op2) & LONG_SIGN_MASK)
599-
&& (Z_LVAL_P(op1) & LONG_SIGN_MASK) != (Z_LVAL_P(result) & LONG_SIGN_MASK))) {
603+
&& (Z_LVAL_P(op1) & LONG_SIGN_MASK) != ((Z_LVAL_P(op1) + Z_LVAL_P(op2)) & LONG_SIGN_MASK))) {
600604
Z_DVAL_P(result) = (double) Z_LVAL_P(op1) + (double) Z_LVAL_P(op2);
601605
Z_TYPE_P(result) = IS_DOUBLE;
602606
} else {
607+
Z_LVAL_P(result) = Z_LVAL_P(op1) + Z_LVAL_P(op2);
603608
Z_TYPE_P(result) = IS_LONG;
604609
}
605610
#endif

0 commit comments

Comments
 (0)