PDO::query(): FETCH_CLASS constructor arguments error reports the wrong argument numbers

未關閉 適合新手
#25,650 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

還沒有人認領這個 Issue。

評估

難度
2/5
預估耗時
1-3 小時
新手友好度
72/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
c
領域
database

研究方向

錯誤的數字來自 ext/pdo/pdo_stmt.c 中 pdo_stmt_setup_fetch_mode() 的建構函式檢查,它硬編碼了 3 和 #2,但該函式其實已經計算了 constructor_arg_num 和 arg1_arg_num。請將這項檢查與同一函式中的其他錯誤比較,然後閱讀 pdo_stmt_class_ctor_errors.phpt,了解既有案例是如何測試的。當 PDO::query() 案例回報參數 #4 和 #3,且 setFetchMode() 的輸出保持不變時,即完成。

由索引模型根據 Issue 內容生成。

描述

Description

PDO::query() reports the wrong argument numbers when PDO::FETCH_CLASS gets constructor arguments for a class without a constructor.

The following code:

<?php
class NoCtor {}

$db = new PDO('sqlite::memory:');

try {
    $db->query('SELECT 1', PDO::FETCH_CLASS, 'NoCtor', [1]);
} catch (Throwable $e) {
    echo $e::class, ': ', $e->getMessage(), "\n";
}

try {
    $db->query('SELECT 1', PDO::FETCH_CLASS, 'NotAClass');
} catch (Throwable $e) {
    echo $e::class, ': ', $e->getMessage(), "\n";
}

Resulted in this output:

ValueError: PDO::query(): Argument #3 must be empty when class provided in argument #2 ($class) does not have a constructor
TypeError: PDO::query(): Argument #3 must be a valid class

But I expected this output instead:

ValueError: PDO::query(): Argument #4 must be empty when class provided in argument #3 ($class) does not have a constructor
TypeError: PDO::query(): Argument #3 must be a valid class

In PDO::query() the class is argument #3 (the second error says so) and the constructor arguments are argument #4.

PDO::query() and PDOStatement::setFetchMode() share pdo_stmt_setup_fetch_mode() in ext/pdo/pdo_stmt.c. The callers pass the position of the fetch mode (1 from setFetchMode(), 2 from PDO::query()), and the function computes arg1_arg_num and constructor_arg_num from it. All errors in the function use these values, except the constructor check, which hardcodes 3 and #2. That is only right for setFetchMode().

The message was added in 3ff7758bcf7 (GH-17427), so PHP 8.5 and later are affected. PHP 8.4 is not affected: it reports a different error for this case. pdo_stmt_class_ctor_errors.phpt covers fetchAll() and setFetchMode(), but not PDO::query().

A fix is to use constructor_arg_num and arg1_arg_num in this message. I have a patch with a test and will open a PR against PHP-8.5.

The quoted paragraphs were drafted with an LLM. The code and the output above come from real runs on PHP 8.4, 8.5 and 8.6.

PHP Version
PHP 8.5.12 (cli) (built: Oct 28 2026 01:20:43) (NTS)
Copyright (c) The PHP Group
Built by https://github.com/docker-library/php
Zend Engine v4.5.12, Copyright (c) Zend Technologies
    with Zend OPcache v8.5.12, Copyright (c), by Zend Technologies

Also reproduced on PHP 8.6.0 and on the PHP-8.5 branch (8.5.14-dev).

Operating System

Debian 13 (php:8.5-cli Docker image)

主要語言
C
星號
41.8k
分支
8.6k
平均合併
2 天 15 小時
30 天內合併 PR
125

環境準備

  • 沒有 Dockerfile 或 Docker Compose 檔案
  • 沒有 Pull Request 範本
  • 閱讀貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

php/php-src 的其他 Issue

查看 php/php-src 的全部 Issue

相似的 Issue

更多 C Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。