From 454793623a313617ca30f53f061c4f292c8ce01f Mon Sep 17 00:00:00 2001 From: Martin Mirchev Date: Thu, 19 Nov 2026 13:20:56 +0800 Subject: [PATCH 1/4] Fix GH-25573: FFI constant expression evaluation - Integer division or modulo by zero, and INT64_MIN / -1, raised SIGFPE; raise an FFI\ParserException instead. - '\a' was never assigned, and the hex digits A-F/a-f in '\xNN' were converted to 0-5. - sizeof of a character constant is sizeof(int), as in C. - _Alignas(N) used the alignment of the constant's type instead of N. - An unsigned value compared with a character constant read the uninitialized double member of the operand. --- ext/ffi/ffi.c | 57 +++++++++++++++++++++++++++++++------- ext/ffi/tests/gh25573.phpt | 44 +++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 10 deletions(-) create mode 100644 ext/ffi/tests/gh25573.phpt diff --git a/ext/ffi/ffi.c b/ext/ffi/ffi.c index 73e738202c8d..9d3a1b020428 100644 --- a/ext/ffi/ffi.c +++ b/ext/ffi/ffi.c @@ -7130,11 +7130,16 @@ void zend_ffi_align_as_val(zend_ffi_dcl *dcl, zend_ffi_val *align_val) /* {{{ */ switch (align_val->kind) { case ZEND_FFI_VAL_INT32: case ZEND_FFI_VAL_UINT32: - dcl->align = zend_ffi_type_uint32.align; - break; case ZEND_FFI_VAL_INT64: case ZEND_FFI_VAL_UINT64: - dcl->align = zend_ffi_type_uint64.align; + /* _Alignas(0) has no effect */ + if (align_val->u64 == 0) { + break; + } + if (align_val->u64 > 0x80000000 || (align_val->u64 & (align_val->u64 - 1)) != 0) { + zend_ffi_parser_error("Invalid alignment %" PRIu64 " at line %d", align_val->u64, FFI_G(line)); + } + dcl->align = (uint32_t)align_val->u64; break; case ZEND_FFI_VAL_FLOAT: dcl->align = zend_ffi_type_float.align; @@ -7312,7 +7317,7 @@ void zend_ffi_align_as_val(zend_ffi_dcl *dcl, zend_ffi_val *align_val) /* {{{ */ val->i64 = (zend_ffi_double)val->u64 OP op2->d; \ } else if (op2->kind == ZEND_FFI_VAL_CHAR) { \ val->kind = ZEND_FFI_VAL_INT32; \ - val->i64 = val->u64 OP op2->d; \ + val->i64 = val->u64 OP op2->ch; \ } else { \ val->kind = ZEND_FFI_VAL_ERROR; \ } \ @@ -7490,14 +7495,45 @@ void zend_ffi_expr_mul(zend_ffi_val *val, zend_ffi_val *op2) /* {{{ */ } /* }}} */ +static bool zend_ffi_expr_int_div_invalid(const zend_ffi_val *val, const zend_ffi_val *op2) /* {{{ */ +{ + bool val_int = val->kind == ZEND_FFI_VAL_UINT32 || val->kind == ZEND_FFI_VAL_UINT64 + || val->kind == ZEND_FFI_VAL_INT32 || val->kind == ZEND_FFI_VAL_INT64 || val->kind == ZEND_FFI_VAL_CHAR; + + if (!val_int) { + return false; + } + switch (op2->kind) { + case ZEND_FFI_VAL_UINT32: + case ZEND_FFI_VAL_UINT64: + return op2->u64 == 0; + case ZEND_FFI_VAL_INT32: + case ZEND_FFI_VAL_INT64: + return op2->i64 == 0 + || (op2->i64 == -1 && val->i64 == INT64_MIN + && (val->kind == ZEND_FFI_VAL_INT32 || val->kind == ZEND_FFI_VAL_INT64)); + case ZEND_FFI_VAL_CHAR: + return op2->ch == 0; + default: + return false; + } +} +/* }}} */ + void zend_ffi_expr_div(zend_ffi_val *val, zend_ffi_val *op2) /* {{{ */ { + if (zend_ffi_expr_int_div_invalid(val, op2)) { + 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_int_div_invalid(val, op2)) { + 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, %); // ??? } /* }}} */ @@ -7710,7 +7746,7 @@ void zend_ffi_expr_sizeof_val(zend_ffi_val *val) /* {{{ */ #endif } else if (val->kind == ZEND_FFI_VAL_CHAR) { val->kind = ZEND_FFI_VAL_UINT32; - val->u64 = zend_ffi_type_char.size; + val->u64 = zend_ffi_type_sint32.size; /* a character constant has type int */ } else if (val->kind == ZEND_FFI_VAL_STRING) { if (memchr(val->str, '\\', val->len)) { // TODO: support for escape sequences ??? @@ -7859,7 +7895,8 @@ void zend_ffi_val_character(zend_ffi_val *val, const char *str, size_t str_len) if (str_len == 3) { val->ch = str[1]; } else if (str[1] == '\\') { - if (str[2] == 'a') { + if (str[2] == 'a' && str_len == 4) { + val->ch = '\a'; } else if (str[2] == 'b' && str_len == 4) { val->ch = '\b'; } else if (str[2] == 'f' && str_len == 4) { @@ -7893,9 +7930,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 +7940,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..0a3d78972aaf --- /dev/null +++ b/ext/ffi/tests/gh25573.phpt @@ -0,0 +1,44 @@ +--TEST-- +GH-25573 (FFI constant expression evaluation) +--EXTENSIONS-- +ffi +--INI-- +ffi.enable=1 +--FILE-- +type("A")); + } catch (FFI\ParserException $e) { + return $e->getMessage(); + } +} +var_dump(size("'\\a'")); +var_dump(size("'\\xA'")); +var_dump(size("'\\x41'")); +var_dump(size("'\\x1f'")); +var_dump(size("sizeof('a')")); +var_dump(size("(0xFFFFFFFFu > 'A') ? 7 : 13")); +var_dump(FFI::alignof(FFI::cdef("typedef _Alignas(16) char T;")->type("T"))); +var_dump(FFI::alignof(FFI::cdef("typedef _Alignas(0) int T;")->type("T"))); +try { FFI::cdef("typedef _Alignas(3) char T;"); } catch (FFI\ParserException $e) { echo $e->getMessage(), "\n"; } +var_dump(size("(1/0) ? 7 : 13")); +var_dump(size("(1%0) ? 7 : 13")); +var_dump(size("((-9223372036854775807 - 1) / -1) ? 7 : 13")); +var_dump(size("(1/2) ? 7 : 13")); +?> +--EXPECT-- +int(7) +int(10) +int(65) +int(31) +int(4) +int(7) +int(16) +int(4) +Invalid alignment 3 at line 1 +string(61) "Division by zero or overflow in constant expression at line 1" +string(61) "Division by zero or overflow in constant expression at line 1" +string(61) "Division by zero or overflow in constant expression at line 1" +int(13) From d558718cf5d1acedc73058484dbfa64c29635079 Mon Sep 17 00:00:00 2001 From: Martin Mirchev Date: Thu, 19 Nov 2026 14:25:50 +0800 Subject: [PATCH 2/4] Keep the FFI instance alive in the GH-25573 test A CType obtained from a temporary FFI instance points into the instance's symbol table, which is freed with it (ASan: heap-use-after-free in FFI::alignof). --- ext/ffi/tests/gh25573.phpt | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ext/ffi/tests/gh25573.phpt b/ext/ffi/tests/gh25573.phpt index 0a3d78972aaf..36bdbb95b644 100644 --- a/ext/ffi/tests/gh25573.phpt +++ b/ext/ffi/tests/gh25573.phpt @@ -20,8 +20,10 @@ var_dump(size("'\\x41'")); var_dump(size("'\\x1f'")); var_dump(size("sizeof('a')")); var_dump(size("(0xFFFFFFFFu > 'A') ? 7 : 13")); -var_dump(FFI::alignof(FFI::cdef("typedef _Alignas(16) char T;")->type("T"))); -var_dump(FFI::alignof(FFI::cdef("typedef _Alignas(0) int T;")->type("T"))); +$ffi = FFI::cdef("typedef _Alignas(16) char T;"); +var_dump(FFI::alignof($ffi->type("T"))); +$ffi = FFI::cdef("typedef _Alignas(0) int T;"); +var_dump(FFI::alignof($ffi->type("T"))); try { FFI::cdef("typedef _Alignas(3) char T;"); } catch (FFI\ParserException $e) { echo $e->getMessage(), "\n"; } var_dump(size("(1/0) ? 7 : 13")); var_dump(size("(1%0) ? 7 : 13")); From b4d9707568262901a05c3f99263c282247e213f8 Mon Sep 17 00:00:00 2001 From: Martin Mirchev Date: Fri, 20 Nov 2026 08:08:30 +0800 Subject: [PATCH 3/4] Reject floating operands of % in FFI constant expressions The operands of % must have integer type. A floating operand was converted to an integer before the remainder, so 1.0 % 0 or 1 % 0.0 raised SIGFPE. --- ext/ffi/ffi.c | 11 +++++++++++ ext/ffi/tests/gh25573.phpt | 8 ++++++++ 2 files changed, 19 insertions(+) diff --git a/ext/ffi/ffi.c b/ext/ffi/ffi.c index 9d3a1b020428..2afdb6bdc6b8 100644 --- a/ext/ffi/ffi.c +++ b/ext/ffi/ffi.c @@ -7529,8 +7529,19 @@ void zend_ffi_expr_div(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; +} +/* }}} */ + void zend_ffi_expr_mod(zend_ffi_val *val, zend_ffi_val *op2) /* {{{ */ { + /* the operands of % must have integer type */ + if (zend_ffi_val_is_floating(val) || zend_ffi_val_is_floating(op2)) { + zend_ffi_parser_error("Invalid operand of '%%' in constant expression at line %d", FFI_G(line)); + } if (zend_ffi_expr_int_div_invalid(val, op2)) { zend_ffi_parser_error("Division by zero or overflow in constant expression at line %d", FFI_G(line)); } diff --git a/ext/ffi/tests/gh25573.phpt b/ext/ffi/tests/gh25573.phpt index 36bdbb95b644..46089b507eed 100644 --- a/ext/ffi/tests/gh25573.phpt +++ b/ext/ffi/tests/gh25573.phpt @@ -29,6 +29,10 @@ var_dump(size("(1/0) ? 7 : 13")); var_dump(size("(1%0) ? 7 : 13")); var_dump(size("((-9223372036854775807 - 1) / -1) ? 7 : 13")); var_dump(size("(1/2) ? 7 : 13")); +var_dump(size("(1.0 % 0) ? 7 : 13")); +var_dump(size("(1 % 0.0) ? 7 : 13")); +var_dump(size("((-9223372036854775807.0 - 1) % -1) ? 7 : 13")); +var_dump(size("(7 % 4) ? 7 : 13")); ?> --EXPECT-- int(7) @@ -44,3 +48,7 @@ string(61) "Division by zero or overflow in constant expression at line 1" string(61) "Division by zero or overflow in constant expression at line 1" string(61) "Division by zero or overflow in constant expression at line 1" int(13) +string(55) "Invalid operand of '%' in constant expression at line 1" +string(55) "Invalid operand of '%' in constant expression at line 1" +string(55) "Invalid operand of '%' in constant expression at line 1" +int(7) From f920a7592a0c222d2996e61ecad52688c5f59a89 Mon Sep 17 00:00:00 2001 From: Martin Mirchev Date: Wed, 25 Nov 2026 09:08:31 +0800 Subject: [PATCH 4/4] Limit the GH-25573 fix to divisions that trap Keep only the SIGFPE fixes on PHP-8.4: reject an integer division or remainder by zero, and INT64_MIN / -1, after the operand conversions that zend_ffi_expr_math() and zend_ffi_expr_int_math() perform, so a character divisor such as '\377' and floating operands of % are covered. The changes to constant values (_Alignas, sizeof of a character constant, escape sequences, character comparisons) move to master. --- ext/ffi/ffi.c | 104 +++++++++++++++++++++++-------------- ext/ffi/tests/gh25573.phpt | 80 +++++++++++++++------------- 2 files changed, 108 insertions(+), 76 deletions(-) diff --git a/ext/ffi/ffi.c b/ext/ffi/ffi.c index 2afdb6bdc6b8..8c3c67fd1cb6 100644 --- a/ext/ffi/ffi.c +++ b/ext/ffi/ffi.c @@ -7130,16 +7130,11 @@ void zend_ffi_align_as_val(zend_ffi_dcl *dcl, zend_ffi_val *align_val) /* {{{ */ switch (align_val->kind) { case ZEND_FFI_VAL_INT32: case ZEND_FFI_VAL_UINT32: + dcl->align = zend_ffi_type_uint32.align; + break; case ZEND_FFI_VAL_INT64: case ZEND_FFI_VAL_UINT64: - /* _Alignas(0) has no effect */ - if (align_val->u64 == 0) { - break; - } - if (align_val->u64 > 0x80000000 || (align_val->u64 & (align_val->u64 - 1)) != 0) { - zend_ffi_parser_error("Invalid alignment %" PRIu64 " at line %d", align_val->u64, FFI_G(line)); - } - dcl->align = (uint32_t)align_val->u64; + dcl->align = zend_ffi_type_uint64.align; break; case ZEND_FFI_VAL_FLOAT: dcl->align = zend_ffi_type_float.align; @@ -7317,7 +7312,7 @@ void zend_ffi_align_as_val(zend_ffi_dcl *dcl, zend_ffi_val *align_val) /* {{{ */ val->i64 = (zend_ffi_double)val->u64 OP op2->d; \ } else if (op2->kind == ZEND_FFI_VAL_CHAR) { \ val->kind = ZEND_FFI_VAL_INT32; \ - val->i64 = val->u64 OP op2->ch; \ + val->i64 = val->u64 OP op2->d; \ } else { \ val->kind = ZEND_FFI_VAL_ERROR; \ } \ @@ -7495,54 +7490,86 @@ void zend_ffi_expr_mul(zend_ffi_val *val, zend_ffi_val *op2) /* {{{ */ } /* }}} */ -static bool zend_ffi_expr_int_div_invalid(const zend_ffi_val *val, const 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_int = val->kind == ZEND_FFI_VAL_UINT32 || val->kind == ZEND_FFI_VAL_UINT64 - || val->kind == ZEND_FFI_VAL_INT32 || val->kind == ZEND_FFI_VAL_INT64 || val->kind == ZEND_FFI_VAL_CHAR; + bool val_fp = zend_ffi_val_is_floating(val); + bool op2_fp = zend_ffi_val_is_floating(op2); + int64_t n, d; - if (!val_int) { - return false; + 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: - return op2->u64 == 0; case ZEND_FFI_VAL_INT32: case ZEND_FFI_VAL_INT64: - return op2->i64 == 0 - || (op2->i64 == -1 && val->i64 == INT64_MIN - && (val->kind == ZEND_FFI_VAL_INT32 || val->kind == ZEND_FFI_VAL_INT64)); + d = op2->i64; + break; case ZEND_FFI_VAL_CHAR: - return op2->ch == 0; + 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_int_div_invalid(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, /); } /* }}} */ -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; -} -/* }}} */ - void zend_ffi_expr_mod(zend_ffi_val *val, zend_ffi_val *op2) /* {{{ */ { - /* the operands of % must have integer type */ - if (zend_ffi_val_is_floating(val) || zend_ffi_val_is_floating(op2)) { - zend_ffi_parser_error("Invalid operand of '%%' in constant expression at line %d", FFI_G(line)); - } - if (zend_ffi_expr_int_div_invalid(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, %); // ??? @@ -7757,7 +7784,7 @@ void zend_ffi_expr_sizeof_val(zend_ffi_val *val) /* {{{ */ #endif } else if (val->kind == ZEND_FFI_VAL_CHAR) { val->kind = ZEND_FFI_VAL_UINT32; - val->u64 = zend_ffi_type_sint32.size; /* a character constant has type int */ + val->u64 = zend_ffi_type_char.size; } else if (val->kind == ZEND_FFI_VAL_STRING) { if (memchr(val->str, '\\', val->len)) { // TODO: support for escape sequences ??? @@ -7906,8 +7933,7 @@ void zend_ffi_val_character(zend_ffi_val *val, const char *str, size_t str_len) if (str_len == 3) { val->ch = str[1]; } else if (str[1] == '\\') { - if (str[2] == 'a' && str_len == 4) { - val->ch = '\a'; + if (str[2] == 'a') { } else if (str[2] == 'b' && str_len == 4) { val->ch = '\b'; } else if (str[2] == 'f' && str_len == 4) { @@ -7941,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' + 10; + n = str[3] - 'A'; } else if (str[3] >= 'a' && str[3] <= 'f') { - n = str[3] - 'a' + 10; + n = str[3] - 'a'; } else { val->kind = ZEND_FFI_VAL_ERROR; return; @@ -7951,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' + 10); + n = n * 16 + (str[4] - 'A'); } else if ((str[4] >= 'a' && str[4] <= 'f') && str_len == 6) { - n = n * 16 + (str[4] - 'a' + 10); + n = n * 16 + (str[4] - 'a'); } 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 index 46089b507eed..91332e5d9b54 100644 --- a/ext/ffi/tests/gh25573.phpt +++ b/ext/ffi/tests/gh25573.phpt @@ -1,5 +1,5 @@ --TEST-- -GH-25573 (FFI constant expression evaluation) +GH-25573 (FFI constant expression division by zero or overflow) --EXTENSIONS-- ffi --INI-- @@ -14,41 +14,47 @@ function size(string $expr) { return $e->getMessage(); } } -var_dump(size("'\\a'")); -var_dump(size("'\\xA'")); -var_dump(size("'\\x41'")); -var_dump(size("'\\x1f'")); -var_dump(size("sizeof('a')")); -var_dump(size("(0xFFFFFFFFu > 'A') ? 7 : 13")); -$ffi = FFI::cdef("typedef _Alignas(16) char T;"); -var_dump(FFI::alignof($ffi->type("T"))); -$ffi = FFI::cdef("typedef _Alignas(0) int T;"); -var_dump(FFI::alignof($ffi->type("T"))); -try { FFI::cdef("typedef _Alignas(3) char T;"); } catch (FFI\ParserException $e) { echo $e->getMessage(), "\n"; } -var_dump(size("(1/0) ? 7 : 13")); -var_dump(size("(1%0) ? 7 : 13")); -var_dump(size("((-9223372036854775807 - 1) / -1) ? 7 : 13")); -var_dump(size("(1/2) ? 7 : 13")); -var_dump(size("(1.0 % 0) ? 7 : 13")); -var_dump(size("(1 % 0.0) ? 7 : 13")); -var_dump(size("((-9223372036854775807.0 - 1) % -1) ? 7 : 13")); -var_dump(size("(7 % 4) ? 7 : 13")); +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-- -int(7) -int(10) -int(65) -int(31) -int(4) -int(7) -int(16) -int(4) -Invalid alignment 3 at line 1 -string(61) "Division by zero or overflow in constant expression at line 1" -string(61) "Division by zero or overflow in constant expression at line 1" -string(61) "Division by zero or overflow in constant expression at line 1" -int(13) -string(55) "Invalid operand of '%' in constant expression at line 1" -string(55) "Invalid operand of '%' in constant expression at line 1" -string(55) "Invalid operand of '%' in constant expression at line 1" -int(7) +(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)