-
Notifications
You must be signed in to change notification settings - Fork 7.9k
ext/pdo: Convert def_stmt_ctor_args field from zval to HashTable pointer #17396
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
Draft
Girgias
wants to merge
2
commits into
php:master
Choose a base branch
from
Girgias:pdo-statement-ctor-args
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
ext/pdo/tests/attr_statement_class/pdo_ATTR_STATEMENT_CLASS_basic.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
--TEST-- | ||
PDO Common: Set PDOStatement class with PDO::ATTR_STATEMENT_CLASS overall test | ||
--EXTENSIONS-- | ||
pdo | ||
--SKIPIF-- | ||
<?php | ||
$dir = getenv('REDIR_TEST_DIR'); | ||
if (false == $dir) die('skip no driver'); | ||
require_once $dir . 'pdo_test.inc'; | ||
PDOTest::skip(); | ||
?> | ||
--FILE-- | ||
<?php | ||
if (getenv('REDIR_TEST_DIR') === false) putenv('REDIR_TEST_DIR='.__DIR__ . '/../../pdo/tests/'); | ||
require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc'; | ||
|
||
$db = PDOTest::factory(); | ||
$db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, true); | ||
|
||
$table = 'pdo_attr_statement_class_basic'; | ||
$db->exec("CREATE TABLE {$table} (id INT, label CHAR(1), PRIMARY KEY(id))"); | ||
$db->exec("INSERT INTO {$table} (id, label) VALUES (1, 'a')"); | ||
$db->exec("INSERT INTO {$table} (id, label) VALUES (2, 'b')"); | ||
|
||
$default = $db->getAttribute(PDO::ATTR_STATEMENT_CLASS); | ||
var_dump($default); | ||
|
||
// Having a public destructor is allowed | ||
class StatementWithPublicDestructor extends PDOStatement { | ||
public function __destruct() { | ||
echo __METHOD__, PHP_EOL; | ||
} | ||
} | ||
|
||
try { | ||
var_dump($db->setAttribute(PDO::ATTR_STATEMENT_CLASS, ['StatementWithPublicDestructor', []])); | ||
} catch (\Throwable $e) { | ||
echo $e::class, ': ', $e->getMessage(), \PHP_EOL; | ||
} | ||
var_dump($db->setAttribute(PDO::ATTR_STATEMENT_CLASS, ['StatementWithPublicDestructor'])); | ||
$stmt = $db->query("SELECT id, label FROM {$table} ORDER BY id ASC"); | ||
unset($stmt); | ||
|
||
echo "Class derived from PDOStatement, with private constructor:\n"; | ||
class StatementWithPrivateConstructor extends PDOStatement { | ||
private function __construct($msg) { | ||
echo __METHOD__, PHP_EOL; | ||
var_dump($this); | ||
var_dump($msg); | ||
} | ||
} | ||
var_dump($db->setAttribute(PDO::ATTR_STATEMENT_CLASS, ['StatementWithPrivateConstructor', ['param1']])); | ||
$stmt = $db->query("SELECT id, label FROM {$table} ORDER BY id ASC"); | ||
unset($stmt); | ||
|
||
echo "Class derived from a child of PDOStatement:\n"; | ||
class StatementDerivedFromChild extends StatementWithPrivateConstructor { | ||
public function fetchAll($fetch_style = 1, ...$fetch_args): array { | ||
return []; | ||
} | ||
} | ||
|
||
var_dump($db->setAttribute(PDO::ATTR_STATEMENT_CLASS, ['StatementDerivedFromChild', ['param1']])); | ||
$stmt = $db->query("SELECT id, label FROM {$table} ORDER BY id ASC"); | ||
var_dump($stmt->fetchAll()); | ||
unset($stmt); | ||
|
||
echo "Reset to default PDOStatement class:\n"; | ||
var_dump($db->setAttribute(PDO::ATTR_STATEMENT_CLASS, ['PDOStatement'])); | ||
$stmt = $db->query("SELECT id, label FROM {$table} ORDER BY id ASC"); | ||
var_dump($stmt->fetchAll()); | ||
unset($stmt); | ||
|
||
?> | ||
--CLEAN-- | ||
<?php | ||
require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc'; | ||
$db = PDOTest::factory(); | ||
PDOTest::dropTableIfExists($db, "pdo_attr_statement_class_basic"); | ||
?> | ||
--EXPECT-- | ||
array(1) { | ||
[0]=> | ||
string(12) "PDOStatement" | ||
} | ||
bool(true) | ||
bool(true) | ||
StatementWithPublicDestructor::__destruct | ||
Class derived from PDOStatement, with private constructor: | ||
bool(true) | ||
StatementWithPrivateConstructor::__construct | ||
object(StatementWithPrivateConstructor)#2 (1) { | ||
["queryString"]=> | ||
string(68) "SELECT id, label FROM pdo_attr_statement_class_basic ORDER BY id ASC" | ||
} | ||
string(6) "param1" | ||
Class derived from a child of PDOStatement: | ||
bool(true) | ||
StatementWithPrivateConstructor::__construct | ||
object(StatementDerivedFromChild)#2 (1) { | ||
["queryString"]=> | ||
string(68) "SELECT id, label FROM pdo_attr_statement_class_basic ORDER BY id ASC" | ||
} | ||
string(6) "param1" | ||
array(0) { | ||
} | ||
Reset to default PDOStatement class: | ||
bool(true) | ||
array(2) { | ||
[0]=> | ||
array(4) { | ||
["id"]=> | ||
string(1) "1" | ||
[0]=> | ||
string(1) "1" | ||
["label"]=> | ||
string(1) "a" | ||
[1]=> | ||
string(1) "a" | ||
} | ||
[1]=> | ||
array(4) { | ||
["id"]=> | ||
string(1) "2" | ||
[0]=> | ||
string(1) "2" | ||
["label"]=> | ||
string(1) "b" | ||
[1]=> | ||
string(1) "b" | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
ext/pdo/tests/attr_statement_class/pdo_ATTR_STATEMENT_CLASS_ctor_arg_gc.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
--TEST-- | ||
PDO Common: Set PDOStatement class with ctor_args that are freed with GC intervention | ||
--EXTENSIONS-- | ||
pdo | ||
--SKIPIF-- | ||
<?php | ||
$dir = getenv('REDIR_TEST_DIR'); | ||
if (false == $dir) die('skip no driver'); | ||
require_once $dir . 'pdo_test.inc'; | ||
PDOTest::skip(); | ||
?> | ||
--FILE-- | ||
<?php | ||
if (getenv('REDIR_TEST_DIR') === false) putenv('REDIR_TEST_DIR='.__DIR__ . '/../../pdo/tests/'); | ||
require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc'; | ||
|
||
class Foo extends PDOStatement { | ||
private function __construct($v) { | ||
var_dump($v); | ||
} | ||
} | ||
|
||
class Bar extends PDO { | ||
public $statementClass = 'Foo'; | ||
function __construct($dsn, $username, $password, $driver_options = []) { | ||
$driver_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION; | ||
parent::__construct($dsn, $username, $password, $driver_options); | ||
|
||
$this->setAttribute(PDO::ATTR_STATEMENT_CLASS, [$this->statementClass, [$this]]); | ||
} | ||
} | ||
|
||
$db = PDOTest::factory(Bar::class); | ||
|
||
$table = 'pdo_attr_statement_class_ctor_arg_gc'; | ||
$db->exec("CREATE TABLE {$table} (id INT, label CHAR(1), PRIMARY KEY(id))"); | ||
$db->exec("INSERT INTO {$table} (id, label) VALUES (1, 'a')"); | ||
$db->exec("INSERT INTO {$table} (id, label) VALUES (2, 'b')"); | ||
$query = "SELECT id, label FROM {$table} ORDER BY id ASC"; | ||
$stmt = $db->query($query); | ||
|
||
var_dump($stmt instanceof Foo); | ||
var_dump($stmt->queryString === $query); | ||
|
||
?> | ||
--CLEAN-- | ||
<?php | ||
require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc'; | ||
$db = PDOTest::factory(); | ||
PDOTest::dropTableIfExists($db, "pdo_attr_statement_class_ctor_arg_gc"); | ||
?> | ||
--EXPECT-- | ||
object(Bar)#1 (1) { | ||
["statementClass"]=> | ||
string(3) "Foo" | ||
} | ||
bool(true) | ||
bool(true) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You probably didn't understand the root cause of the issue properly as this is almost certainly wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is indeed wrong. If cycles are registered by not collected then you lose the link to the ctor_args and future invocations may have wrong results.
The problem is that on shutdown the cycles are collected, the array is destroyed first, and then again in free_storage. You can solve this by using a dtor:
Not sure if the size save is worth the complexity though.