Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ PHP NEWS
. Fixed use-after-free in getElementById() after a removed ID attribute is
freed. (Ilia Alshanetsky)

- FFI:
. Fixed FFI::string() reading out of bounds for a negative size.
(Marc Bennewitz)

- Filter:
. Fixed stack overflow in filter_var() and filter_var_array() with deeply
nested arrays. (Eyüp Can Akman)
Expand Down
4 changes: 4 additions & 0 deletions ext/ffi/ffi.c
Original file line number Diff line number Diff line change
Expand Up @@ -4620,6 +4620,10 @@ ZEND_METHOD(FFI, string) /* {{{ */
cdata = (zend_ffi_cdata*)Z_OBJ_P(zv);
type = ZEND_FFI_TYPE(cdata->type);
if (!size_is_null) {
if (size < 0) {
zend_argument_value_error(2, "must be greater than or equal to 0");
RETURN_THROWS();
}
if (type->kind == ZEND_FFI_TYPE_POINTER) {
ptr = *(void**)cdata->ptr;
} else {
Expand Down
33 changes: 33 additions & 0 deletions ext/ffi/tests/string_negative_size.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--TEST--
FFI::string() rejects a negative size
--EXTENSIONS--
ffi
--INI--
ffi.enable=1
--FILE--
<?php
$ffi = FFI::cdef();
$array = $ffi->new("char[4]");
FFI::memcpy($array, "abcd", 4);
$pointer = FFI::addr($array[0]);

foreach (['$array' => $array, '$pointer' => $pointer] as $label => $cdata) {
foreach (['3' => 3, '0' => 0, '-1' => -1, 'PHP_INT_MIN' => PHP_INT_MIN] as $sizeLabel => $size) {
try {
$result = var_export(FFI::string($cdata, $size), true);
echo "FFI::string($label, $sizeLabel): $result\n";
} catch (Throwable $e) {
echo "FFI::string($label, $sizeLabel): ", $e::class, ": ", $e->getMessage(), "\n";
}
}
}
?>
--EXPECT--
FFI::string($array, 3): 'abc'
FFI::string($array, 0): ''
FFI::string($array, -1): ValueError: FFI::string(): Argument #2 ($size) must be greater than or equal to 0
FFI::string($array, PHP_INT_MIN): ValueError: FFI::string(): Argument #2 ($size) must be greater than or equal to 0
FFI::string($pointer, 3): 'abc'
FFI::string($pointer, 0): ''
FFI::string($pointer, -1): ValueError: FFI::string(): Argument #2 ($size) must be greater than or equal to 0
FFI::string($pointer, PHP_INT_MIN): ValueError: FFI::string(): Argument #2 ($size) must be greater than or equal to 0
Loading