Commit c7265742d06 for php

commit c7265742d06e7dfffcb082754988a6dc43b29cf7
Author: Tomasz Kowalczyk <tomasz@kowalczyk.cc>
Date:   Thu Nov 26 00:43:35 2026 +0100

    ext/pgsql: fix pg_fetch_object() JIT crash when constructor keeps $this

    pg_fetch_object() calls the constructor on the returned object, and the
    constructor can keep a reference to $this. The stub still declared
    @refcount 1, which lets function JIT and tracing JIT destroy the object when
    the result is discarded while that reference still uses it. Drop the
    annotation.

    Fix GH-25772
    Close GH-25773

diff --git a/NEWS b/NEWS
index f4beb1dc6d0..495a49d6aa1 100644
--- a/NEWS
+++ b/NEWS
@@ -167,6 +167,10 @@ PHP                                                                        NEWS
   . Fixed bug GH-25588 (Use-after-free of a string parameter omitted on a later
     execute() or rebound while fetching). (Ilia Alshanetsky)

+- PGSQL:
+  . Fixed bug GH-25772 (pg_fetch_object() crashes with JIT when the constructor
+    keeps a reference and the result is discarded). (Tomasz Kowalczyk)
+
 - Phar:
   . Fixed bug GH-25528 (Phar::setSignatureAlgorithm(): OpenSSL signing reads a
     freed private key). (Artem Ukrainskiy)
diff --git a/Zend/Optimizer/zend_func_infos.h b/Zend/Optimizer/zend_func_infos.h
index 6a484490b34..faff2f43b5c 100644
--- a/Zend/Optimizer/zend_func_infos.h
+++ b/Zend/Optimizer/zend_func_infos.h
@@ -317,7 +317,6 @@ static const func_info_t func_infos[] = {
 	F1("pg_fetch_row", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_STRING|MAY_BE_ARRAY_OF_NULL|MAY_BE_FALSE),
 	F1("pg_fetch_assoc", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_STRING|MAY_BE_ARRAY_OF_NULL|MAY_BE_FALSE),
 	F1("pg_fetch_array", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_STRING|MAY_BE_ARRAY_OF_NULL|MAY_BE_FALSE),
-	F1("pg_fetch_object", MAY_BE_OBJECT|MAY_BE_FALSE),
 	F1("pg_fetch_all", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_ARRAY),
 	F1("pg_fetch_all_columns", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING|MAY_BE_ARRAY_OF_NULL),
 	F1("pg_last_oid", MAY_BE_STRING|MAY_BE_LONG|MAY_BE_FALSE),
diff --git a/ext/pgsql/pgsql.stub.php b/ext/pgsql/pgsql.stub.php
index 2f51bdfca6b..31774ed56fe 100644
--- a/ext/pgsql/pgsql.stub.php
+++ b/ext/pgsql/pgsql.stub.php
@@ -643,7 +643,6 @@ function pg_fetch_assoc(PgSql\Result $result, ?int $row = null): array|false {}
      */
     function pg_fetch_array(PgSql\Result $result, ?int $row = null, int $mode = PGSQL_BOTH): array|false {}

-    /** @refcount 1 */
     function pg_fetch_object(PgSql\Result $result, ?int $row = null, string $class = "stdClass", array $constructor_args = []): object|false {}

     /**
diff --git a/ext/pgsql/pgsql_arginfo.h b/ext/pgsql/pgsql_arginfo.h
index 9d8f6369000..3839f550edb 100644
Binary files a/ext/pgsql/pgsql_arginfo.h and b/ext/pgsql/pgsql_arginfo.h differ
diff --git a/ext/pgsql/tests/gh25772.phpt b/ext/pgsql/tests/gh25772.phpt
new file mode 100644
index 00000000000..0d3008c2b47
--- /dev/null
+++ b/ext/pgsql/tests/gh25772.phpt
@@ -0,0 +1,41 @@
+--TEST--
+GH-25772: pg_fetch_object() crashes with JIT when the constructor keeps a reference and the result is discarded
+--EXTENSIONS--
+pgsql
+opcache
+--SKIPIF--
+<?php include("inc/skipif.inc"); ?>
+--INI--
+opcache.enable=1
+opcache.enable_cli=1
+opcache.jit=function
+opcache.jit_buffer_size=64M
+opcache.file_update_protection=0
+--FILE--
+<?php
+include("inc/config.inc");
+
+class Row {
+    public static array $rows = [];
+
+    public function __construct() {
+        self::$rows[] = $this;
+    }
+}
+
+function test(PgSql\Result $result) {
+    pg_fetch_object($result, 0, Row::class);
+}
+
+$conn = pg_connect($conn_str);
+test(pg_query($conn, "SELECT 1 AS id"));
+var_dump(Row::$rows);
+?>
+--EXPECTF--
+array(1) {
+  [0]=>
+  object(Row)#%d (1) {
+    ["id"]=>
+    string(1) "1"
+  }
+}