Skip to content

Commit 2c3b176

Browse files
committed
Merge branch 'PHP-7.3'
* PHP-7.3: Check column number before trying to fetch the value
2 parents 7cad44b + a22d285 commit 2c3b176

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

ext/pdo/pdo_stmt.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,13 @@ static inline void fetch_value(pdo_stmt_t *stmt, zval *dest, int colno, int *typ
530530
int caller_frees = 0;
531531
int type, new_type;
532532

533+
if (colno < 0 || colno >= stmt->column_count) {
534+
pdo_raise_impl_error(stmt->dbh, stmt, "HY000", "Invalid column index");
535+
ZVAL_FALSE(dest);
536+
537+
return;
538+
}
539+
533540
col = &stmt->columns[colno];
534541
type = PDO_PARAM_TYPE(col->param_type);
535542
new_type = type_override ? (int)PDO_PARAM_TYPE(*type_override) : type;

ext/pdo/tests/pdo_038.phpt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
--TEST--
2+
PDOStatement::fetchColumn() invalid column index
3+
--SKIPIF--
4+
<?php # vim:ft=php
5+
if (!extension_loaded('pdo')) die('skip');
6+
$dir = getenv('REDIR_TEST_DIR');
7+
if (false == $dir) die('skip no driver');
8+
require_once $dir . 'pdo_test.inc';
9+
PDOTest::skip();
10+
?>
11+
--FILE--
12+
<?php
13+
if (getenv('REDIR_TEST_DIR') === false) putenv('REDIR_TEST_DIR='.dirname(__FILE__) . '/../../pdo/tests/');
14+
require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc';
15+
16+
function fetchColumn($stmt, $index) {
17+
$stmt->execute();
18+
return $stmt->fetchColumn($index);
19+
}
20+
21+
$conn = PDOTest::factory();
22+
$query = 'SELECT 1';
23+
24+
switch ($conn->getAttribute(PDO::ATTR_DRIVER_NAME)) {
25+
case 'oci':
26+
$query .= ' FROM DUAL';
27+
break;
28+
case 'firebird':
29+
$query .= ' FROM RDB$DATABASE';
30+
break;
31+
}
32+
33+
$stmt = $conn->prepare($query);
34+
35+
var_dump(fetchColumn($stmt, -1));
36+
var_dump(fetchColumn($stmt, 0));
37+
var_dump(fetchColumn($stmt, 1));
38+
?>
39+
--EXPECTF--
40+
Warning: PDOStatement::fetchColumn(): SQLSTATE[HY000]: General error: Invalid column index in %s
41+
bool(false)
42+
string(1) "1"
43+
44+
Warning: PDOStatement::fetchColumn(): SQLSTATE[HY000]: General error: Invalid column index in %s
45+
bool(false)

0 commit comments

Comments
 (0)