diff --git a/ext/ffi/ffi.c b/ext/ffi/ffi.c index 73e738202c8d..8c3c67fd1cb6 100644 --- a/ext/ffi/ffi.c +++ b/ext/ffi/ffi.c @@ -7490,14 +7490,88 @@ void zend_ffi_expr_mul(zend_ffi_val *val, zend_ffi_val *op2) /* {{{ */ } /* }}} */ +static bool zend_ffi_val_is_floating(const zend_ffi_val *val) /* {{{ */ +{ + return val->kind == ZEND_FFI_VAL_FLOAT || val->kind == ZEND_FFI_VAL_DOUBLE + || val->kind == ZEND_FFI_VAL_LONG_DOUBLE; +} +/* }}} */ + +/* Whether dividing val by op2 traps (integer division by zero, or INT64_MIN / -1). + * zend_ffi_expr_math() divides floating operands as floating point; + * zend_ffi_expr_int_math() first converts them to integers. */ +static bool zend_ffi_expr_div_traps(const zend_ffi_val *val, const zend_ffi_val *op2, bool int_math) /* {{{ */ +{ + bool val_fp = zend_ffi_val_is_floating(val); + bool op2_fp = zend_ffi_val_is_floating(op2); + int64_t n, d; + + if (val_fp || op2_fp) { + if (!int_math || (val_fp && (op2_fp || op2->kind == ZEND_FFI_VAL_CHAR))) { + return false; + } + } + switch (val->kind) { + case ZEND_FFI_VAL_UINT32: + case ZEND_FFI_VAL_UINT64: + case ZEND_FFI_VAL_INT32: + case ZEND_FFI_VAL_INT64: + n = val->i64; + break; + case ZEND_FFI_VAL_CHAR: + n = val->ch; + break; + case ZEND_FFI_VAL_FLOAT: + case ZEND_FFI_VAL_DOUBLE: + case ZEND_FFI_VAL_LONG_DOUBLE: + n = (int64_t)val->d; + break; + default: + return false; + } + switch (op2->kind) { + case ZEND_FFI_VAL_UINT32: + case ZEND_FFI_VAL_UINT64: + case ZEND_FFI_VAL_INT32: + case ZEND_FFI_VAL_INT64: + d = op2->i64; + break; + case ZEND_FFI_VAL_CHAR: + d = op2->ch; + break; + case ZEND_FFI_VAL_FLOAT: + case ZEND_FFI_VAL_DOUBLE: + case ZEND_FFI_VAL_LONG_DOUBLE: + d = (int64_t)op2->d; + break; + default: + return false; + } + if (d == 0) { + return true; + } + /* signed division: a signed (or converted floating) dividend by a signed divisor */ + return n == INT64_MIN && d == -1 + && (val->kind == ZEND_FFI_VAL_INT32 || val->kind == ZEND_FFI_VAL_INT64 || val_fp) + && (op2->kind == ZEND_FFI_VAL_INT32 || op2->kind == ZEND_FFI_VAL_INT64 + || op2->kind == ZEND_FFI_VAL_CHAR); +} +/* }}} */ + void zend_ffi_expr_div(zend_ffi_val *val, zend_ffi_val *op2) /* {{{ */ { + if (zend_ffi_expr_div_traps(val, op2, false)) { + zend_ffi_parser_error("Division by zero or overflow in constant expression at line %d", FFI_G(line)); + } zend_ffi_expr_math(val, op2, /); } /* }}} */ void zend_ffi_expr_mod(zend_ffi_val *val, zend_ffi_val *op2) /* {{{ */ { + if (zend_ffi_expr_div_traps(val, op2, true)) { + zend_ffi_parser_error("Division by zero or overflow in constant expression at line %d", FFI_G(line)); + } zend_ffi_expr_int_math(val, op2, %); // ??? } /* }}} */ diff --git a/ext/ffi/tests/gh25573.phpt b/ext/ffi/tests/gh25573.phpt new file mode 100644 index 000000000000..91332e5d9b54 --- /dev/null +++ b/ext/ffi/tests/gh25573.phpt @@ -0,0 +1,60 @@ +--TEST-- +GH-25573 (FFI constant expression division by zero or overflow) +--EXTENSIONS-- +ffi +--INI-- +ffi.enable=1 +--FILE-- +type("A")); + } catch (FFI\ParserException $e) { + return $e->getMessage(); + } +} +foreach ([ + "(1/0) ? 7 : 13", + "(1%0) ? 7 : 13", + "(1u/0) ? 7 : 13", + "('a'/0) ? 7 : 13", + "(1/'\\0') ? 7 : 13", + "((-9223372036854775807 - 1) / -1) ? 7 : 13", + "((-9223372036854775807 - 1) % -1) ? 7 : 13", + "((-9223372036854775807 - 1) / '\\377') ? 7 : 13", + "((-9223372036854775807 - 1) % '\\377') ? 7 : 13", + "(1.0 % 0) ? 7 : 13", + "(1 % 0.0) ? 7 : 13", + "(1 % 0.5) ? 7 : 13", + "((-9223372036854775807.0 - 1) % -1) ? 7 : 13", + // not trapping + "(1/2) ? 7 : 13", + "(7 % 4) ? 7 : 13", + "(1.0/0) ? 7 : 13", + "((-9223372036854775807 - 1) / 0xffffffffffffffffu) ? 7 : 13", + "((-9223372036854775807 - 1) % -2) ? 7 : 13", +] as $expr) { + echo $expr, ": "; + var_dump(size($expr)); +} +?> +--EXPECT-- +(1/0) ? 7 : 13: string(61) "Division by zero or overflow in constant expression at line 1" +(1%0) ? 7 : 13: string(61) "Division by zero or overflow in constant expression at line 1" +(1u/0) ? 7 : 13: string(61) "Division by zero or overflow in constant expression at line 1" +('a'/0) ? 7 : 13: string(61) "Division by zero or overflow in constant expression at line 1" +(1/'\0') ? 7 : 13: string(61) "Division by zero or overflow in constant expression at line 1" +((-9223372036854775807 - 1) / -1) ? 7 : 13: string(61) "Division by zero or overflow in constant expression at line 1" +((-9223372036854775807 - 1) % -1) ? 7 : 13: string(61) "Division by zero or overflow in constant expression at line 1" +((-9223372036854775807 - 1) / '\377') ? 7 : 13: string(61) "Division by zero or overflow in constant expression at line 1" +((-9223372036854775807 - 1) % '\377') ? 7 : 13: string(61) "Division by zero or overflow in constant expression at line 1" +(1.0 % 0) ? 7 : 13: string(61) "Division by zero or overflow in constant expression at line 1" +(1 % 0.0) ? 7 : 13: string(61) "Division by zero or overflow in constant expression at line 1" +(1 % 0.5) ? 7 : 13: string(61) "Division by zero or overflow in constant expression at line 1" +((-9223372036854775807.0 - 1) % -1) ? 7 : 13: string(61) "Division by zero or overflow in constant expression at line 1" +(1/2) ? 7 : 13: int(13) +(7 % 4) ? 7 : 13: int(7) +(1.0/0) ? 7 : 13: int(7) +((-9223372036854775807 - 1) / 0xffffffffffffffffu) ? 7 : 13: int(13) +((-9223372036854775807 - 1) % -2) ? 7 : 13: int(13)