Skip to content

Commit b3ff8b6

Browse files
committed
PHPC-1748 Fix wrong return value of Cursor::key when iterator is not valid
1 parent 70a097e commit b3ff8b6

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/MongoDB/Cursor.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ static int php_phongo_cursor_valid(php_phongo_cursor_t* cursor) /* {{{ */
6464

6565
static void php_phongo_cursor_get_current_key(php_phongo_cursor_t* cursor, zval* key) /* {{{ */
6666
{
67+
if (Z_ISUNDEF(cursor->visitor_data.zchild)) {
68+
ZVAL_NULL(key);
69+
return;
70+
}
71+
6772
ZVAL_LONG(key, cursor->current);
6873
} /* }}} */
6974

tests/cursor/cursor-iterator-003.phpt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
--TEST--
2+
MongoDB\Driver\Cursor handles invalid positions gracefully
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
<?php skip_if_not_live(); ?>
6+
<?php skip_if_not_clean(); ?>
7+
--FILE--
8+
<?php
9+
require_once __DIR__ . "/../utils/basic.inc";
10+
11+
$manager = new MongoDB\Driver\Manager(URI);
12+
13+
$bulkWrite = new MongoDB\Driver\BulkWrite;
14+
$bulkWrite->insert(array('_id' => 0));
15+
16+
$writeResult = $manager->executeBulkWrite(NS, $bulkWrite);
17+
18+
$cursor = $manager->executeQuery(NS, new MongoDB\Driver\Query(array()));
19+
20+
$cursor->rewind();
21+
var_dump($cursor->valid());
22+
var_dump($cursor->key());
23+
var_dump($cursor->current());
24+
25+
$cursor->next();
26+
var_dump($cursor->valid());
27+
var_dump($cursor->key());
28+
var_dump($cursor->current());
29+
30+
?>
31+
===DONE===
32+
<?php exit(0); ?>
33+
--EXPECTF--
34+
bool(true)
35+
int(0)
36+
object(stdClass)#%d (1) {
37+
["_id"]=>
38+
int(0)
39+
}
40+
bool(false)
41+
NULL
42+
NULL
43+
===DONE===

0 commit comments

Comments
 (0)