Skip to content

Fix error message allocation of PDO PgSQL. #7723

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

Conversation

ranvis
Copy link
Contributor

@ranvis ranvis commented Dec 5, 2021

In PDO PgSQL driver, some error messages are allocated using estrdup() instead of pestrdup().
This cause crash if persistent connection is used and the last error message is freed (with pefree().)

<?php
$db = new PDO(
    'pgsql:dbname=' . DB_NAME,
    USER_NAME,
    PASSWORD,
    [
        PDO::ATTR_PERSISTENT => true,
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_EMULATE_PREPARES => false,
    ],
);

$st = $db->prepare('select 1');
for ($i = 0; ++$i <= 2;) {
    try {
        $st->bindValue(':invalid', $i);
    } catch (PDOException $e) {
        echo "$i\n";
    }
}

Expected result:

1
2

Actual result:

1
*** Error in `php': free(): invalid pointer: 0x... ***
======= Backtrace: =========
/lib64/libc.so.6(+0x81329)[0x...]
/usr/lib64/php/modules/pdo_pgsql.so(+0x3c11)[0x...]

Copy link
Member

@cmb69 cmb69 left a comment

Choose a reason for hiding this comment

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

Thank you for the PR! The fix is correct, but it would be good to have a regression tests for that. You can copy the boilerplate from any of the pgo_pgsql tests, for instance, https://github.com/php/php-src/blob/PHP-8.0/ext/pdo_pgsql/tests/bug73959.phpt.

@ranvis
Copy link
Contributor Author

ranvis commented Dec 6, 2021

Added regression test as pr7723.phpt. Thank you for the pointer.

Regarding #7725, I first wondered if Issue must be created first.
Another thing that I wondered is that I cannot find a naming convention for *.phpt compatible with the current system.
https://qa.php.net/write-test.php#naming-conventions
I know bug{id}.phpt is bad because the number will collide in future though.

@cmb69
Copy link
Member

cmb69 commented Dec 6, 2021

Thanks for adding the regression test! And, frankly, I haven't thought about the naming issue so far. Maybe we should just go with gh7723.phpt; maybe that should be discussed on the mailing list. Maybe @nikic has some thoughts about that.

@ranvis ranvis force-pushed the fix-pdo-pgsql-errmsg branch from 85825c7 to 44a7e39 Compare December 7, 2021 15:37
@ranvis
Copy link
Contributor Author

ranvis commented Dec 7, 2021

Thanks. Not sure how it will go I changed to the better one anyway.

@ranvis ranvis force-pushed the fix-pdo-pgsql-errmsg branch from 44a7e39 to ac81cef Compare December 7, 2021 15:40
@cmb69 cmb69 closed this in 778513f Dec 12, 2021
@ranvis ranvis deleted the fix-pdo-pgsql-errmsg branch December 23, 2021 08:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants