From 2a1692e909ca483ff7ce821c4157b12745d65497 Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Mon, 7 Dec 2020 17:12:39 +0100 Subject: [PATCH 1/2] Ensure Cursor::current returns null on invalid positions --- src/MongoDB/Cursor.c | 6 +++++- tests/cursor/bug1713-001.phpt | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 tests/cursor/bug1713-001.phpt diff --git a/src/MongoDB/Cursor.c b/src/MongoDB/Cursor.c index 78ccfb7bd..eb2f50e58 100644 --- a/src/MongoDB/Cursor.c +++ b/src/MongoDB/Cursor.c @@ -312,7 +312,11 @@ PHP_METHOD(Cursor, current) data = php_phongo_cursor_get_current_data(intern); if (data) { - ZVAL_COPY_DEREF(return_value, data); + if (Z_ISUNDEF_P(data)) { + RETURN_NULL(); + } else { + ZVAL_COPY_DEREF(return_value, data); + } } } diff --git a/tests/cursor/bug1713-001.phpt b/tests/cursor/bug1713-001.phpt new file mode 100644 index 000000000..8b1a3bbb4 --- /dev/null +++ b/tests/cursor/bug1713-001.phpt @@ -0,0 +1,24 @@ +--TEST-- +MongoDB\Driver\Cursor PHPC-171: The cursor current method does not return anything +--SKIPIF-- + + + +--FILE-- +executeQuery(NS, new MongoDB\Driver\Query([])); + +var_dump($cursor->valid()); +var_dump($cursor->current()); + +?> +===DONE=== + +--EXPECTF-- +bool(false) +NULL +===DONE=== From fb916b5ad1a15b4e3d069958470dbe32d10802b6 Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Mon, 21 Dec 2020 15:47:16 +0100 Subject: [PATCH 2/2] Addressed code review feedback --- src/MongoDB/Cursor.c | 10 ++++------ tests/cursor/bug1713-001.phpt | 3 +-- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/MongoDB/Cursor.c b/src/MongoDB/Cursor.c index eb2f50e58..354ec3b06 100644 --- a/src/MongoDB/Cursor.c +++ b/src/MongoDB/Cursor.c @@ -311,12 +311,10 @@ PHP_METHOD(Cursor, current) data = php_phongo_cursor_get_current_data(intern); - if (data) { - if (Z_ISUNDEF_P(data)) { - RETURN_NULL(); - } else { - ZVAL_COPY_DEREF(return_value, data); - } + if (Z_ISUNDEF_P(data)) { + RETURN_NULL(); + } else { + ZVAL_COPY_DEREF(return_value, data); } } diff --git a/tests/cursor/bug1713-001.phpt b/tests/cursor/bug1713-001.phpt index 8b1a3bbb4..38f1882cf 100644 --- a/tests/cursor/bug1713-001.phpt +++ b/tests/cursor/bug1713-001.phpt @@ -1,9 +1,8 @@ --TEST-- -MongoDB\Driver\Cursor PHPC-171: The cursor current method does not return anything +PHPC-1713: MongoDB\Driver\Cursor::current() does not return anything --SKIPIF-- - --FILE--