Skip to content

Commit e126ca1

Browse files
morozovadambaratz
authored andcommitted
Check column number before trying to fetch the value
1 parent a1aaec0 commit e126ca1

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ PHP NEWS
1515
. Fixed bug #77200 (imagecropauto(…, GD_CROP_SIDES) crops left but not right).
1616
(cmb)
1717

18+
- PDO:
19+
. Handle invalid index passed to PDOStatement::fetchColumn() as error. (Sergei
20+
Morozov)
21+
1822
- Sockets:
1923
. Fixed bug #77136 (Unsupported IPV6_RECVPKTINFO constants on macOS).
2024
(Mizunashi Mana)

ext/pdo/pdo_stmt.c

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

535+
if (colno < 0 || colno >= stmt->column_count) {
536+
pdo_raise_impl_error(stmt->dbh, stmt, "HY000", "Invalid column index");
537+
ZVAL_FALSE(dest);
538+
539+
return;
540+
}
541+
535542
col = &stmt->columns[colno];
536543
type = PDO_PARAM_TYPE(col->param_type);
537544
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)