Skip to content

Commit f0236f1

Browse files
committed
pdo_pgsql: preserve the pending exception when a COPY row fails to convert
pgsqlCopyFromArray() feeds each row through try_convert_to_string(). A non-stringable row throws a TypeError, but both the array and iterator branches then called pdo_pgsql_error() and recorded a fabricated PGRES_FATAL_ERROR, leaving PDO::errorInfo() reporting "HY000" for what is a client-side type error. Return through the pending exception instead of overwriting the driver error state. Closes phpGH-22384
1 parent cf56a8e commit f0236f1

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

ext/pdo_pgsql/pgsql_driver.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,9 @@ void pgsqlCopyFromArray_internal(INTERNAL_FUNCTION_PARAMETERS)
720720
if (Z_TYPE_P(pg_rows) == IS_ARRAY) {
721721
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(pg_rows), tmp) {
722722
if (!_pdo_pgsql_send_copy_data(H, tmp)) {
723+
if (EG(exception)) {
724+
RETURN_THROWS();
725+
}
723726
pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
724727
PDO_HANDLE_DBH_ERR();
725728
RETURN_FALSE;
@@ -742,6 +745,9 @@ void pgsqlCopyFromArray_internal(INTERNAL_FUNCTION_PARAMETERS)
742745
tmp = iter->funcs->get_current_data(iter);
743746
if (!_pdo_pgsql_send_copy_data(H, tmp)) {
744747
zend_iterator_dtor(iter);
748+
if (EG(exception)) {
749+
RETURN_THROWS();
750+
}
745751
pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
746752
PDO_HANDLE_DBH_ERR();
747753
RETURN_FALSE;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
--TEST--
2+
PDO PgSQL pgsqlCopyFromArray()/copyFromArray() with a non-stringable row throws without polluting errorInfo
3+
--EXTENSIONS--
4+
pdo_pgsql
5+
--SKIPIF--
6+
<?php
7+
require __DIR__ . '/config.inc';
8+
require __DIR__ . '/../../../ext/pdo/tests/pdo_test.inc';
9+
PDOTest::skip();
10+
?>
11+
--FILE--
12+
<?php
13+
require __DIR__ . '/../../../ext/pdo/tests/pdo_test.inc';
14+
$db = PDOTest::test_factory(__DIR__ . '/common.phpt');
15+
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
16+
17+
$db->exec('CREATE TABLE test_copy_non_stringable (v text)');
18+
19+
try {
20+
$db->pgsqlCopyFromArray('test_copy_non_stringable', [new stdClass()]);
21+
} catch (\Error $e) {
22+
echo $e->getMessage(), PHP_EOL;
23+
}
24+
25+
var_dump($db->errorInfo()[0]);
26+
27+
$pgsql = PDOTest::test_factory(__DIR__ . '/common.phpt', Pdo\Pgsql::class, true);
28+
$pgsql->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
29+
30+
try {
31+
$pgsql->copyFromArray('test_copy_non_stringable', [new stdClass()]);
32+
} catch (\Error $e) {
33+
echo $e->getMessage(), PHP_EOL;
34+
}
35+
36+
var_dump($pgsql->errorInfo()[0]);
37+
?>
38+
--CLEAN--
39+
<?php
40+
require __DIR__ . '/../../../ext/pdo/tests/pdo_test.inc';
41+
$db = PDOTest::test_factory(__DIR__ . '/common.phpt');
42+
$db->query('DROP TABLE IF EXISTS test_copy_non_stringable CASCADE');
43+
?>
44+
--EXPECTF--
45+
Deprecated: Method PDO::pgsqlCopyFromArray() is deprecated since 8.5, use Pdo\Pgsql::copyFromArray() instead in %s on line %d
46+
Object of class stdClass could not be converted to string
47+
string(5) "00000"
48+
Object of class stdClass could not be converted to string
49+
string(5) "00000"

0 commit comments

Comments
 (0)