Skip to content

Commit e3cfa4b

Browse files
committed
ext/pdo_sqlite: PDO::sqliteCreateCollection return type strenghtening.
Is supposed to be Pdo_Sqlite::createCollation but behavior differs in regard of return type checks. close GH-18799
1 parent eac91d0 commit e3cfa4b

File tree

5 files changed

+23
-6
lines changed

5 files changed

+23
-6
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ PHP NEWS
141141
- PDO_SQLITE:
142142
. throw on null bytes / resolve GH-13952 (divinity76).
143143
. Implement GH-17321: Add setAuthorizer to Pdo\Sqlite. (nielsdos)
144+
. PDO::sqliteCreateCollation now throws a TypeError if the callback
145+
has a wrong return type. (David Carlier)
144146

145147
- PGSQL:
146148
. Added pg_close_stmt to close a prepared statement while allowing

UPGRADING

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,9 @@ PHP 8.5 UPGRADE NOTES
268268
- PDO_SQLITE:
269269
. SQLite PDO::quote() will now throw an exception or emit a warning,
270270
depending on the error mode, if the string contains a null byte.
271+
. PDO::sqliteCreateCollation will now throw an exception
272+
if the callback has the wrong return type, making it more
273+
in line with Pdo_Sqlite::createCollation behavior.
271274

272275
- PGSQL:
273276
. pg_copy_from also supports inputs as Iterable.

ext/pdo_sqlite/pdo_sqlite.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,14 +385,14 @@ static int php_sqlite_collation_callback(void *context, int string1_len, const v
385385
zend_type_error("%s(): Return value of the collation callback must be of type int, %s returned",
386386
ZSTR_VAL(func_name), zend_zval_value_name(&retval));
387387
zend_string_release(func_name);
388-
zval_ptr_dtor(&retval);
389-
return FAILURE;
388+
ret = FAILURE;
390389
}
391390
if (Z_LVAL(retval) > 0) {
392391
ret = 1;
393392
} else if (Z_LVAL(retval) < 0) {
394393
ret = -1;
395394
}
395+
zval_ptr_dtor(&retval);
396396
}
397397

398398
return ret;

ext/pdo_sqlite/sqlite_driver.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -483,9 +483,16 @@ static int php_sqlite3_collation_callback(void *context, int string1_len, const
483483

484484
zend_call_known_fcc(&collation->callback, &retval, /* argc */ 2, zargs, /* named_params */ NULL);
485485

486+
zval_ptr_dtor(&zargs[0]);
487+
zval_ptr_dtor(&zargs[1]);
488+
486489
if (!Z_ISUNDEF(retval)) {
487490
if (Z_TYPE(retval) != IS_LONG) {
488-
convert_to_long(&retval);
491+
zend_string *func_name = get_active_function_or_method_name();
492+
zend_type_error("%s(): Return value of the collation callback must be of type int, %s returned",
493+
ZSTR_VAL(func_name), zend_zval_value_name(&retval));
494+
zend_string_release(func_name);
495+
ret = FAILURE;
489496
}
490497
if (Z_LVAL(retval) > 0) {
491498
ret = 1;
@@ -495,9 +502,6 @@ static int php_sqlite3_collation_callback(void *context, int string1_len, const
495502
zval_ptr_dtor(&retval);
496503
}
497504

498-
zval_ptr_dtor(&zargs[0]);
499-
zval_ptr_dtor(&zargs[1]);
500-
501505
return ret;
502506
}
503507

ext/pdo_sqlite/tests/pdo_sqlite_createcollation.phpt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ foreach ($result as $row) {
2424
echo $row['name'] . "\n";
2525
}
2626

27+
$db->sqliteCreateCollation('MYCOLLATEBAD', function($a, $b) { return $a; });
28+
29+
try {
30+
$db->query('SELECT name FROM test_pdo_sqlite_createcollation ORDER BY name COLLATE MYCOLLATEBAD');
31+
} catch (\TypeError $e) {
32+
echo $e->getMessage(), PHP_EOL;
33+
}
2734
?>
2835
--EXPECT--
2936
1
@@ -32,3 +39,4 @@ foreach ($result as $row) {
3239
1
3340
10
3441
2
42+
PDO::query(): Return value of the collation callback must be of type int, string returned

0 commit comments

Comments
 (0)