Skip to content

PHPC-1748 Fix wrong return value of Cursor::key when iterator is not valid #1192

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/MongoDB/Cursor.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ static int php_phongo_cursor_valid(php_phongo_cursor_t* cursor) /* {{{ */

static void php_phongo_cursor_get_current_key(php_phongo_cursor_t* cursor, zval* key) /* {{{ */
{
if (Z_ISUNDEF(cursor->visitor_data.zchild)) {
ZVAL_NULL(key);
return;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you introduced the Iterator methods, it looks like php_phongo_cursor_get_current_key and php_phongo_cursor_get_current_data are only called from key() and current(), respectively. Can we just delete these two functions?

That would allow you to move this logic into key(), which would be consistent with what you did for current() in 70a097e.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I can refactor this 👍

ZVAL_LONG(key, cursor->current);
} /* }}} */

Expand Down
43 changes: 43 additions & 0 deletions tests/cursor/cursor-iterator-003.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
--TEST--
MongoDB\Driver\Cursor handles invalid positions gracefully
--SKIPIF--
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
<?php skip_if_not_live(); ?>
<?php skip_if_not_clean(); ?>
--FILE--
<?php
require_once __DIR__ . "/../utils/basic.inc";

$manager = new MongoDB\Driver\Manager(URI);

$bulkWrite = new MongoDB\Driver\BulkWrite;
$bulkWrite->insert(array('_id' => 0));

$writeResult = $manager->executeBulkWrite(NS, $bulkWrite);

$cursor = $manager->executeQuery(NS, new MongoDB\Driver\Query(array()));

$cursor->rewind();
var_dump($cursor->valid());
var_dump($cursor->key());
var_dump($cursor->current());

$cursor->next();
var_dump($cursor->valid());
var_dump($cursor->key());
var_dump($cursor->current());

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
bool(true)
int(0)
object(stdClass)#%d (1) {
["_id"]=>
int(0)
}
bool(false)
NULL
NULL
===DONE===