Skip to content

Commit b42a137

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

File tree

2 files changed

+13
-17
lines changed

2 files changed

+13
-17
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 & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -191,25 +191,13 @@ public function testCastCuttedPointerToChar()
191191
PHP, $pointer);
192192
}
193193

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-
*/
205194
public function testCastNonTrailingCharPointer()
206195
{
207196
$actualMessage = 'Hello World!';
208197
$actualLength = \strlen($actualMessage);
209198

210-
$string = \FFI::cdef()->new('char['.$actualLength.']');
199+
$string = \FFI::cdef()->new('char['.($actualLength+1).']');
211200
$pointer = \FFI::addr($string[0]);
212-
213201
\FFI::memcpy($pointer, $actualMessage, $actualLength);
214202

215203
// Remove automatically addition of the trailing "\0" and remove trailing "\0"
@@ -218,7 +206,7 @@ public function testCastNonTrailingCharPointer()
218206

219207
$this->assertDumpMatchesFormat(<<<PHP
220208
FFI\CData<char*> size 8 align 8 {
221-
cdata: "$actualMessage%s"
209+
cdata: %A"$actualMessage%s"
222210
}
223211
PHP, $pointer);
224212
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy