From 37b8eaffd3eca2d8995351c275c76cac440eed05 Mon Sep 17 00:00:00 2001 From: Jeremy Mikola Date: Mon, 8 Jan 2018 21:52:08 -0500 Subject: [PATCH] PHPC-1092: Don't reset hash on successive get_properties calls This removes a call to zend_hash_clean() on successive calls to a BSON object's get_properties handler, which can lead to an infinite loop when iterating the object's properties with foreach on PHP 7.x. For historical reference, the original call to zend_hash_clean() was introduced in ad1f3f049f5c101a8b4f89f13e6384bbefb6ef6c for #607 (PHPC-939). --- php_phongo.h | 1 - .../bson/bson-binary-get_properties-001.phpt | 20 +++++++++++ .../bson/bson-binary-get_properties-002.phpt | 21 ++++++++++++ .../bson-dbpointer-get_properties-001.phpt | 21 ++++++++++++ .../bson-dbpointer-get_properties-002.phpt | 22 ++++++++++++ .../bson-decimal128-get_properties-001.phpt | 20 +++++++++++ .../bson-decimal128-get_properties-002.phpt | 21 ++++++++++++ .../bson-javascript-get_properties-001.phpt | 34 +++++++++++++++++++ .../bson-javascript-get_properties-002.phpt | 33 ++++++++++++++++++ .../bson-objectid-get_properties-001.phpt | 18 ++++++++++ .../bson-objectid-get_properties-002.phpt | 19 +++++++++++ tests/bson/bson-regex-get_properties-001.phpt | 20 +++++++++++ tests/bson/bson-regex-get_properties-002.phpt | 21 ++++++++++++ .../bson/bson-symbol-get_properties-001.phpt | 19 +++++++++++ .../bson/bson-symbol-get_properties-002.phpt | 20 +++++++++++ .../bson-timestamp-get_properties-001.phpt | 20 +++++++++++ .../bson-timestamp-get_properties-002.phpt | 21 ++++++++++++ .../bson-utcdatetime-get_properties-001.phpt | 18 ++++++++++ .../bson-utcdatetime-get_properties-002.phpt | 19 +++++++++++ 19 files changed, 387 insertions(+), 1 deletion(-) create mode 100644 tests/bson/bson-binary-get_properties-001.phpt create mode 100644 tests/bson/bson-binary-get_properties-002.phpt create mode 100644 tests/bson/bson-dbpointer-get_properties-001.phpt create mode 100644 tests/bson/bson-dbpointer-get_properties-002.phpt create mode 100644 tests/bson/bson-decimal128-get_properties-001.phpt create mode 100644 tests/bson/bson-decimal128-get_properties-002.phpt create mode 100644 tests/bson/bson-javascript-get_properties-001.phpt create mode 100644 tests/bson/bson-javascript-get_properties-002.phpt create mode 100644 tests/bson/bson-objectid-get_properties-001.phpt create mode 100644 tests/bson/bson-objectid-get_properties-002.phpt create mode 100644 tests/bson/bson-regex-get_properties-001.phpt create mode 100644 tests/bson/bson-regex-get_properties-002.phpt create mode 100644 tests/bson/bson-symbol-get_properties-001.phpt create mode 100644 tests/bson/bson-symbol-get_properties-002.phpt create mode 100644 tests/bson/bson-timestamp-get_properties-001.phpt create mode 100644 tests/bson/bson-timestamp-get_properties-002.phpt create mode 100644 tests/bson/bson-utcdatetime-get_properties-001.phpt create mode 100644 tests/bson/bson-utcdatetime-get_properties-002.phpt diff --git a/php_phongo.h b/php_phongo.h index 399d9e782..e95901c7e 100644 --- a/php_phongo.h +++ b/php_phongo.h @@ -196,7 +196,6 @@ zend_bool phongo_writeconcernerror_init(zval *return_value, bson_t *bson TSRMLS_ ALLOC_HASHTABLE(props); \ zend_hash_init((props), (size), NULL, ZVAL_PTR_DTOR, 0); \ } else if ((intern)->properties) { \ - zend_hash_clean((intern)->properties); \ (props) = (intern)->properties; \ } else { \ ALLOC_HASHTABLE(props); \ diff --git a/tests/bson/bson-binary-get_properties-001.phpt b/tests/bson/bson-binary-get_properties-001.phpt new file mode 100644 index 000000000..776b378e3 --- /dev/null +++ b/tests/bson/bson-binary-get_properties-001.phpt @@ -0,0 +1,20 @@ +--TEST-- +MongoDB\BSON\Binary get_properties handler (get_object_vars) +--FILE-- + +===DONE=== + +--EXPECT-- +array(2) { + ["data"]=> + string(6) "foobar" + ["type"]=> + int(0) +} +===DONE=== diff --git a/tests/bson/bson-binary-get_properties-002.phpt b/tests/bson/bson-binary-get_properties-002.phpt new file mode 100644 index 000000000..2a6e7bbe6 --- /dev/null +++ b/tests/bson/bson-binary-get_properties-002.phpt @@ -0,0 +1,21 @@ +--TEST-- +MongoDB\BSON\Binary get_properties handler (foreach) +--FILE-- + $value) { + var_dump($key); + var_dump($value); +} + +?> +===DONE=== + +--EXPECT-- +string(4) "data" +string(6) "foobar" +string(4) "type" +int(0) +===DONE=== diff --git a/tests/bson/bson-dbpointer-get_properties-001.phpt b/tests/bson/bson-dbpointer-get_properties-001.phpt new file mode 100644 index 000000000..c790222dc --- /dev/null +++ b/tests/bson/bson-dbpointer-get_properties-001.phpt @@ -0,0 +1,21 @@ +--TEST-- +MongoDB\BSON\DBPointer get_properties handler (get_object_vars) +--FILE-- +dbptr; + +var_dump(get_object_vars($dbptr)); + +?> +===DONE=== + +--EXPECT-- +array(2) { + ["ref"]=> + string(11) "phongo.test" + ["id"]=> + string(24) "5a2e78accd485d55b405ac12" +} +===DONE=== diff --git a/tests/bson/bson-dbpointer-get_properties-002.phpt b/tests/bson/bson-dbpointer-get_properties-002.phpt new file mode 100644 index 000000000..fad60d957 --- /dev/null +++ b/tests/bson/bson-dbpointer-get_properties-002.phpt @@ -0,0 +1,22 @@ +--TEST-- +MongoDB\BSON\DBPointer get_properties handler (foreach) +--FILE-- +dbptr; + +foreach ($dbptr as $key => $value) { + var_dump($key); + var_dump($value); +} + +?> +===DONE=== + +--EXPECT-- +string(3) "ref" +string(11) "phongo.test" +string(2) "id" +string(24) "5a2e78accd485d55b405ac12" +===DONE=== diff --git a/tests/bson/bson-decimal128-get_properties-001.phpt b/tests/bson/bson-decimal128-get_properties-001.phpt new file mode 100644 index 000000000..3155a94d1 --- /dev/null +++ b/tests/bson/bson-decimal128-get_properties-001.phpt @@ -0,0 +1,20 @@ +--TEST-- +MongoDB\BSON\Decimal128 get_properties handler (get_object_vars) +--SKIPIF-- + +--FILE-- + +===DONE=== + +--EXPECT-- +array(1) { + ["dec"]=> + string(9) "1234.5678" +} +===DONE=== diff --git a/tests/bson/bson-decimal128-get_properties-002.phpt b/tests/bson/bson-decimal128-get_properties-002.phpt new file mode 100644 index 000000000..e1f7ce9ea --- /dev/null +++ b/tests/bson/bson-decimal128-get_properties-002.phpt @@ -0,0 +1,21 @@ +--TEST-- +MongoDB\BSON\Decimal128 get_properties handler (foreach) +--SKIPIF-- + +--FILE-- + $value) { + var_dump($key); + var_dump($value); +} + +?> +===DONE=== + +--EXPECT-- +string(3) "dec" +string(9) "1234.5678" +===DONE=== diff --git a/tests/bson/bson-javascript-get_properties-001.phpt b/tests/bson/bson-javascript-get_properties-001.phpt new file mode 100644 index 000000000..f37c22a5e --- /dev/null +++ b/tests/bson/bson-javascript-get_properties-001.phpt @@ -0,0 +1,34 @@ +--TEST-- +MongoDB\BSON\Javascript get_properties handler (get_object_vars) +--FILE-- + 42]), +]; + +foreach ($tests as $test) { + var_dump(get_object_vars($test)); +} + +?> +===DONE=== + +--EXPECTF-- +array(2) { + ["code"]=> + string(33) "function foo(bar) { return bar; }" + ["scope"]=> + NULL +} +array(2) { + ["code"]=> + string(30) "function foo() { return bar; }" + ["scope"]=> + object(stdClass)#%d (%d) { + ["bar"]=> + int(42) + } +} +===DONE=== diff --git a/tests/bson/bson-javascript-get_properties-002.phpt b/tests/bson/bson-javascript-get_properties-002.phpt new file mode 100644 index 000000000..84e5196ea --- /dev/null +++ b/tests/bson/bson-javascript-get_properties-002.phpt @@ -0,0 +1,33 @@ +--TEST-- +MongoDB\BSON\Javascript get_properties handler (foreach) +--FILE-- + 42]), +]; + +foreach ($tests as $test) { + foreach ($test as $key => $value) { + var_dump($key); + var_dump($value); + } +} + +?> +===DONE=== + +--EXPECTF-- +string(4) "code" +string(33) "function foo(bar) { return bar; }" +string(5) "scope" +NULL +string(4) "code" +string(30) "function foo() { return bar; }" +string(5) "scope" +object(stdClass)#%d (%d) { + ["bar"]=> + int(42) +} +===DONE=== diff --git a/tests/bson/bson-objectid-get_properties-001.phpt b/tests/bson/bson-objectid-get_properties-001.phpt new file mode 100644 index 000000000..b201c5408 --- /dev/null +++ b/tests/bson/bson-objectid-get_properties-001.phpt @@ -0,0 +1,18 @@ +--TEST-- +MongoDB\BSON\ObjectId get_properties handler (get_object_vars) +--FILE-- + +===DONE=== + +--EXPECT-- +array(1) { + ["oid"]=> + string(24) "53e2a1c40640fd72175d4603" +} +===DONE=== diff --git a/tests/bson/bson-objectid-get_properties-002.phpt b/tests/bson/bson-objectid-get_properties-002.phpt new file mode 100644 index 000000000..04ae6ea48 --- /dev/null +++ b/tests/bson/bson-objectid-get_properties-002.phpt @@ -0,0 +1,19 @@ +--TEST-- +MongoDB\BSON\ObjectId get_properties handler (foreach) +--FILE-- + $value) { + var_dump($key); + var_dump($value); +} + +?> +===DONE=== + +--EXPECT-- +string(3) "oid" +string(24) "53e2a1c40640fd72175d4603" +===DONE=== diff --git a/tests/bson/bson-regex-get_properties-001.phpt b/tests/bson/bson-regex-get_properties-001.phpt new file mode 100644 index 000000000..8f4973364 --- /dev/null +++ b/tests/bson/bson-regex-get_properties-001.phpt @@ -0,0 +1,20 @@ +--TEST-- +MongoDB\BSON\Regex get_properties handler (get_object_vars) +--FILE-- + +===DONE=== + +--EXPECT-- +array(2) { + ["pattern"]=> + string(6) "regexp" + ["flags"]=> + string(1) "i" +} +===DONE=== diff --git a/tests/bson/bson-regex-get_properties-002.phpt b/tests/bson/bson-regex-get_properties-002.phpt new file mode 100644 index 000000000..66b0df4c9 --- /dev/null +++ b/tests/bson/bson-regex-get_properties-002.phpt @@ -0,0 +1,21 @@ +--TEST-- +MongoDB\BSON\Regex get_properties handler (foreach) +--FILE-- + $value) { + var_dump($key); + var_dump($value); +} + +?> +===DONE=== + +--EXPECT-- +string(7) "pattern" +string(6) "regexp" +string(5) "flags" +string(1) "i" +===DONE=== diff --git a/tests/bson/bson-symbol-get_properties-001.phpt b/tests/bson/bson-symbol-get_properties-001.phpt new file mode 100644 index 000000000..24a306860 --- /dev/null +++ b/tests/bson/bson-symbol-get_properties-001.phpt @@ -0,0 +1,19 @@ +--TEST-- +MongoDB\BSON\Symbol get_properties handler (get_object_vars) +--FILE-- +symbol; + +var_dump(get_object_vars($symbol)); + +?> +===DONE=== + +--EXPECT-- +array(1) { + ["symbol"]=> + string(4) "test" +} +===DONE=== diff --git a/tests/bson/bson-symbol-get_properties-002.phpt b/tests/bson/bson-symbol-get_properties-002.phpt new file mode 100644 index 000000000..f55ef70a3 --- /dev/null +++ b/tests/bson/bson-symbol-get_properties-002.phpt @@ -0,0 +1,20 @@ +--TEST-- +MongoDB\BSON\Symbol get_properties handler (foreach) +--FILE-- +symbol; + +foreach ($symbol as $key => $value) { + var_dump($key); + var_dump($value); +} + +?> +===DONE=== + +--EXPECT-- +string(6) "symbol" +string(4) "test" +===DONE=== diff --git a/tests/bson/bson-timestamp-get_properties-001.phpt b/tests/bson/bson-timestamp-get_properties-001.phpt new file mode 100644 index 000000000..65e0b0558 --- /dev/null +++ b/tests/bson/bson-timestamp-get_properties-001.phpt @@ -0,0 +1,20 @@ +--TEST-- +MongoDB\BSON\Timestamp get_properties handler (get_object_vars) +--FILE-- + +===DONE=== + +--EXPECT-- +array(2) { + ["increment"]=> + string(4) "1234" + ["timestamp"]=> + string(4) "5678" +} +===DONE=== diff --git a/tests/bson/bson-timestamp-get_properties-002.phpt b/tests/bson/bson-timestamp-get_properties-002.phpt new file mode 100644 index 000000000..321fa713e --- /dev/null +++ b/tests/bson/bson-timestamp-get_properties-002.phpt @@ -0,0 +1,21 @@ +--TEST-- +MongoDB\BSON\Timestamp get_properties handler (foreach) +--FILE-- + $value) { + var_dump($key); + var_dump($value); +} + +?> +===DONE=== + +--EXPECT-- +string(9) "increment" +string(4) "1234" +string(9) "timestamp" +string(4) "5678" +===DONE=== diff --git a/tests/bson/bson-utcdatetime-get_properties-001.phpt b/tests/bson/bson-utcdatetime-get_properties-001.phpt new file mode 100644 index 000000000..ab4dd66d9 --- /dev/null +++ b/tests/bson/bson-utcdatetime-get_properties-001.phpt @@ -0,0 +1,18 @@ +--TEST-- +MongoDB\BSON\UTCDateTime get_properties handler (get_object_vars) +--FILE-- + +===DONE=== + +--EXPECT-- +array(1) { + ["milliseconds"]=> + string(13) "1416445411987" +} +===DONE=== diff --git a/tests/bson/bson-utcdatetime-get_properties-002.phpt b/tests/bson/bson-utcdatetime-get_properties-002.phpt new file mode 100644 index 000000000..bcb4aee85 --- /dev/null +++ b/tests/bson/bson-utcdatetime-get_properties-002.phpt @@ -0,0 +1,19 @@ +--TEST-- +MongoDB\BSON\UTCDateTime get_properties handler (foreach) +--FILE-- + $value) { + var_dump($key); + var_dump($value); +} + +?> +===DONE=== + +--EXPECT-- +string(12) "milliseconds" +string(13) "1416445411987" +===DONE===