Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions ext/ffi/ffi.c
Original file line number Diff line number Diff line change
Expand Up @@ -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, %); // ???
}
/* }}} */
Expand Down
60 changes: 60 additions & 0 deletions ext/ffi/tests/gh25573.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
--TEST--
GH-25573 (FFI constant expression division by zero or overflow)
--EXTENSIONS--
ffi
--INI--
ffi.enable=1
--FILE--
<?php
function size(string $expr) {
try {
$ffi = FFI::cdef("typedef char A[$expr];");
return FFI::sizeof($ffi->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)
Loading