[php-src] Issue #23758: PDO_Firebird returns null for non-null empty BLOBs
| From: | MrSpockDe | Date: | Fri, 18 Sep 2026 16:11:30 +0000 |
| Subject: | [php-src] Issue #23758: PDO_Firebird returns null for non-null empty BLOBs | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-252774@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/23758
Author: MrSpockDe
### Description
<h2>Description</h2><p>PDO_Firebird returns PHP <code
dir="ltr">null</code> when fetching a non-NULL BLOB whose length is
zero.</p><p><strong>Expected behavior:</strong> An empty BLOB should return
an empty PHP string (<code dir="ltr">''</code>), while SQL NULL
should return PHP <code
dir="ltr">null</code>.</p><p><strong>Actual
behavior:</strong> Both values return PHP <code dir="ltr">null</code>,
although SQL expressions confirm that Firebird stores them
distinctly:</p><ul><li>SQL NULL: <code dir="ltr">IS NULL =
true</code>, <code dir="ltr">OCTET_LENGTH =
NULL</code></li><li>Empty BLOB: <code dir="ltr">IS NULL =
false</code>, <code dir="ltr">OCTET_LENGTH =
0</code></li></ul><p>This loses information during retrieval. Applications
cannot distinguish an absent value from an explicitly stored empty value.</p><p>The
behavior occurs with both text and binary BLOBs, after INSERT and UPDATE, using string or stream
input. It reproduces with direct PDO, without Laravel or another
framework.</p><h2>Reproducer</h2><p>Requires an existing Firebird database
and PDO_Firebird. Set <code dir="ltr">FB_DSN</code>, <code
dir="ltr">FB_USER</code>, and <code
dir="ltr">FB_PASSWORD</code> as appropriate.</p><pre
dir="ltr"><code><?php
// Example: FB_DSN='firebird:dbname=localhost/3055:/path/database.fdb;charset=UTF8'
// FB_USER=sysdba FB_PASSWORD=... php pdo_firebird_empty_blob.php
$pdo = new PDO(
getenv('FB_DSN') ?:
'firebird:dbname=localhost/3055:/var/lib/firebird/data/database..fdb;charset=UTF8',
getenv('FB_USER') ?: 'sysdba',
getenv('FB_PASSWORD') ?: 'masterkey',
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_ORACLE_NULLS =>
PDO::NULL_NATURAL]
);
$table = 'BLOB_REPRO_'.bin2hex(random_bytes(6));
$created = false;
$stmt = null;
try {
$pdo->exec("CREATE TABLE $table (ID INTEGER, PAYLOAD BLOB SUB_TYPE BINARY)");
$created = true;
$stmt = $pdo->prepare("INSERT INTO $table (ID, PAYLOAD) VALUES (?, ?)");
foreach ([null, '', "\0"] as $id => $value) {
$stmt->bindValue(1, $id, PDO::PARAM_INT);
$stmt->bindValue(2, $value, $value === null ? PDO::PARAM_NULL : PDO::PARAM_STR);
$stmt->execute();
}
$stmt = null;
$stmt = $pdo->query("SELECT ID, CASE WHEN PAYLOAD IS NULL THEN 1 ELSE 0 END,
OCTET_LENGTH(PAYLOAD), PAYLOAD FROM $table ORDER BY ID");
while ($row = $stmt->fetch(PDO::FETCH_NUM)) {
$value = $row[3] === null ? 'NULL' : 'string('.strlen($row[3]).')
hex='.bin2hex($row[3]);
printf("%s: IS NULL=%s; OCTET_LENGTH=%s; PHP value=%s\n",
['SQL NULL', 'Empty BLOB', 'NUL byte'][(int) $row[0]],
$row[1] ? 'true' : 'false',
$row[2] === null ? 'NULL' : $row[2],
$value
);
}
} finally {
// Release prepared statements before dropping the table (Firebird metadata locks).
$stmt = null;
if ($created) {
$pdo->exec("DROP TABLE $table");
}
}</code></pre><h2>Actual output</h2><pre
dir="ltr"><code>SQL NULL: IS NULL=true; OCTET_LENGTH=NULL; PHP value=NULL
Empty BLOB: IS NULL=false; OCTET_LENGTH=0; PHP value=NULL
NUL byte: IS NULL=false; OCTET_LENGTH=1; PHP value=string(1)
hex=00</code></pre><h2>Expected output</h2><pre
dir="ltr"><code>SQL NULL: IS NULL=true; OCTET_LENGTH=NULL; PHP value=NULL
Empty BLOB: IS NULL=false; OCTET_LENGTH=0; PHP value=string(0) hex=
NUL byte: IS NULL=false; OCTET_LENGTH=1; PHP value=string(1)
hex=00</code></pre><h2>Tested versions</h2><p>Earlier framework-free
PDO diagnostics reproduced the behavior with:</p><div><div><div>
PHP | Firebird server | Firebird client | Platform
-- | -- | -- | --
8.2.33 | 4.0.7 and 5.0.4 | 3.0.11 | Linux
8..3.33 | 4.0.7 and 5.0.4 | 3.0.11 | Linux
8.4.25 | 4.0.7 and 5.0.4 | 3.0.11 | Linux
8.5.10 | 4.0.7 and 5.0.4 | 3.0.11 | Linux
</div></div></div><p>The exact minimal reproducer above was additionally
verified with PHP <strong>8.5.2</strong>, Firebird client
<strong>5.0.3</strong>, on macOS, against both server
versions.</p><p><code dir="ltr">FETCH_ASSOC</code>, <code
dir="ltr">FETCH_OBJ</code>, and <code
dir="ltr">FETCH_NUM</code> show the same behavior. Using <code
dir="ltr">bindColumn(..., PDO::PARAM_LOB)</code> with <code
dir="ltr">FETCH_BOUND</code> returns streams for nonempty BLOBs, but still
returns <code dir="ltr">null</code> for empty
BLOBs.</p><p><code dir="ltr">PDO::NULL_TO_STRING</code> is not a
solution because it also converts genuine SQL NULL values to empty
strings.</p><h2>Source analysis</h2><p>In <code
dir="ltr">ext/pdo_firebird/firebird_statement..c</code>, the relevant function is
<code dir="ltr">php_firebird_fetch_blob()</code> in PHP 8.4/8.5, named
<code dir="ltr">firebird_fetch_blob()</code> in PHP
8.2/8.3.</p><p>The function assigns a string to the result inside <code
dir="ltr">if (len)</code>. When the BLOB length is zero, that assignment is
skipped, while the function still returns success.</p><p>PDO initializes the destination
to NULL before invoking the driver's column-fetch callback in <code
dir="ltr">ext/pdo/pdo_stmt.c</code>. This appears to explain the observed
result.</p>
### PHP Version
```plain
PHP 8..5.2 (cli) (built: Jan 13 2026 21:40:53) (NTS)
Copyright (c) The PHP Group
Built by Homebrew
Zend Engine v4.5.2, Copyright (c) Zend Technologies
with Zend OPcache v8.5.2, Copyright (c), by Zend Technologies
```
### Operating System
macOS