php / php/php-src

char CData can't be directly assigned a number

Open
#8,355 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Description

The following code:

<?php

$v = FFI::new("char[2]");
$v[0] = 65;
var_dump($v);

Resulted in this output:

Fatal error: Uncaught FFI\Exception: Incompatible types when assigning to type 'char' from PHP 'int' in Command line code:1
Stack trace:
#0 {main}
  thrown in Command line code on line 1

But I expected this output instead:

object(FFI\CData:char[2])#1 (2) {
  [0]=>
  string(1) "A"
  [1]=>
  string(1) ""
}

It's somewhat weird to have to type cast around, i.e. I could just do a FFI::cast to uint8_t * and it works. But for some reason, char is special. Seems like an arbitrary restriction to me, and it really should accept both.

PHP Version

master

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

Reproduce the PHP FFI example from the issue and trace the handling of assignments to a char CData value. Locate the relevant FFI implementation and existing FFI tests, then add coverage for assigning an integer to char and verify that the expected character value is produced.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.