]> granicus.if.org Git - php/commitdiff
Fix #79664: PDOStatement::getColumnMeta fails on empty result set
authorChristoph M. Becker <cmbecker69@gmx.de>
Tue, 2 Jun 2020 07:36:39 +0000 (09:36 +0200)
committerChristoph M. Becker <cmbecker69@gmx.de>
Tue, 2 Jun 2020 08:45:47 +0000 (10:45 +0200)
As its name suggests, `sqlite3_data_count` returns the number of
columns in the current row of the result set; we are interested in the
number of columns regardless of the current row, so we have to use
`sqlite3_column_count` instead.

NEWS
ext/pdo_sqlite/sqlite_statement.c
ext/pdo_sqlite/tests/bug79664.phpt [new file with mode: 0644]

diff --git a/NEWS b/NEWS
index e73dbc58671953b7e370e1bc028e3a586d5b16cf..e8bab176df4cf868e197b07548002d13c421ab23 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -5,6 +5,10 @@ PHP                                                                        NEWS
 - Core:
   . Fixed bug #79650 (php-win.exe 100% cpu lockup). (cmb)
 
+- PDO SQLite:
+  . Fixed bug #79664 (PDOStatement::getColumnMeta fails on empty result set).
+    (cmb)
+
 ?? ??? ????, PHP 7.3.19
 
 - Core:
index c51ab539566e2a2456d79619280a122a5c003882..5aae9a073b73dc26b355ac4d5dc6969a8cdd5e5f 100644 (file)
@@ -337,7 +337,7 @@ static int pdo_sqlite_stmt_col_meta(pdo_stmt_t *stmt, zend_long colno, zval *ret
        if (!S->stmt) {
                return FAILURE;
        }
-       if(colno >= sqlite3_data_count(S->stmt)) {
+       if(colno >= sqlite3_column_count(S->stmt)) {
                /* error invalid column */
                pdo_sqlite_error_stmt(stmt);
                return FAILURE;
diff --git a/ext/pdo_sqlite/tests/bug79664.phpt b/ext/pdo_sqlite/tests/bug79664.phpt
new file mode 100644 (file)
index 0000000..072d107
--- /dev/null
@@ -0,0 +1,32 @@
+--TEST--
+Bug #79664 (PDOStatement::getColumnMeta fails on empty result set)
+--SKIPIF--
+<?php
+if (!extension_loaded('pdo_sqlite')) print 'skip not loaded';
+?>
+--FILE--
+<?php
+$pdo = new PDO('sqlite::memory:', null, null, [
+       PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
+]);
+$stmt = $pdo->query('select 1 where 0');
+if ($stmt->columnCount()) {
+    var_dump($stmt->getColumnMeta(0));
+}
+?>
+--EXPECT--
+array(6) {
+  ["native_type"]=>
+  string(4) "null"
+  ["flags"]=>
+  array(0) {
+  }
+  ["name"]=>
+  string(1) "1"
+  ["len"]=>
+  int(4294967295)
+  ["precision"]=>
+  int(0)
+  ["pdo_type"]=>
+  int(2)
+}