[php-src] master: ext/pdo_mysql: Fix crash re-executing a statement after a failed execute

From: Date: Wed, 18 Nov 2026 15:05:19 +0000
Subject: [php-src] master: ext/pdo_mysql: Fix crash re-executing a statement after a failed execute
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-143830@lists.php.net to get a copy of this message
Author: Ilia Alshanetsky (iliaal) Date: 2026-11-18T09:49:28-05:00 Commit: https://github.com/php/php-src/commit/0edaacebef0c702cb1c08fe071e803ee394e5a3c Raw diff: https://github.com/php/php-src/commit/0edaacebef0c702cb1c08fe071e803ee394e5a3c.diff ext/pdo_mysql: Fix crash re-executing a statement after a failed execute With libmysqlclient, a failed bind or execute in pdo_mysql_stmt_execute_prepared_libmysql() zeroed S->params, which also cleared the is_null and length pointers that PDO_PARAM_EVT_ALLOC sets once per bound parameter. Executing the statement again without rebinding then wrote through NULL in PDO_PARAM_EVT_EXEC_PRE. Drop the clear: EXEC_PRE rewrites every field libmysqlclient reads before each bind, as it already does on the success path. mysqlnd builds are unaffected. Closes GH-25554 Changed paths: A ext/pdo_mysql/tests/pdo_mysql_stmt_execute_retry_after_failure.phpt M NEWS M ext/pdo_mysql/mysql_statement.c Diff: diff --git a/NEWS b/NEWS index 99cabf55d1c0..5a7dd2c30171 100644 --- a/NEWS +++ b/NEWS @@ -106,6 +106,10 @@ PHP NEWS . Fixed \C being allowed in patterns enabling UTF mode with (*UTF) instead of the u modifier. (Jorg Sowa) +- PDO_MYSQL: + . Fixed a crash when executing a prepared statement again after a failed + execute() with libmysqlclient. (Ilia Alshanetsky) + - POSIX: . Fixed bug GH-25530 (posix_get_last_error() returns an error from a previous request). (Artem Ukrainskiy) diff --git a/ext/pdo_mysql/mysql_statement.c b/ext/pdo_mysql/mysql_statement.c index 0ce7c7224dc6..0b5429126672 100644 --- a/ext/pdo_mysql/mysql_statement.c +++ b/ext/pdo_mysql/mysql_statement.c @@ -270,9 +270,6 @@ static int pdo_mysql_stmt_execute_prepared_libmysql(pdo_stmt_t *stmt) /* {{{ */ /* (re)bind the parameters */ if (mysql_stmt_bind_param(S->stmt, S->params) || mysql_stmt_execute(S->stmt)) { - if (S->params) { - memset(S->params, 0, S->num_params * sizeof(MYSQL_BIND)); - } pdo_mysql_error_stmt(stmt); if (mysql_stmt_errno(S->stmt) == 2057) { /* CR_NEW_STMT_METADATA makes the statement unusable */ diff --git a/ext/pdo_mysql/tests/pdo_mysql_stmt_execute_retry_after_failure.phpt b/ext/pdo_mysql/tests/pdo_mysql_stmt_execute_retry_after_failure.phpt new file mode 100644 index 000000000000..c1d8efb1762d --- /dev/null +++ b/ext/pdo_mysql/tests/pdo_mysql_stmt_execute_retry_after_failure.phpt @@ -0,0 +1,61 @@ +--TEST-- +PDO MySQL native prepared statement can retry after execute failure +--EXTENSIONS-- +pdo_mysql +--SKIPIF-- +<?php +require_once __DIR__ . '/inc/mysql_pdo_test.inc'; +MySQLPDOTest::skip(); +?> +--FILE-- +<?php +require_once __DIR__ . '/inc/mysql_pdo_test.inc'; + +$db = MySQLPDOTest::factory(); +$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); +$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); +$db->exec('CREATE TABLE pdo_mysql_stmt_execute_retry (id INT PRIMARY KEY, value INT NOT NULL)'); + +$stmt = $db->prepare('INSERT INTO pdo_mysql_stmt_execute_retry (id, value) VALUES (1, ?)'); +$value = null; +$stmt->bindParam(1, $value, PDO::PARAM_INT); + +try { + $stmt->execute(); +} catch (PDOException $e) { + echo $e::class, ': ', $e->getMessage(), PHP_EOL; +} + +$value = 2; +var_dump($stmt->execute()); + +$stmt = $db->prepare('INSERT INTO pdo_mysql_stmt_execute_retry (id, value) VALUES (?, ?)'); +$stmt->bindValue(1, 2, PDO::PARAM_INT); +$value = null; +$stmt->bindParam(2, $value, PDO::PARAM_STR); + +try { + $stmt->execute(); +} catch (PDOException $e) { + echo $e::class, ': ', $e->getMessage(), PHP_EOL; +} + +$value = '3'; +var_dump($stmt->execute()); +foreach ($db->query('SELECT id, value FROM pdo_mysql_stmt_execute_retry ORDER BY id') as $row) { + echo $row['id'], ': ', $row['value'], PHP_EOL; +} +?> +--CLEAN-- +<?php +require_once __DIR__ . '/inc/mysql_pdo_test.inc'; +$db = MySQLPDOTest::factory(); +$db->exec('DROP TABLE IF EXISTS pdo_mysql_stmt_execute_retry'); +?> +--EXPECT-- +PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'value' cannot be null +bool(true) +PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'value' cannot be null +bool(true) +1: 2 +2: 3

« previous php.cvs (#143830) next »