php / php/php-src

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

Open
#7,904 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Extension: ffi Status: Verified
Dominant language
C
Stars
40.4k
Forks
8.1k
Avg merge
2d 13h
Merged PRs (30d)
96

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided PHP 8.1.1 FFI::cast() reproduction and compare pointer casts from $val with casts from $val + 4. Then inspect the FFI::cast() entry point and related tests, if present. Done means the documented shared-reference behavior is consistent for both casts, or the documentation clearly reflects the verified behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, php
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.