Skip to content

FFI constant expressions: 1/0 crashes; '\a', hex escapes, sizeof('a'), _Alignas(N) and some comparisons are evaluated wrongly #25573

Open

Description

@Marti2203

Description

Several errors in the evaluation of C constant expressions in FFI::cdef() (array sizes, enum values,
_Alignas). Each line below shows what FFI computes and what C gives (gcc).

Division by zero crashes the process. zend_ffi_expr_div() / zend_ffi_expr_mod() divide without
checking the divisor:

<?php
FFI::cdef("typedef char A[(1/0) ? 7 : 13];");

Result: Floating point exception (core dumped) (exit 136). The same for 1 % 0 and for
(-9223372036854775807 - 1) / -1. Expected: an FFI\ParserException, as for other invalid declarations.

Character constants:

<?php
function size($e) { return FFI::sizeof(FFI::cdef("typedef char A[$e];")->type("A")); }
var_dump(size("'\\a'"), size("'\\xA'"), size("'\\x1f'"), size("sizeof('a')"));

Result: 0, 0, 21, 1. Expected (C): 7, 10, 31, 4.

  • zend_ffi_val_character() has an empty branch for \a, so the value is never set.
  • Hex digits A-F / a-f are converted as str[3] - 'A' instead of str[3] - 'A' + 10.
  • A character constant has type int in C, but zend_ffi_expr_sizeof_val() uses the size of char.

_Alignas(constant) ignores the value. zend_ffi_align_as_val() uses the alignment of the
constant's type:

<?php
var_dump(FFI::alignof(FFI::cdef("typedef _Alignas(16) char T;")->type("T")));

Result: int(4). Expected: int(16). (_Alignas(double), the type form, works.)

Unsigned vs character comparison reads the wrong union member. In zend_ffi_expr_cmp, the
unsigned-vs-CHAR case compares val->u64 OP op2->d, but only op2->ch is set, so the result depends
on stale memory:

<?php
$c = FFI::cdef("typedef char A[(0xFFFFFFFFu > 'A') ? 7 : 13]; typedef char S[(1.0e300 > 2.0) ? 7 : 13]; typedef char B[(0xFFFFFFFFu > 'A') ? 7 : 13];");
var_dump(FFI::sizeof($c->type("A")), FFI::sizeof($c->type("B")));

Result: int(13) int(7) (the same expression gives two answers). Expected: int(7) int(7).

Mixed signed/unsigned comparisons do not follow C's conversions. (0xFFFFFFFFu == -1) is false
(C: true, both operands become unsigned int) and (-1 < 1u) is true (C: false). The comparison macro
compares i64 against u64 directly (/* signed/unsigned */). This one needs the usual arithmetic
conversions and is not changed by the patch below.

uint32-eq-minus1 size=13 (C: 7)
int-lt-uint size=7 (C: 13)

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

PHP Version

PHP 8.7.0-dev (cli) (NTS), master e709ba40; same code on PHP-8.4, PHP-8.5, PHP-8.6

Operating System

Ubuntu 20.04 (x86-64)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions