xref: /PHP-8.2/ext/ffi/tests/bug77706.phpt (revision bd9f4fa6)
1--TEST--
2Bug #77632 (FFI Segfaults When Called With Variadics)
3--EXTENSIONS--
4ffi
5--SKIPIF--
6<?php
7try {
8    $libc = FFI::cdef("int printf(const char *format, ...);", "libc.so.6");
9} catch (Throwable $_) {
10    die('skip libc.so.6 not available');
11}
12?>
13--INI--
14ffi.enable=1
15--FILE--
16<?php
17$header = '
18typedef struct _IO_FILE FILE;
19extern FILE *stdout;
20extern FILE *stdin;
21extern FILE *stderr;
22
23typedef uint64_t time_t;
24typedef uint32_t pid_t;
25
26time_t time(time_t*);
27pid_t getpid(void);
28int fprintf(FILE *, const char *, ...);
29';
30
31$ffi = FFI::cdef($header, 'libc.so.6');
32
33try {
34    $ffi->time();
35} catch (Throwable $e) {
36    echo get_class($e) . ": " . $e->getMessage() . "\n";
37}
38
39try {
40    $ffi->time(null, null);
41} catch (Throwable $e) {
42    echo get_class($e) . ": " . $e->getMessage() . "\n";
43}
44
45try {
46    $ffi->fprintf($ffi->stdout);
47} catch (Throwable $e) {
48    echo get_class($e) . ": " . $e->getMessage() . "\n";
49}
50
51try {
52    $ffi->fprintf($ffi->stdout, 123, "Hello %s\n", "World");
53} catch (Throwable $e) {
54    echo get_class($e) . ": " . $e->getMessage() . "\n";
55}
56?>
57--EXPECT--
58FFI\Exception: Incorrect number of arguments for C function 'time', expecting exactly 1 parameter
59FFI\Exception: Incorrect number of arguments for C function 'time', expecting exactly 1 parameter
60FFI\Exception: Incorrect number of arguments for C function 'fprintf', expecting at least 2 parameters
61FFI\Exception: Passing incompatible argument 2 of C function 'fprintf', expecting 'char*', found PHP 'int'
62