Skip to content

Fix GH-25573: FFI constant expression evaluation - #25575

Open
Marti2203 wants to merge 2 commits into
php:PHP-8.4from
Marti2203:fix-gh25573-ffi-constexpr
Open

Marti2203 wants to merge 2 commits into
php:PHP-8.4from
Marti2203:fix-gh25573-ffi-constexpr

Conversation

@Marti2203

Copy link
Copy Markdown
Contributor

Fixes #25573.

  • zend_ffi_expr_div() / zend_ffi_expr_mod(): integer division or modulo by zero, and INT64_MIN / -1, raise an FFI\ParserException ("Division by zero or overflow in constant expression") instead of SIGFPE.
  • zend_ffi_val_character(): assign '\a', and convert the hex digits A-F/a-f to 10-15 (they gave 0-5).
  • zend_ffi_expr_sizeof_val(): sizeof of a character constant is sizeof(int).
  • zend_ffi_align_as_val(): _Alignas(N) uses N. _Alignas(0) has no effect; a value that is not a power of two is rejected.
  • zend_ffi_expr_cmp: an unsigned value compared with a character constant now uses op2->ch; it read the uninitialized op2->d.

The mixed signed/unsigned comparison semantics described in the issue (0xFFFFFFFFu == -1, -1 < 1u) are not changed here.

ext/ffi/tests/gh25573.phpt covers each case; it fails without the change (SIGFPE). ext/ffi/tests pass.

This issue was found by an LLM-assisted tool and verified manually.

- 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.
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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant