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

Offen Anfängerfreundlich
#25,650 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Anfängerfreundlichkeit
72/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
c
Bereich
database

Rechercherichtung

Die falschen Zahlen kommen von der Konstruktorprüfung in pdo_stmt_setup_fetch_mode() in ext/pdo/pdo_stmt.c, die 3 und #2 fest codiert, obwohl die Funktion bereits constructor_arg_num und arg1_arg_num berechnet. Vergleiche diese Prüfung mit den anderen Fehlern in derselben Funktion und lies dann pdo_stmt_class_ctor_errors.phpt, um zu sehen, wie bestehende Fälle getestet werden. Fertig ist es, wenn der Fall PDO::query() Argument #4 und #3 meldet und die Ausgabe von setFetchMode() unverändert bleibt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

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)

Vorherrschende Sprache
C
Sterne
41.8k
Forks
8.6k
Ø Merge
2 T. 15 Std.
Gemergte PRs (30 T.)
125

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus php/php-src

Alle Issues in php/php-src

Ähnliche Issues

Weitere Issues zu C

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.