Skip to content

PHPC-334: Don't produce BSON with multiple __pclass fields #61

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 1 commit into from
Jul 22, 2015
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
2 changes: 2 additions & 0 deletions src/bson.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
/* PHP Core stuff */
#include <php.h>
#include <ext/spl/spl_array.h>
#include <Zend/zend_hash.h>
#include <Zend/zend_interfaces.h>

/* PHP array helpers */
Expand Down Expand Up @@ -789,6 +790,7 @@ PHONGO_API void zval_to_bson(zval *data, php_phongo_bson_flags_t flags, bson_t *
if (instanceof_function(Z_OBJCE_P(data), php_phongo_persistable_ce TSRMLS_CC)) {
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));
zend_hash_del(ht_data, PHONGO_ODM_FIELD_NAME, sizeof(PHONGO_ODM_FIELD_NAME));
}
}

Expand Down
31 changes: 31 additions & 0 deletions tests/bson/bug0334.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
--TEST--
PHPC-334: Injected __pclass should override a __pclass key in bsonSerialize() return value
--SKIPIF--
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
--FILE--
<?php
use MongoDB\BSON as BSON;

require_once __DIR__ . "/../utils/basic.inc";

class MyClass implements BSON\Persistable {
function bsonSerialize() {
return array(
"foo" => "bar",
"__pclass" => "baz",
);
}
function bsonUnserialize(array $data) {
}
}

hex_dump(fromPHP(new MyClass));

?>
===DONE===
<?php exit(0); ?>
--EXPECT--
0 : 28 00 00 00 05 5f 5f 70 63 6c 61 73 73 00 07 00 [(....__pclass...]
10 : 00 00 80 4d 79 43 6c 61 73 73 02 66 6f 6f 00 04 [...MyClass.foo..]
20 : 00 00 00 62 61 72 00 00 [...bar..]
===DONE===