Skip to content

Fix __call in PDO subclass breaking method calls #9102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions ext/pdo/pdo_dbh.c
Original file line number Diff line number Diff line change
Expand Up @@ -1291,7 +1291,8 @@ static zend_function *dbh_method_get(zend_object **object, zend_string *method_n
pdo_dbh_object_t *dbh_obj = php_pdo_dbh_fetch_object(*object);
zend_string *lc_method_name;

if ((fbc = zend_std_get_method(object, method_name, key)) == NULL) {
fbc = zend_std_get_method(object, method_name, key);
if (fbc == NULL || (fbc->type == ZEND_USER_FUNCTION && ((zend_op_array *)fbc)->opcodes == &EG(call_trampoline_op))) {
/* not a pre-defined method, nor a user-defined method; check
* the driver specific methods */
if (!dbh_obj->inner->cls_methods[PDO_DBH_DRIVER_METHOD_KIND_DBH]) {
Expand All @@ -1303,8 +1304,16 @@ static zend_function *dbh_method_get(zend_object **object, zend_string *method_n
}

lc_method_name = zend_string_tolower(method_name);
fbc = zend_hash_find_ptr(dbh_obj->inner->cls_methods[PDO_DBH_DRIVER_METHOD_KIND_DBH], lc_method_name);
zend_function *driver_fbc = zend_hash_find_ptr(dbh_obj->inner->cls_methods[PDO_DBH_DRIVER_METHOD_KIND_DBH], lc_method_name);
zend_string_release_ex(lc_method_name, 0);

if (driver_fbc) {
if (fbc) {
zend_string_release(fbc->common.function_name);
zend_free_trampoline(fbc);
}
fbc = driver_fbc;
}
}

out:
Expand Down
36 changes: 36 additions & 0 deletions ext/pdo_sqlite/tests/gh7803.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
--TEST--
GH-7803: PDO __call breaks method calls
--FILE--
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to explicitly declare the required extensions (pdo_sqlite is not loaded on AppVeyor by default):

Suggested change
--FILE--
--EXTENSIONS--
pdo_sqlite
--FILE--

<?php

class DB extends PDO
{
public function __call(string $name, array $args)
{
echo "__call: $name\n";
}
}

$db = new DB('sqlite::memory:', '', '', []);

$db->sqliteCreateFunction('CONCAT', function (...$args) {
return implode('', $args);
});

$sqlite = 'sqlite';
$createFunction = 'CreateFunction';
$db->{$sqlite . $createFunction}('CONCAT2', function () {});

$db->nonexistent();

var_dump($db->query('SELECT CONCAT(\'123\', \'456\', \'789\')')->fetch());

?>
--EXPECT--
__call: nonexistent
array(2) {
["CONCAT('123', '456', '789')"]=>
string(9) "123456789"
[0]=>
string(9) "123456789"
}