Skip to content

FFI: struct with an enum field passed by value is described to libffi with a void element #25558

Open

Description

@DPDmancul

Description

Passing by value a struct with an enum field to a C function, causes the arrival of wrong data on the foreign side.

Example

test.c

typedef enum {
    RED = 0,
    GREEN = 1
} color;

typedef struct {
    char a;
    color b;
    char c;
} test;

int read_b(test x) {
    return x.b;
}

char read_c(test x) {
    return x.c;
}
gcc -shared -fPIC -o libtest.so test.c

test.php

<?php
$ffi = FFI::cdef(<<<EOF
    typedef enum {
        RED = 0,
        GREEN = 1
    } color;

    typedef struct {
        char a;
        color b;
        char c;
    } test;

    int read_b(test x);
    char read_c(test x);
    EOF,
    __DIR__ . '/libtest.so',
);

$test = $ffi->new('test');
$test->a = 2;
$test->b = $ffi->GREEN;
$test->c = 3;

var_dump($ffi->read_b($test));
var_dump((int)$ffi->read_c($test));

The result should be:

int(1)
int(3)

but we got instead:

int(1)
int(0)

Declaring the b filed of int type in cdef, the output is the expected one.

Cause

The switch in the function zend_ffi_face_struct_add_fields in ext/ffi/ffi.c has no case for ZEND_FFI_TYPE_ENUM, so it is treated as void(default case). I would have expected the enum case to be the same of an integer.

PHP Version

PHP 8.5.8 (cli) (built: Jul 14 2026 01:27:21) (NTS)
Copyright (c) The PHP Group
Built by https://github.com/docker-library/php
Zend Engine v4.5.8, Copyright (c) Zend Technologies
    with Xdebug v3.5.3, Copyright (c) 2002-2026, by Derick Rethans
    with Zend OPcache v8.5.8, Copyright (c), by Zend Technologies

Operating System

Debian (Docker, x86_64)

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions