Commit c39caa515a9 for php

commit c39caa515a981317b248e1164b62f08325fac033
Author: Marc Bennewitz <marc@mabe.berlin>
Date:   Wed Nov 25 07:31:47 2026 +0100

    Fix FFI::string() reading out of bounds for a negative size

    FFI::string() passed a negative size on to the string allocation, where
    it wrapped around to a small allocation that was then filled with
    nearly SIZE_MAX bytes read from the pointer. For arrays, a negative size was
    rejected by the comparison with the array size only because that
    comparison happens to be unsigned.

    Reject a negative size with a ValueError, for pointers and arrays alike.

    Closes GH-25731.

diff --git a/NEWS b/NEWS
index 1a77a3e71e3..2f2d92d086f 100644
--- a/NEWS
+++ b/NEWS
@@ -71,6 +71,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)

 - 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 06f564c2e7e..9977008f83d 100644
--- a/ext/ffi/ffi.c
+++ b/ext/ffi/ffi.c
@@ -4624,6 +4624,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 {
diff --git a/ext/ffi/tests/string_negative_size.phpt b/ext/ffi/tests/string_negative_size.phpt
new file mode 100644
index 00000000000..6aaa1195143
--- /dev/null
+++ b/ext/ffi/tests/string_negative_size.phpt
@@ -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