Skip to content
Advertisement

php-ffi: Return type const char* is a string

Why is the result of const char * a string and char * an object „byte-array“?

A simple example:

JavaScript

returns

JavaScript

Removing const from the definition returns:

JavaScript

Background

A function of a dynamically linked external C library returns binary data which include NULL-bytes. When const is in the definition of the header file the resulting string is not complete = shortened to the first NULL-byte = corrupt file.

My workaround

Remove const from the definition and

JavaScript

returns the correct complete string.

(In the real-life scenario the $size for FFI::string($array, $size) is known and returned back from a different function of the C library.)

Advertisement

Answer

Dropping const or adding unsigned in the definition seem to be reasonable workarounds (cf. https://github.com/dstogov/php-ffi/issues/42#issuecomment-791237784)

Examples

Dropping const

JavaScript

Adding unsigned (or changing to uint8_t)

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement