diff --git a/ext/ffi/ffi.c b/ext/ffi/ffi.c index 73e738202c8d..25389980e43b 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, %); // ??? } /* }}} */ @@ -7893,9 +7967,9 @@ void zend_ffi_val_character(zend_ffi_val *val, const char *str, size_t str_len) if (str[3] >= '0' && str[3] <= '9') { n = str[3] - '0'; } else if (str[3] >= 'A' && str[3] <= 'F') { - n = str[3] - 'A'; + n = str[3] - 'A' + 10; } else if (str[3] >= 'a' && str[3] <= 'f') { - n = str[3] - 'a'; + n = str[3] - 'a' + 10; } else { val->kind = ZEND_FFI_VAL_ERROR; return; @@ -7903,9 +7977,9 @@ void zend_ffi_val_character(zend_ffi_val *val, const char *str, size_t str_len) if ((str[4] >= '0' && str[4] <= '9') && str_len == 6) { n = n * 16 + (str[4] - '0'); } else if ((str[4] >= 'A' && str[4] <= 'F') && str_len == 6) { - n = n * 16 + (str[4] - 'A'); + n = n * 16 + (str[4] - 'A' + 10); } else if ((str[4] >= 'a' && str[4] <= 'f') && str_len == 6) { - n = n * 16 + (str[4] - 'a'); + n = n * 16 + (str[4] - 'a' + 10); } else if (str_len != 5) { val->kind = ZEND_FFI_VAL_ERROR; return; 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) diff --git a/ext/ffi/tests/gh25573_hex.phpt b/ext/ffi/tests/gh25573_hex.phpt new file mode 100644 index 000000000000..d15a288890df --- /dev/null +++ b/ext/ffi/tests/gh25573_hex.phpt @@ -0,0 +1,41 @@ +--TEST-- +GH-25573 (FFI hex escapes with digits A-F) +--EXTENSIONS-- +ffi +--INI-- +ffi.enable=1 +--FILE-- +type("A")); + } catch (FFI\ParserException $e) { + return $e->getMessage(); + } +} +foreach ([ + "'\\x9'", + "'\\xA'", + "'\\xf'", + "'\\x41'", + "'\\x1f'", + "'\\x7F'", + "'\\x6B'", + "'\\x5c'", + "((-9223372036854775807 - 1) / '\\xff') ? 7 : 13", +] as $expr) { + echo $expr, ": "; + var_dump(size($expr)); +} +?> +--EXPECT-- +'\x9': int(9) +'\xA': int(10) +'\xf': int(15) +'\x41': int(65) +'\x1f': int(31) +'\x7F': int(127) +'\x6B': int(107) +'\x5c': int(92) +((-9223372036854775807 - 1) / '\xff') ? 7 : 13: string(61) "Division by zero or overflow in constant expression at line 1"