[php-src] Issue #8007: FFI::cast() from pointer returns incorrect value
| From: | damianwadley | Date: | Sat, 29 Jan 2022 02:47:12 +0000 |
| Subject: | [php-src] Issue #8007: FFI::cast() from pointer returns incorrect value | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-239341@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/8007
Comment Author: damianwadley
Oops, sent that a bit early.
```php
$val = FFI::new('char[8]');
FFI::memcpy($val, 'abcdefgh', 8);
var_dump($val);
$first = FFI::cast('int32_t', $val);
var_dump($first);
var_dump(unpack('l', 'abcdefgh'));
$second = FFI::cast('int32_t*', $val + 4);
var_dump($second[0]);
var_dump(unpack('l', 'efgh'));
```
```
object(FFI\CData:char[8])#1 (8) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
string(1) "c"
[3]=>
string(1) "d"
[4]=>
string(1) "e"
[5]=>
string(1) "f"
[6]=>
string(1) "g"
[7]=>
string(1) "h"
}
object(FFI\CData:int32_t)#2 (1) {
["cdata"]=>
int(1684234849)
}
array(1) {
[1]=>
int(1684234849)
}
int(1751606885)
array(1) {
[1]=>
int(1751606885)
}
```
Neither of the issues seem to be bugs to me, however the treatment of an array (automatically
dereferenced) versus a pointer (not dereferenced) is surprising.