com php-src: Protect against overflows in pi constraints: ext/opcache/Optimizer/zend_ssa.c

From: Date: Sun, 20 Dec 2015 11:56:05 +0000
Subject: com php-src: Protect against overflows in pi constraints: ext/opcache/Optimizer/zend_ssa.c
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-90360@lists.php.net to get a copy of this message
Commit: 420ff42bc0339df78817fdb04f240f86cd32ef61 Author: Nikita Popov <nikic@php.net> Sun, 20 Dec 2015 12:47:39 +0100 Parents: 825b5901dfdd5fa25f3467e7c0ee9bce04ae61a1 Branches: master Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=420ff42bc0339df78817fdb04f240f86cd32ef61 Log: Protect against overflows in pi constraints Changed paths: M ext/opcache/Optimizer/zend_ssa.c Diff: diff --git a/ext/opcache/Optimizer/zend_ssa.c b/ext/opcache/Optimizer/zend_ssa.c index 41ef21c..0a63ecf 100644 --- a/ext/opcache/Optimizer/zend_ssa.c +++ b/ext/opcache/Optimizer/zend_ssa.c @@ -120,6 +120,15 @@ static int find_adjusted_tmp_var(const zend_op_array *op_array, uint32_t build_f return -1; } +static inline zend_bool add_will_overflow(zend_long a, zend_long b) { + return (b > 0 && a > ZEND_LONG_MAX - b) + || (b < 0 && a < ZEND_LONG_MIN - b); +} +static inline zend_bool sub_will_overflow(zend_long a, zend_long b) { + return (b > 0 && a < ZEND_LONG_MIN + b) + || (b < 0 && a > ZEND_LONG_MAX + b); +} + static int zend_ssa_rename(const zend_op_array *op_array, zend_ssa *ssa, int *var, int n) /* {{{ */ { zend_basic_block *blocks = ssa->cfg.blocks; @@ -602,32 +611,49 @@ int zend_build_ssa(zend_arena **arena, const zend_op_array *op_array, uint32_t b } if (var1 >= 0 && var2 >= 0) { - int tmp = val1; - val1 -= val2; - val2 -= tmp; + if (!sub_will_overflow(val1, val2) && !sub_will_overflow(val2, val1)) { + zend_long tmp = val1; + val1 -= val2; + val2 -= tmp; + } else { + var1 = -1; + var2 = -1; + } } else if (var1 >= 0 && var2 < 0) { + zend_long add_val2 = 0; if ((opline-1)->op2_type == IS_CONST && Z_TYPE_P(CRT_CONSTANT((opline-1)->op2)) == IS_LONG) { - val2 += Z_LVAL_P(CRT_CONSTANT((opline-1)->op2)); + add_val2 = Z_LVAL_P(CRT_CONSTANT((opline-1)->op2)); } else if ((opline-1)->op2_type == IS_CONST && Z_TYPE_P(CRT_CONSTANT((opline-1)->op2)) == IS_FALSE) { - val2 += 0; + add_val2 = 0; } else if ((opline-1)->op2_type == IS_CONST && Z_TYPE_P(CRT_CONSTANT((opline-1)->op2)) == IS_TRUE) { - val2 += 1; + add_val2 = 1; + } else { + var1 = -1; + } + if (!add_will_overflow(val2, add_val2)) { + val2 += add_val2; } else { var1 = -1; } } else if (var1 < 0 && var2 >= 0) { + zend_long add_val1 = 0; if ((opline-1)->op1_type == IS_CONST && Z_TYPE_P(CRT_CONSTANT((opline-1)->op1)) == IS_LONG) { - val1 += Z_LVAL_P(CRT_CONSTANT((opline-1)->op1)); + add_val1 = Z_LVAL_P(CRT_CONSTANT((opline-1)->op1)); } else if ((opline-1)->op1_type == IS_CONST && Z_TYPE_P(CRT_CONSTANT((opline-1)->op1)) == IS_FALSE) { - val1 += 0; + add_val1 = 0; } else if ((opline-1)->op1_type == IS_CONST && Z_TYPE_P(CRT_CONSTANT((opline-1)->op1)) == IS_TRUE) { - val1 += 1; + add_val1 = 1; + } else { + var2 = -1; + } + if (!add_will_overflow(val1, add_val1)) { + val1 += add_val1; } else { var2 = -1; }

« previous php.cvs (#90360) next »