-
Notifications
You must be signed in to change notification settings - Fork 7.9k
ext/pdo: Convert def_stmt_ctor_args field to Hashtable* #12154
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
Closed
Changes from all commits
Commits
Show all changes
4 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
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
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,48 @@ | ||
--TEST-- | ||
PDO: 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); | ||
|
||
$dummy_query = get_dummy_sql_request(); | ||
|
||
$stmt = $db->query($dummy_query); | ||
var_dump($stmt instanceof Foo); | ||
var_dump($stmt->queryString === $dummy_query); | ||
|
||
?> | ||
--EXPECT-- | ||
object(Bar)#1 (1) { | ||
["statementClass"]=> | ||
string(3) "Foo" | ||
} | ||
bool(true) | ||
bool(true) |
37 changes: 37 additions & 0 deletions
37
ext/pdo/tests/pdo_ATTR_STATEMENT_CLASS_ctor_arg_no-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,37 @@ | ||
--TEST-- | ||
PDO: Set PDOStatement class with ctor_args that are freed without 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); | ||
} | ||
} | ||
|
||
$db = PDOTest::factory(); | ||
|
||
$db->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('Foo', ['param1'])); | ||
|
||
$dummy_query = get_dummy_sql_request(); | ||
|
||
$stmt = $db->query($dummy_query); | ||
var_dump($stmt instanceof Foo); | ||
var_dump($stmt->queryString === $dummy_query); | ||
|
||
?> | ||
--EXPECT-- | ||
string(6) "param1" | ||
bool(true) | ||
bool(true) |
39 changes: 39 additions & 0 deletions
39
ext/pdo/tests/pdo_ATTR_STATEMENT_CLASS_ctor_arg_no-gc_var_modified.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,39 @@ | ||
--TEST-- | ||
PDO: Set PDOStatement class with ctor_args that are freed without GC intervention as a variable that is modified | ||
--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); | ||
} | ||
} | ||
|
||
$db = PDOTest::factory(); | ||
|
||
$a = ['Foo', ['param1']]; | ||
$db->setAttribute(PDO::ATTR_STATEMENT_CLASS, $a); | ||
$a[0] = 'Bar'; | ||
|
||
$dummy_query = get_dummy_sql_request(); | ||
|
||
$stmt = $db->query($dummy_query); | ||
var_dump($stmt instanceof Foo); | ||
var_dump($stmt->queryString === $dummy_query); | ||
|
||
?> | ||
--EXPECT-- | ||
string(6) "param1" | ||
bool(true) | ||
bool(true) |
39 changes: 39 additions & 0 deletions
39
ext/pdo/tests/pdo_ATTR_STATEMENT_CLASS_ctor_arg_no-gc_var_unset.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,39 @@ | ||
--TEST-- | ||
PDO: Set PDOStatement class with ctor_args that are freed without GC intervention as a variable that is unset | ||
--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); | ||
} | ||
} | ||
|
||
$db = PDOTest::factory(); | ||
|
||
$a = ['Foo', ['param1']]; | ||
$db->setAttribute(PDO::ATTR_STATEMENT_CLASS, $a); | ||
unset($a); | ||
|
||
$dummy_query = get_dummy_sql_request(); | ||
|
||
$stmt = $db->query($dummy_query); | ||
var_dump($stmt instanceof Foo); | ||
var_dump($stmt->queryString === $dummy_query); | ||
|
||
?> | ||
--EXPECT-- | ||
string(6) "param1" | ||
bool(true) | ||
bool(true) |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Isn't increasing the count here the cause of the memory leak?
Passing it to the constructor increments the ref count, so I thought maybe there was no need to increment the count at this point, so I commented it out and tried running the following code.
Memory leak no longer occurs.
result:
I also tried this as well.
This does not leak memory to begin with, but I tried it to confirm behavior.
result:
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.
I don't think not incrementing the reference counter is the correct approach.
As something along the lines of:
Should still work, I'm pretty sure this is why I did a dup.
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.
Also just removing this line causes use after frees so just that is definitely not the solution.
Uh oh!
There was an error while loading. Please reload this page.
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.
@Girgias
This was clearly a mistake.
However, after looking around, I still don't think I should increment the refcount here, for other reasons.The test code you provided that includes unset worked fine even after removingGC_TRY_ADDREF
.dbh->def_stmt_ctor_args = Z_ARRVAL_P(item);
is a copy, so whatever happens to$a
after this should have no effect at all (of course, there is no problem usingunset()
).Sorry, the above is also incorrect.
In the following case, at the time of setAttribute, refcount is 2, and unset() reduces it to 1.
(with removing
GC_TRY_ADDREF
. I checked by gdb.)Uh oh!
There was an error while loading. Please reload this page.
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.
If I remove
GC_TRY_ADDREF
, ht will be freed by the next process and then used again.So in this case, it definitely need an
GC_TRY_ADDREF
here, I understood it.