bug in zend_multiply.h

php.internals

Ard Biesheuvel

22 years ago
This code fails on 64-bit archs because it relies on the accuracy of a double to be sufficient to represent LONG_MAX. The patch adresses it by checking for overflow by reversing the calculation instead of casting to a double and back to long. #include "build/php5-cvs/Zend/zend_multiply.h" #define LONG_MIN (1L << (8*sizeof(long)-1)) #define LONG_MAX ~LONG_MIN int main() { long res = 0; double d = 0; int overflow; /* obviously (LONG_MAX-1) * 1 can be represented by a long */ ZEND_SIGNED_MULTIPLY_LONG(LONG_MAX-1,1,res,d,overflow); printf("res: %ld/%.4f, %soverflow\n", result, d, overflow?"":"no "); return 0; }
-- Ard