Skip to content

FFI passes PHP bools to variadic C functions without promoting them to int #25574

Open

Description

@Marti2203

Description

zend_ffi_pass_var_arg() passes true/false as ffi_type_uint8. For a variadic argument C applies
the default argument promotions, so the callee reads an int; the upper bytes are whatever was in the
register or stack slot:

<?php
$ffi = FFI::cdef("int snprintf(char *s, size_t n, const char *fmt, ...);");
$buf = $ffi->new("char[64]");
$ffi->snprintf($buf, 64, "%d %d %d %d %d %d %d %d", -1, -1, -1, -1, -1, -1, -1, -1);
$ffi->snprintf($buf, 64, "%d %d %d %d %d %d %d %d", true, false, true, false, true, false, true, false);
var_dump(FFI::string($buf));

Resulted in this output (the 7th value changes between runs):

string(36) "1 0 1 -256 -255 -256 1449856257 -256"

But I expected this output instead:

string(15) "1 0 1 0 1 0 1 0"

Passing bools as ffi_type_sint32 (int32_t), like C's promotion, fixes it.

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), libffi 3.3

Activity

  1. devnexen commented on Nov 19, 2026

    @devnexen
    Member

    Hi @Marti2203 your fix looks right, feel free to open a PR.

  2. added a commit that references this issue on Nov 19, 2026
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