[php-src] Issue #7949: FFI struct with flexible array member
| From: | userqq | Date: | Mon, 17 Jan 2022 00:20:19 +0000 |
| Subject: | [php-src] Issue #7949: FFI struct with flexible array member | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-239041@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/7949
Comment Author: userqq
> but the programmer is responsible to allocate a proper struct
I finally got it. Such struct could be allocated in foreign function, like:
```php
$lib = FFI::cdef("
typedef struct Data {
int32_t size;
char data[];
} Data;
Data createData();
", 'lib.so');
```
> FFI::new() can't do that
> The fact that FFI::memcpy() does not work, looks like a bug to me.
So it still could be possible allocate such structs in PHP?
In some cases passing PHP string to FFI function (and receiving it as C string) will not work
properly. If there is a possibility string contains \0 it will be truncated at first NULL-byte, so
to handle such cases length should be passed explicitly.
It would be very handy to have possibility use structs with flexible array member instead of
additional length argument in foreign function.
```php
$lib = FFI::cdef("
typedef struct Data {
int32_t size;
char data[];
} Data;
void handleData(Data data);
", 'lib.so');
```