diff --git a/NEWS b/NEWS index c599b16bf9f9..06a3676090b1 100644 --- a/NEWS +++ b/NEWS @@ -178,6 +178,10 @@ PHP NEWS . Fixed a crash when executing a prepared statement again after a failed execute() with libmysqlclient. (Ilia Alshanetsky) +- PDO_PGSQL: + . Fixed bug GH-25838 (use-after-free when a lazy statement follows a failed + query). (Yun Dou) + - PDO_SQLite: . Fixed bug GH-25588 (Use-after-free of a string parameter omitted on a later execute() or rebound while fetching). (Ilia Alshanetsky) diff --git a/ext/pdo_pgsql/pgsql_statement.c b/ext/pdo_pgsql/pgsql_statement.c index b2af41a339b1..60199f86269f 100644 --- a/ext/pdo_pgsql/pgsql_statement.c +++ b/ext/pdo_pgsql/pgsql_statement.c @@ -191,7 +191,7 @@ static int pgsql_stmt_dtor(pdo_stmt_t *stmt) pgsql_stmt_finish(S, FIN_DISCARD|(server_obj_usable ? FIN_CLOSE|FIN_ABORT : 0)); - if (server_obj_usable && S->H->running_stmt == S) { + if ((server_obj_usable || stmt->database_object_handle == NULL) && S->H->running_stmt == S) { S->H->running_stmt = NULL; } diff --git a/ext/pdo_pgsql/tests/gh25838.phpt b/ext/pdo_pgsql/tests/gh25838.phpt new file mode 100644 index 000000000000..02b575161279 --- /dev/null +++ b/ext/pdo_pgsql/tests/gh25838.phpt @@ -0,0 +1,30 @@ +--TEST-- +GH-25838 (pdo_pgsql: use-after-free when a lazy statement follows a failed query) +--EXTENSIONS-- +pdo_pgsql +--SKIPIF-- + +--CREDITS-- +Yun Dou +--FILE-- +setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + +try { + $pdo->query('SELECT 1 / 0'); +} catch (PDOException $e) { + echo $e->getCode(), PHP_EOL; +} + +$pdo->setAttribute(PDO::ATTR_PREFETCH, 0); +echo $pdo->query('SELECT 43')->fetchColumn(), PHP_EOL; +?> +--EXPECT-- +22012 +43