Commit fac0c135e21 for php
commit fac0c135e21209f081740dddb27b67c66587880a
Author: Marc Bennewitz <marc@mabe.berlin>
Date: Fri Nov 27 07:52:31 2026 +0100
Fix FFI::memcpy(), FFI::memcmp() and FFI::memset() accepting a negative size (#25827)
Reject negative sizes with a ValueError, as FFI::string() already does
since GH-25731. This makes size validation consistent for CData and
strings and prevents invalid sizes from reaching the native calls.
Consolidate regression coverage for all four functions in mem_size.phpt.
Closes #25827
diff --git a/NEWS b/NEWS
index 1b762859600..02871232143 100644
--- a/NEWS
+++ b/NEWS
@@ -83,8 +83,8 @@ PHP NEWS
- FFI:
. Fixed bug GH-25558 (struct with an enum field passed by value is described
to libffi with a void element). (Davide Peressoni)
- . Fixed FFI::string() reading out of bounds for a negative size.
- (Marc Bennewitz)
+ . Fixed FFI::string(), FFI::memcpy(), FFI::memcmp() and FFI::memset()
+ reading or writing out of bounds for a negative size. (Marc Bennewitz)
- Filter:
. Fixed stack overflow in filter_var() and filter_var_array() with deeply
diff --git a/ext/ffi/ffi.c b/ext/ffi/ffi.c
index 9977008f83d..6873c7cc46d 100644
--- a/ext/ffi/ffi.c
+++ b/ext/ffi/ffi.c
@@ -4459,6 +4459,11 @@ ZEND_METHOD(FFI, memcpy) /* {{{ */
Z_PARAM_LONG(size)
ZEND_PARSE_PARAMETERS_END();
+ if (UNEXPECTED(size < 0)) {
+ zend_argument_value_error(3, "must be greater than or equal to 0");
+ RETURN_THROWS();
+ }
+
cdata1 = (zend_ffi_cdata*)Z_OBJ_P(zv1);
type1 = ZEND_FFI_TYPE(cdata1->type);
if (type1->kind == ZEND_FFI_TYPE_POINTER) {
@@ -4515,6 +4520,11 @@ ZEND_METHOD(FFI, memcmp) /* {{{ */
Z_PARAM_LONG(size)
ZEND_PARSE_PARAMETERS_END();
+ if (UNEXPECTED(size < 0)) {
+ zend_argument_value_error(3, "must be greater than or equal to 0");
+ RETURN_THROWS();
+ }
+
ZVAL_DEREF(zv1);
if (Z_TYPE_P(zv1) == IS_STRING) {
ptr1 = Z_STRVAL_P(zv1);
@@ -4589,6 +4599,11 @@ ZEND_METHOD(FFI, memset) /* {{{ */
Z_PARAM_LONG(size)
ZEND_PARSE_PARAMETERS_END();
+ if (UNEXPECTED(size < 0)) {
+ zend_argument_value_error(3, "must be greater than or equal to 0");
+ RETURN_THROWS();
+ }
+
cdata = (zend_ffi_cdata*)Z_OBJ_P(zv);
type = ZEND_FFI_TYPE(cdata->type);
if (type->kind == ZEND_FFI_TYPE_POINTER) {
@@ -4624,7 +4639,7 @@ 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) {
+ if (UNEXPECTED(size < 0)) {
zend_argument_value_error(2, "must be greater than or equal to 0");
RETURN_THROWS();
}
diff --git a/ext/ffi/tests/mem_size.phpt b/ext/ffi/tests/mem_size.phpt
new file mode 100644
index 00000000000..bf8ddab7d47
--- /dev/null
+++ b/ext/ffi/tests/mem_size.phpt
@@ -0,0 +1,90 @@
+--TEST--
+FFI::memcpy(), FFI::memcmp(), FFI::memset() and FFI::string() reject invalid sizes
+--EXTENSIONS--
+ffi
+--INI--
+ffi.enable=1
+--FILE--
+<?php
+$ffi = FFI::cdef();
+$array = $ffi->new("char[4]");
+$small = $ffi->new("char[2]");
+$pointer = FFI::addr($array[0]);
+
+/* Nothing limits the size when passing $pointer alone, so it only gets sizes not beyond $array */
+$pointerSizes = ['0' => 0, '2' => 2, '3' => 3, '-1' => -1, 'PHP_INT_MIN' => PHP_INT_MIN];
+$allSizes = $pointerSizes + ['PHP_INT_MAX' => PHP_INT_MAX];
+$cases = [
+ 'FFI::memcpy($small, "abc", %s)' => [fn($size) => FFI::memcpy($small, "abc", $size), $allSizes],
+ 'FFI::memcpy($pointer, $small, %s)' => [fn($size) => FFI::memcpy($pointer, $small, $size), $allSizes],
+ 'FFI::memcmp($small, $array, %s)' => [fn($size) => FFI::memcmp($small, $array, $size), $allSizes],
+ 'FFI::memcmp($pointer, $small, %s)' => [fn($size) => FFI::memcmp($pointer, $small, $size), $allSizes],
+ 'FFI::memcmp($pointer, $pointer, %s)' => [fn($size) => FFI::memcmp($pointer, $pointer, $size), $pointerSizes],
+ 'FFI::memset($small, ord("x"), %s)' => [fn($size) => FFI::memset($small, ord("x"), $size), $allSizes],
+ 'FFI::memset($pointer, ord("x"), %s)' => [fn($size) => FFI::memset($pointer, ord("x"), $size), $pointerSizes],
+ 'FFI::string($small, %s)' => [fn($size) => FFI::string($small, $size), $allSizes],
+ 'FFI::string($pointer, %s)' => [fn($size) => FFI::string($pointer, $size), $pointerSizes],
+];
+foreach ($cases as $label => [$case, $caseSizes]) {
+ foreach ($caseSizes as $sizeLabel => $size) {
+ try {
+ $result = var_export($case($size), true);
+ } catch (Throwable $e) {
+ $result = $e::class . ": " . $e->getMessage();
+ }
+ echo sprintf($label, $sizeLabel), ": ", $result, "\n";
+ }
+}
+?>
+--EXPECT--
+FFI::memcpy($small, "abc", 0): NULL
+FFI::memcpy($small, "abc", 2): NULL
+FFI::memcpy($small, "abc", 3): FFI\Exception: Attempt to write over data boundary
+FFI::memcpy($small, "abc", -1): ValueError: FFI::memcpy(): Argument #3 ($size) must be greater than or equal to 0
+FFI::memcpy($small, "abc", PHP_INT_MIN): ValueError: FFI::memcpy(): Argument #3 ($size) must be greater than or equal to 0
+FFI::memcpy($small, "abc", PHP_INT_MAX): FFI\Exception: Attempt to write over data boundary
+FFI::memcpy($pointer, $small, 0): NULL
+FFI::memcpy($pointer, $small, 2): NULL
+FFI::memcpy($pointer, $small, 3): FFI\Exception: Attempt to read over data boundary
+FFI::memcpy($pointer, $small, -1): ValueError: FFI::memcpy(): Argument #3 ($size) must be greater than or equal to 0
+FFI::memcpy($pointer, $small, PHP_INT_MIN): ValueError: FFI::memcpy(): Argument #3 ($size) must be greater than or equal to 0
+FFI::memcpy($pointer, $small, PHP_INT_MAX): FFI\Exception: Attempt to read over data boundary
+FFI::memcmp($small, $array, 0): 0
+FFI::memcmp($small, $array, 2): 0
+FFI::memcmp($small, $array, 3): FFI\Exception: attempt to read over data boundary
+FFI::memcmp($small, $array, -1): ValueError: FFI::memcmp(): Argument #3 ($size) must be greater than or equal to 0
+FFI::memcmp($small, $array, PHP_INT_MIN): ValueError: FFI::memcmp(): Argument #3 ($size) must be greater than or equal to 0
+FFI::memcmp($small, $array, PHP_INT_MAX): FFI\Exception: attempt to read over data boundary
+FFI::memcmp($pointer, $small, 0): 0
+FFI::memcmp($pointer, $small, 2): 0
+FFI::memcmp($pointer, $small, 3): FFI\Exception: Attempt to read over data boundary
+FFI::memcmp($pointer, $small, -1): ValueError: FFI::memcmp(): Argument #3 ($size) must be greater than or equal to 0
+FFI::memcmp($pointer, $small, PHP_INT_MIN): ValueError: FFI::memcmp(): Argument #3 ($size) must be greater than or equal to 0
+FFI::memcmp($pointer, $small, PHP_INT_MAX): FFI\Exception: Attempt to read over data boundary
+FFI::memcmp($pointer, $pointer, 0): 0
+FFI::memcmp($pointer, $pointer, 2): 0
+FFI::memcmp($pointer, $pointer, 3): 0
+FFI::memcmp($pointer, $pointer, -1): ValueError: FFI::memcmp(): Argument #3 ($size) must be greater than or equal to 0
+FFI::memcmp($pointer, $pointer, PHP_INT_MIN): ValueError: FFI::memcmp(): Argument #3 ($size) must be greater than or equal to 0
+FFI::memset($small, ord("x"), 0): NULL
+FFI::memset($small, ord("x"), 2): NULL
+FFI::memset($small, ord("x"), 3): FFI\Exception: attempt to write over data boundary
+FFI::memset($small, ord("x"), -1): ValueError: FFI::memset(): Argument #3 ($size) must be greater than or equal to 0
+FFI::memset($small, ord("x"), PHP_INT_MIN): ValueError: FFI::memset(): Argument #3 ($size) must be greater than or equal to 0
+FFI::memset($small, ord("x"), PHP_INT_MAX): FFI\Exception: attempt to write over data boundary
+FFI::memset($pointer, ord("x"), 0): NULL
+FFI::memset($pointer, ord("x"), 2): NULL
+FFI::memset($pointer, ord("x"), 3): NULL
+FFI::memset($pointer, ord("x"), -1): ValueError: FFI::memset(): Argument #3 ($size) must be greater than or equal to 0
+FFI::memset($pointer, ord("x"), PHP_INT_MIN): ValueError: FFI::memset(): Argument #3 ($size) must be greater than or equal to 0
+FFI::string($small, 0): ''
+FFI::string($small, 2): 'xx'
+FFI::string($small, 3): FFI\Exception: attempt to read over data boundary
+FFI::string($small, -1): ValueError: FFI::string(): Argument #2 ($size) must be greater than or equal to 0
+FFI::string($small, PHP_INT_MIN): ValueError: FFI::string(): Argument #2 ($size) must be greater than or equal to 0
+FFI::string($small, PHP_INT_MAX): FFI\Exception: attempt to read over data boundary
+FFI::string($pointer, 0): ''
+FFI::string($pointer, 2): 'xx'
+FFI::string($pointer, 3): 'xxx'
+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
diff --git a/ext/ffi/tests/string_negative_size.phpt b/ext/ffi/tests/string_negative_size.phpt
deleted file mode 100644
index 6aaa1195143..00000000000
--- a/ext/ffi/tests/string_negative_size.phpt
+++ /dev/null
@@ -1,33 +0,0 @@
---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