Skip to content

PHPC-528: Support zval references when appending BSON #192

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 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
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
24 changes: 16 additions & 8 deletions src/bson.c
Original file line number Diff line number Diff line change
Expand Up @@ -981,9 +981,13 @@ void object_to_bson(zval *object, php_phongo_bson_flags_t flags, const char *key
zval_to_bson(object, flags, &child, NULL TSRMLS_CC);
bson_append_document_end(bson, &child);
}
void phongo_bson_append(bson_t *bson, php_phongo_bson_flags_t flags, const char *key, long key_len, int entry_type, zval *entry TSRMLS_DC)

static void phongo_bson_append(bson_t *bson, php_phongo_bson_flags_t flags, const char *key, long key_len, zval *entry TSRMLS_DC)
{
switch (entry_type)
#if PHP_VERSION_ID >= 70000
try_again:
#endif
switch (Z_TYPE_P(entry))
{
case IS_NULL:
bson_append_null(bson, key, key_len);
Expand Down Expand Up @@ -1043,12 +1047,16 @@ void phongo_bson_append(bson_t *bson, php_phongo_bson_flags_t flags, const char

#if PHP_VERSION_ID >= 70000
case IS_INDIRECT:
phongo_bson_append(bson, flags, key, key_len, Z_TYPE_P(Z_INDIRECT_P(entry)), Z_INDIRECT_P(entry) TSRMLS_DC);
phongo_bson_append(bson, flags, key, key_len, Z_INDIRECT_P(entry) TSRMLS_DC);
break;

case IS_REFERENCE:
ZVAL_DEREF(entry);
goto try_again;
#endif

default:
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "Got unsupported type %d '%s'", entry_type, zend_get_type_by_const(entry_type));
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "Got unsupported type %d '%s'", Z_TYPE_P(entry), zend_get_type_by_const(Z_TYPE_P(entry)));
}
}

Expand Down Expand Up @@ -1231,7 +1239,7 @@ PHONGO_API void zval_to_bson(zval *data, php_phongo_bson_flags_t flags, bson_t *
phongo_bson_append(bson, flags & ~PHONGO_BSON_ADD_ID,
member ? ZSTR_VAL(member) : ZSTR_VAL(key),
member ? ZSTR_LEN(member) : ZSTR_LEN(key),
Z_TYPE_P(value), value TSRMLS_CC);
value TSRMLS_CC);

if (member) {
zend_string_release(member);
Expand All @@ -1242,14 +1250,14 @@ PHONGO_API void zval_to_bson(zval *data, php_phongo_bson_flags_t flags, bson_t *
flags &= ~PHONGO_BSON_ADD_ID;
}
}
phongo_bson_append(bson, flags & ~PHONGO_BSON_ADD_ID, ZSTR_VAL(key), ZSTR_LEN(key), Z_TYPE_P(value), value TSRMLS_CC);
phongo_bson_append(bson, flags & ~PHONGO_BSON_ADD_ID, ZSTR_VAL(key), ZSTR_LEN(key), value TSRMLS_CC);
}
} else {
char numbuf[32];
const char *skey;
unsigned int skey_len = 0;
skey_len = bson_uint32_to_string(num_key, (const char **)&skey, numbuf, sizeof(numbuf));
phongo_bson_append(bson, flags & ~PHONGO_BSON_ADD_ID, skey, skey_len, Z_TYPE_P(value), value TSRMLS_CC);
phongo_bson_append(bson, flags & ~PHONGO_BSON_ADD_ID, skey, skey_len, value TSRMLS_CC);
}
} ZEND_HASH_FOREACH_END();
}
Expand Down Expand Up @@ -1296,7 +1304,7 @@ PHONGO_API void zval_to_bson(zval *data, php_phongo_bson_flags_t flags, bson_t *
} else {
key_len = bson_uint32_to_string(index, (const char **)&key, numbuf, sizeof(numbuf));
}
phongo_bson_append(bson, flags & ~PHONGO_BSON_ADD_ID, key, key_len, Z_TYPE_PP(entry), *entry TSRMLS_CC);
phongo_bson_append(bson, flags & ~PHONGO_BSON_ADD_ID, key, key_len, *entry TSRMLS_CC);
}
#endif

Expand Down
20 changes: 20 additions & 0 deletions tests/bson/bug0528.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
PHPC-528: Cannot append reference to BSON
--SKIPIF--
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
--FILE--
<?php
require_once __DIR__ . "/../utils/basic.inc";

$embedded = ['foo'];
$data = ['embedded' => &$embedded];

$bson = fromPHP($data);
echo toJson(fromPHP($data)), "\n";

?>
===DONE===
<?php exit(0); ?>
--EXPECT--
{ "embedded" : [ "foo" ] }
===DONE===