cvs: ZendEngine2 / zend_operators.c

From: Date: Sun, 14 Sep 2003 17:37:01 +0000
Subject: cvs: ZendEngine2 / zend_operators.c
Groups: php.zend-engine.cvs 
Request: Send a blank email to php-zend-engine-cvs+get-1833@lists.php.net to get a copy of this message
zeev Sun Sep 14 13:37:01 2003 EDT Modified files: /ZendEngine2 zend_operators.c Log: Commit 64-bit fixes to the standard operators by Ard Biesheuvel (abies@php.net) Index: ZendEngine2/zend_operators.c diff -u ZendEngine2/zend_operators.c:1.160 ZendEngine2/zend_operators.c:1.161 --- ZendEngine2/zend_operators.c:1.160 Sun Aug 17 15:11:48 2003 +++ ZendEngine2/zend_operators.c Sun Sep 14 13:37:01 2003 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: zend_operators.c,v 1.160 2003/08/17 19:11:48 helly Exp $ */ +/* $Id: zend_operators.c,v 1.161 2003/09/14 17:37:01 zeev Exp $ */ #include <ctype.h> @@ -33,6 +33,8 @@ #include "ext/bcmath/number.h" #endif +#define LONG_SIGN_MASK (1L << (8*SIZEOF_LONG-1)) + ZEND_API int zend_atoi(const char *str, int str_len) { int retval; @@ -664,13 +666,16 @@ if (op1->type == IS_LONG && op2->type == IS_LONG) { - double dval = (double) op1->value.lval + (double) op2->value.lval; + long lval = op1->value.lval + op2->value.lval; + + /* check for overflow by comparing sign bits */ + if ( (op1->value.lval & LONG_SIGN_MASK) == (op2->value.lval & LONG_SIGN_MASK) + && (op1->value.lval & LONG_SIGN_MASK) != (lval & LONG_SIGN_MASK)) { - if ((dval > (double) LONG_MAX) || (dval < (double) LONG_MIN)) { - result->value.dval = dval; + result->value.dval = (double) op1->value.lval + (double) op2->value.lval; result->type = IS_DOUBLE; } else { - result->value.lval = op1->value.lval + op2->value.lval; + result->value.lval = lval; result->type = IS_LONG; } return SUCCESS; @@ -701,13 +706,16 @@ zendi_convert_scalar_to_number(op2, op2_copy, result); if (op1->type == IS_LONG && op2->type == IS_LONG) { - double dval = (double) op1->value.lval - (double) op2->value.lval; + long lval = op1->value.lval - op2->value.lval; + + /* check for overflow by comparing sign bits */ + if ( (op1->value.lval & LONG_SIGN_MASK) != (op2->value.lval & LONG_SIGN_MASK) + && (op1->value.lval & LONG_SIGN_MASK) != (lval & LONG_SIGN_MASK)) { - if ((dval < (double) LONG_MIN) || (dval > (double) LONG_MAX)) { - result->value.dval = dval; + result->value.dval = (double) op1->value.lval - (double) op2->value.lval; result->type = IS_DOUBLE; } else { - result->value.lval = op1->value.lval - op2->value.lval; + result->value.lval = lval; result->type = IS_LONG; } return SUCCESS; @@ -738,13 +746,14 @@ zendi_convert_scalar_to_number(op2, op2_copy, result); if (op1->type == IS_LONG && op2->type == IS_LONG) { - double dval = (double) op1->value.lval * (double) op2->value.lval; - - if ((dval > (double) LONG_MAX) || (dval < (double) LONG_MIN)) { - result->value.dval = dval; + long lval = op1->value.lval * op2->value.lval; + + /* check for overflow by applying the reverse calculation */ + if (op1->value.lval != 0 && lval / op1->value.lval != op2->value.lval) { + result->value.dval = (double) op1->value.lval * (double) op2->value.lval; result->type = IS_DOUBLE; } else { - result->value.lval = op1->value.lval * op2->value.lval; + result->value.lval = lval; result->type = IS_LONG; } return SUCCESS;

« previous php.zend-engine.cvs (#1833) next »