Skip to content

PDOStatement::$queryString "readonly" is not properly implemented #17346

Open
@nielsdos

Description

@nielsdos

Description

The following code:

<?php
$stmt = new PDOStatement();
var_dump($stmt);
for($i=0;$i<10;$i++)
        $stmt->queryString = (string) $i;
var_dump($stmt);

Resulted in this output:

object(PDOStatement)#1 (0) {
  ["queryString"]=>
  uninitialized(string)
}
object(PDOStatement)#1 (1) {
  ["queryString"]=>
  string(1) "9"
}

But I expected this output instead:

object(PDOStatement)#1 (0) {
  ["queryString"]=>
  uninitialized(string)
}
Fatal error: Uncaught Error: Property queryString is read only in x.php:5

The reason this happens is because the following code is wrong:

php-src/ext/pdo/pdo_stmt.c

Lines 2008 to 2014 in 209e0d6

if (zend_string_equals_literal(name, "queryString")) {
zval *query_string = OBJ_PROP_NUM(object, 0);
if (!Z_ISUNDEF_P(query_string)) {
zend_throw_error(NULL, "Property queryString is read only");
return value;
}
}

The second loop iteration just reuses the cache slot and won't go through the write handler, bypassing the fake readonly check.
Either this should be relaxed or be fixed (i.e. "real readonly").

PHP Version

8.3+

Operating System

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions