Skip to content

Commit 74fe917

Browse files
committed
Check PDOStatement initialization during iteration
1 parent f076ab0 commit 74fe917

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

ext/pdo/pdo_stmt.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2287,15 +2287,18 @@ static const zend_object_iterator_funcs pdo_stmt_iter_funcs = {
22872287

22882288
zend_object_iterator *pdo_stmt_iter_get(zend_class_entry *ce, zval *object, int by_ref)
22892289
{
2290-
pdo_stmt_t *stmt = Z_PDO_STMT_P(object);
2291-
struct php_pdo_iterator *I;
2292-
22932290
if (by_ref) {
22942291
zend_throw_error(NULL, "An iterator cannot be used with foreach by reference");
22952292
return NULL;
22962293
}
22972294

2298-
I = ecalloc(1, sizeof(struct php_pdo_iterator));
2295+
pdo_stmt_t *stmt = Z_PDO_STMT_P(object);
2296+
if (!stmt->dbh) {
2297+
zend_throw_error(NULL, "PDO object is uninitialized");
2298+
return NULL;
2299+
}
2300+
2301+
struct php_pdo_iterator *I = ecalloc(1, sizeof(struct php_pdo_iterator));
22992302
zend_iterator_init(&I->iter);
23002303
I->iter.funcs = &pdo_stmt_iter_funcs;
23012304
Z_ADDREF_P(object);

ext/pdo/tests/pdo_uninitialized.phpt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
--TEST--
2+
Uninitialized PDO objects
3+
--SKIPIF--
4+
<?php if (!extension_loaded('pdo')) die('skip'); ?>
5+
--FILE--
6+
<?php
7+
8+
class MyPDO extends PDO {
9+
public function __construct() {}
10+
}
11+
class MyPDOStatement extends PDOStatement {
12+
public function __construct() {}
13+
}
14+
15+
$pdo = new MyPDO;
16+
try {
17+
$pdo->query("foo");
18+
} catch (Error $e) {
19+
echo $e->getMessage(), "\n";
20+
}
21+
22+
$stmt = new MyPDOStatement;
23+
try {
24+
$stmt->fetch();
25+
} catch (Error $e) {
26+
echo $e->getMessage(), "\n";
27+
}
28+
$stmt = new MyPDOStatement;
29+
try {
30+
foreach ($stmt as $row) {}
31+
} catch (Error $e) {
32+
echo $e->getMessage(), "\n";
33+
}
34+
35+
?>
36+
--EXPECT--
37+
PDO object is not initialized, constructor was not called
38+
PDO object is uninitialized
39+
PDO object is uninitialized

0 commit comments

Comments
 (0)