8000 [VarDumper] Fix FFI caster test · symfony/symfony@bf76480 · GitHub
[go: up one dir, main page]

Skip to content

Commit bf76480

Browse files
[VarDumper] Fix FFI caster test
1 parent 7b53770 commit bf76480

File tree

2 files changed

+13
-18
lines changed
  • src/Symfony/Component/VarDumper
    • Caster
  • Tests/Caster
  • 2 files changed

    +13
    -18
    lines changed

    src/Symfony/Component/VarDumper/Caster/FFICaster.php

    Lines changed: 11 additions & 3 deletions
    Original file line numberDiff line numberDiff line change
    @@ -115,12 +115,20 @@ private static function castFFIPointer(Stub $stub, CType $type, ?CData $data = n
    115115
    private static function castFFIStringValue(CData $data): string|CutStub
    116116
    {
    117117
    $result = [];
    118+
    $ffi = \FFI::cdef(<<<C
    119+
    bool is_zend_mm(void);
    120+
    bool is_zend_ptr(const void *ptr);
    121+
    C);
    118122

    119123
    for ($i = 0; $i < self::MAX_STRING_LENGTH; ++$i) {
    120-
    $result[$i] = $data[$i];
    124+
    if ($ffi->is_zend_ptr($data) && $ffi->is_zend_mm()) {
    125+
    $result[$i] = $data[$i];
    121126

    122-
    if ("\0" === $result[$i]) {
    123-
    return implode('', $result);
    127+
    if ("\0" === $data[$i]) {
    128+
    return implode('', $result);
    129+
    }
    130+
    } else {
    131+
    break;
    124132
    }
    125133
    }
    126134

    src/Symfony/Component/VarDumper/Tests/Caster/FFICasterTest.php

    Lines changed: 2 additions & 15 deletions
    Original file line numberDiff line numberDiff line change
    @@ -12,7 +12,6 @@
    1212
    namespace Symfony\Component\VarDumper\Tests\Caster;
    1313

    1414
    use PHPUnit\Framework\TestCase;
    15-
    use Symfony\Component\VarDumper\Caster\FFICaster;
    1615
    use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
    1716

    1817
    /**
    @@ -191,25 +190,13 @@ public function testCastCuttedPointerToChar()
    191190
    PHP, $pointer);
    192191
    }
    193192

    194-
    /**
    195-
    * It is worth noting that such a test can cause SIGSEGV, as it breaks
    196-
    * into "foreign" memory. However, this is only theoretical, since
    197-
    * memory is allocated within the PHP process and almost always "garbage
    198-
    * data" will be read from the PHP process itself.
    199-
    *
    200-
    * If this test fails for some reason, please report it: We may have to
    201-
    * disable the dumping of strings ("char*") feature in VarDumper.
    202-
    *
    203-
    * @see FFICaster::castFFIStringValue()
    204-
    */
    205193
    public function testCastNonTrailingCharPointer()
    206194
    {
    207195
    $actualMessage = 'Hello World!';
    208196
    $actualLength = \strlen($actualMessage);
    209197

    210-
    $string = \FFI::cdef()->new('char['.$actualLength.']');
    198+
    $string = \FFI::cdef()->new('char['.($actualLength + 1).']');
    211199
    $pointer = \FFI::addr($string[0]);
    212-
    213200
    \FFI::memcpy($pointer, $actualMessage, $actualLength);
    214201

    215202
    // Remove automatically addition of the trailing "\0" and remove trailing "\0"
    @@ -218,7 +205,7 @@ public function testCastNonTrailingCharPointer()
    218205

    219206
    $this->assertDumpMatchesFormat(<<<PHP
    220207
    FFI\CData<char*> size 8 align 8 {
    221-
    cdata: "$actualMessage%s"
    208+
    cdata: %A"$actualMessage%s"
    222209
    }
    223210
    PHP, $pointer);
    224211
    }

    0 commit comments

    Comments
     (0)
    0