[php-src] Issue #7867: FFI::cast vs FFI::memcpy

From: Date: Sat, 01 Jan 2022 21:26:49 +0000
Subject: [php-src] Issue #7867: FFI::cast vs FFI::memcpy
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-238704@lists.php.net to get a copy of this message
Issue: https://github.com/php/php-src/issues/7867 Author: userqq ### Description Hi! Not sure its incorrect behaviour, but as you can see FFI::cast when applied to FFI\CData with offset returns data different from copy result. The following code: ```php $value = FFI::new('char[26]'); FFI::memcpy($value, implode('', range('a', 'z')), 26); $slice = FFI::new('char[4]'); echo 'cast from start' . PHP_EOL; FFI::memcpy($slice, $value, 4); var_dump($value + 0, $slice, FFI::cast('char[4]', $value)); echo PHP_EOL; echo 'cast with offset' . PHP_EOL; FFI::memcpy($slice, $value + 4, 4); var_dump($value + 4, $slice, FFI::cast('char[4]', $value + 4)); echo PHP_EOL; ``` Resulted in this output: ``` cast from start object(FFI\CData:char*)#3 (1) { [0]=> string(1) "a" } object(FFI\CData:char[4])#2 (4) { [0]=> string(1) "a" [1]=> string(1) "b" [2]=> string(1) "c" [3]=> string(1) "d" } object(FFI\CData:char[4])#4 (4) { [0]=> string(1) "a" [1]=> string(1) "b" [2]=> string(1) "c" [3]=> string(1) "d" } cast with offset object(FFI\CData:char*)#4 (1) { [0]=> string(1) "e" } object(FFI\CData:char[4])#2 (4) { [0]=> string(1) "e" [1]=> string(1) "f" [2]=> string(1) "g" [3]=> string(1) "h" } object(FFI\CData:char[4])#5 (4) { [0]=> string(1) "▒" [1]=> string(1) "C" [2]=> string(1) "▒" [3]=> string(1) "x" } ``` But I expected this output instead: ``` cast from start object(FFI\CData:char*)#3 (1) { [0]=> string(1) "a" } object(FFI\CData:char[4])#2 (4) { [0]=> string(1) "a" [1]=> string(1) "b" [2]=> string(1) "c" [3]=> string(1) "d" } object(FFI\CData:char[4])#4 (4) { [0]=> string(1) "a" [1]=> string(1) "b" [2]=> string(1) "c" [3]=> string(1) "d" } cast with offset object(FFI\CData:char*)#4 (1) { [0]=> string(1) "e" } object(FFI\CData:char[4])#2 (4) { [0]=> string(1) "e" [1]=> string(1) "f" [2]=> string(1) "g" [3]=> string(1) "h" } object(FFI\CData:char[4])#5 (4) { [0]=> string(1) "e" [1]=> string(1) "f" [2]=> string(1) "g" [3]=> string(1) "h" } ``` ### PHP Version PHP 8.1.1 ### Operating System _No response_

« previous php.bugs (#238704) next »