-
Notifications
You must be signed in to change notification settings - Fork 208
PHPC-274: Fix zval_to_bson() encoding of BSON\Serializable objects #40
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -607,34 +607,43 @@ void object_to_bson(zval *object, php_phongo_bson_flags_t flags, const char *key | |
|
||
if (instanceof_function(Z_OBJCE_P(object), php_phongo_type_ce TSRMLS_CC)) { | ||
if (instanceof_function(Z_OBJCE_P(object), php_phongo_serializable_ce TSRMLS_CC)) { | ||
zval *retval = NULL; | ||
zval *obj_data = NULL; | ||
bson_t child; | ||
HashTable *tmp_ht; | ||
|
||
zend_call_method_with_0_params(&object, NULL, NULL, BSON_SERIALIZE_FUNC_NAME, &retval); | ||
if (retval) { | ||
bson_t child; | ||
HashTable *tmp_ht; | ||
zend_call_method_with_0_params(&object, NULL, NULL, BSON_SERIALIZE_FUNC_NAME, &obj_data); | ||
|
||
convert_to_array_ex(&retval); | ||
tmp_ht = HASH_OF(retval); | ||
if (!obj_data) { | ||
/* zend_call_method() failed */ | ||
return; | ||
} | ||
|
||
if (tmp_ht) { | ||
tmp_ht->nApplyCount++; | ||
} | ||
if (Z_TYPE_P(obj_data) != IS_ARRAY) { | ||
phongo_throw_exception(PHONGO_ERROR_RUNTIME TSRMLS_CC, "Expected %s() to return an array, %s given", BSON_SERIALIZE_FUNC_NAME, zend_get_type_by_const(Z_TYPE_P(obj_data))); | ||
zval_ptr_dtor(&obj_data); | ||
|
||
bson_append_document_begin(bson, key, key_len, &child); | ||
if (instanceof_function(Z_OBJCE_P(object), php_phongo_persistable_ce TSRMLS_CC)) { | ||
if (flags & PHONGO_BSON_ADD_CHILD_ODS) { | ||
bson_append_binary(&child, PHONGO_ODM_FIELD_NAME, -1, 0x80, (const uint8_t *)Z_OBJCE_P(object)->name, strlen(Z_OBJCE_P(object)->name)); | ||
} | ||
} | ||
zval_to_bson(retval, flags, &child, NULL TSRMLS_CC); | ||
bson_append_document_end(bson, &child); | ||
return; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For PHPC-275: this conditional and the preceding |
||
} | ||
|
||
if (tmp_ht) { | ||
tmp_ht->nApplyCount--; | ||
tmp_ht = HASH_OF(obj_data); | ||
|
||
if (tmp_ht) { | ||
tmp_ht->nApplyCount++; | ||
} | ||
|
||
bson_append_document_begin(bson, key, key_len, &child); | ||
if (instanceof_function(Z_OBJCE_P(object), php_phongo_persistable_ce TSRMLS_CC)) { | ||
if (flags & PHONGO_BSON_ADD_CHILD_ODS) { | ||
bson_append_binary(&child, PHONGO_ODM_FIELD_NAME, -1, 0x80, (const uint8_t *)Z_OBJCE_P(object)->name, strlen(Z_OBJCE_P(object)->name)); | ||
} | ||
zval_ptr_dtor(&retval); | ||
} | ||
zval_to_bson(obj_data, flags, &child, NULL TSRMLS_CC); | ||
bson_append_document_end(bson, &child); | ||
|
||
if (tmp_ht) { | ||
tmp_ht->nApplyCount--; | ||
} | ||
zval_ptr_dtor(&obj_data); | ||
return; | ||
} | ||
|
||
|
@@ -763,23 +772,30 @@ PHONGO_API void zval_to_bson(zval *data, php_phongo_bson_flags_t flags, bson_t * | |
|
||
switch(Z_TYPE_P(data)) { | ||
case IS_OBJECT: | ||
if (instanceof_function(Z_OBJCE_P(data), php_phongo_persistable_ce TSRMLS_CC)) { | ||
if (instanceof_function(Z_OBJCE_P(data), php_phongo_serializable_ce TSRMLS_CC)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For PHPC-274: this swap of |
||
zend_call_method_with_0_params(&data, NULL, NULL, BSON_SERIALIZE_FUNC_NAME, &obj_data); | ||
|
||
if (flags & PHONGO_BSON_ADD_ODS) { | ||
bson_append_binary(bson, PHONGO_ODM_FIELD_NAME, -1, 0x80, (const uint8_t *)Z_OBJCE_P(data)->name, strlen(Z_OBJCE_P(data)->name)); | ||
if (!obj_data) { | ||
/* zend_call_method() failed */ | ||
return; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of returning here and after an exception below, we should break and then change the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
|
||
zend_call_method_with_0_params(&data, NULL, NULL, BSON_SERIALIZE_FUNC_NAME, &obj_data); | ||
if(obj_data) { | ||
if (Z_TYPE_P(obj_data) == IS_ARRAY) { | ||
ht_data = HASH_OF(obj_data); | ||
} else { | ||
phongo_throw_exception(PHONGO_ERROR_RUNTIME TSRMLS_CC, "%s", "Return value expected to be array"); | ||
zval_ptr_dtor(&obj_data); | ||
} | ||
if (Z_TYPE_P(obj_data) != IS_ARRAY) { | ||
phongo_throw_exception(PHONGO_ERROR_RUNTIME TSRMLS_CC, "Expected %s() to return an array, %s given", BSON_SERIALIZE_FUNC_NAME, zend_get_type_by_const(Z_TYPE_P(obj_data))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should also be an UnexpectedValueException. |
||
zval_ptr_dtor(&obj_data); | ||
|
||
break; | ||
return; | ||
} | ||
|
||
ht_data = HASH_OF(obj_data); | ||
|
||
if (instanceof_function(Z_OBJCE_P(data), php_phongo_persistable_ce TSRMLS_CC)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For PHPC-274: since we swapped the |
||
if (flags & PHONGO_BSON_ADD_ODS) { | ||
bson_append_binary(bson, PHONGO_ODM_FIELD_NAME, -1, 0x80, (const uint8_t *)Z_OBJCE_P(data)->name, strlen(Z_OBJCE_P(data)->name)); | ||
} | ||
} | ||
|
||
break; | ||
} | ||
/* break intentionally omitted */ | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
--TEST-- | ||
BSON encoding error when bsonSerialize() for root document does not return an array | ||
--SKIPIF-- | ||
<?php require __DIR__ . "/../utils/basic-skipif.inc"?> | ||
--FILE-- | ||
<?php | ||
require_once __DIR__ . "/../utils/basic.inc"; | ||
|
||
class MyClass implements BSON\Serializable | ||
{ | ||
private $value; | ||
|
||
public function __construct($value) | ||
{ | ||
$this->value = $value; | ||
} | ||
|
||
public function bsonSerialize() | ||
{ | ||
return $this->value; | ||
} | ||
} | ||
|
||
$invalidValues = array(new stdClass, 'foo', 1, true); | ||
|
||
foreach ($invalidValues as $invalidValue) { | ||
try { | ||
BSON\fromArray(new MyClass($invalidValue)); | ||
} catch (MongoDB\Driver\Exception\RuntimeException $e) { | ||
echo $e->getMessage(), "\n"; | ||
} | ||
} | ||
|
||
?> | ||
===DONE=== | ||
<?php exit(0); ?> | ||
--EXPECT-- | ||
Expected bsonSerialize() to return an array, object given | ||
Expected bsonSerialize() to return an array, string given | ||
Expected bsonSerialize() to return an array, integer given | ||
Expected bsonSerialize() to return an array, boolean given | ||
===DONE=== |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
--TEST-- | ||
BSON encoding error when bsonSerialize() for embedded document does not return an array | ||
--SKIPIF-- | ||
<?php require __DIR__ . "/../utils/basic-skipif.inc"?> | ||
--FILE-- | ||
<?php | ||
require_once __DIR__ . "/../utils/basic.inc"; | ||
|
||
class MyClass implements BSON\Serializable | ||
{ | ||
private $value; | ||
|
||
public function __construct($value) | ||
{ | ||
$this->value = $value; | ||
} | ||
|
||
public function bsonSerialize() | ||
{ | ||
return $this->value; | ||
} | ||
} | ||
|
||
$invalidValues = array(new stdClass, 'foo', 1, true); | ||
|
||
foreach ($invalidValues as $invalidValue) { | ||
try { | ||
$bson = BSON\fromArray(array('embed' => new MyClass($invalidValue))); | ||
} catch (MongoDB\Driver\Exception\RuntimeException $e) { | ||
echo $e->getMessage(), "\n"; | ||
} | ||
} | ||
|
||
?> | ||
===DONE=== | ||
<?php exit(0); ?> | ||
--EXPECT-- | ||
Expected bsonSerialize() to return an array, object given | ||
Expected bsonSerialize() to return an array, string given | ||
Expected bsonSerialize() to return an array, integer given | ||
Expected bsonSerialize() to return an array, boolean given | ||
===DONE=== |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be an UnexpectedValueException.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change this to UnexpectedValueException please
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#43