php / php/php-src

FFI::cast() from pointer returns new value instead of reference.

未关闭
#7,904 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Bug Extension: ffi Status: Verified
主要语言
C
星标
40.4k
派生
8.2k
平均合并
2 天 13 小时
30 天内合并 PR
96

描述

Description

Hi!
I'm also not sure it's a bug, but documentation says: FFI::cast() creates a new FFI\CData object, that references the same C data structure, but is associated with a different type.

The following code:

<?php

$val = FFI::new('char[8]');
FFI::memcpy($val, implode('', range('a', 'h')), 8);

var_dump($val);

$firstInt = FFI::cast('int32_t', $val);
var_dump($firstInt);
$firstInt->cdata = 0;
var_dump($val);

$secondInt = FFI::cast('int32_t', $val + 4);
var_dump($secondInt);
$secondInt->cdata = 0;
var_dump($val);

Resulted in this output:

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)
}
object(FFI\CData:char[8])#1 (8) {
  [0]=>
  string(1) ""
  [1]=>
  string(1) ""
  [2]=>
  string(1) ""
  [3]=>
  string(1) ""
  [4]=>
  string(1) "e"
  [5]=>
  string(1) "f"
  [6]=>
  string(1) "g"
  [7]=>
  string(1) "h"
}
object(FFI\CData:int32_t)#4 (1) {
  ["cdata"]=>
  int(721424396)
}
object(FFI\CData:char[8])#1 (8) {
  [0]=>
  string(1) ""
  [1]=>
  string(1) ""
  [2]=>
  string(1) ""
  [3]=>
  string(1) ""
  [4]=>
  string(1) "e"
  [5]=>
  string(1) "f"
  [6]=>
  string(1) "g"
  [7]=>
  string(1) "h"
}

But I expected this output instead:

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)
}
object(FFI\CData:char[8])#1 (8) {
  [0]=>
  string(1) ""
  [1]=>
  string(1) ""
  [2]=>
  string(1) ""
  [3]=>
  string(1) ""
  [4]=>
  string(1) "e"
  [5]=>
  string(1) "f"
  [6]=>
  string(1) "g"
  [7]=>
  string(1) "h"
}
object(FFI\CData:int32_t)#4 (1) {
  ["cdata"]=>
  int(721424396)
}
object(FFI\CData:char[8])#1 (8) {
  [0]=>
  string(1) ""
  [1]=>
  string(1) ""
  [2]=>
  string(1) ""
  [3]=>
  string(1) ""
  [4]=>
  string(1) ""
  [5]=>
  string(1) ""
  [6]=>
  string(1) ""
  [7]=>
  string(1) ""
}
PHP Version

PHP 8.1.1

Operating System

No response

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先运行提供的 PHP 8.1.1 FFI::cast() 复现代码,并比较从 $val 进行的指针转换与从 $val + 4 进行的转换。然后检查 FFI::cast() 的入口点以及相关测试(如果存在)。当文档中记录的共享引用行为对于两种转换都保持一致,或文档明确反映已验证的行为时,即视为完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
c, php
领域
backend
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。