php-src icon indicating copy to clipboard operation
php-src copied to clipboard

PDOException with SQLSTATE string loses code after serialization round-trip on PHP 8.5.12

Open tacman opened this issue 3 weeks ago • 0 comments

PHP 8.5.12 CLI NTS, Homebrew macOS.

Minimal reproduction, no framework:

try {
    $pdo = new PDO('sqlite::memory:');
    $pdo->query('select * from missing');
} catch (PDOException $e) {
    $copy = unserialize(serialize($e));
    var_dump($e->getCode(), $copy->getCode());
}

Actual:

Warning: Undefined property: PDOException::$code
string(5) "HY000"
NULL

Expected: both codes remain "HY000" without warnings.

A manually constructed PDOException with an integer code round-trips correctly. This affects actual driver errors with SQLSTATE strings.

Impact: Symfony 8.2 parallel Messenger sends exceptions between processes. A PostgreSQL unique-constraint error in our application triggered this defect; ErrorHandler's FlattenException::setCode(string|int) then received null and stopped the worker. The standalone SQLite reproduction demonstrates that the serialization defect is independent of Symfony and PostgreSQL.

Independent review

The same reproduction fails with php -n -d zend.exception_ignore_args=1, without php.ini, optional extensions, Composer, or Symfony.

Before unserialization, the serialized representation contains the protected code property with the string value HY000; after unserialization that property is absent. The PHP-8.5 source has CHECK_EXC_TYPE(ZEND_STR_CODE, IS_LONG) in Exception::__wakeup(), which unsets a non-integer code: https://github.com/php/php-src/blob/PHP-8.5/Zend/zend_exceptions.c

PDO explicitly permits a SQLSTATE string code: https://www.php.net/manual/en/pdoexception.getcode.php

This report establishes reproduction on 8.5.12; it does not claim the bug was introduced in that release or only affects that branch.

tacman avatar Nov 07 '26 23:11 tacman