Skip to content

Commit d64698a

Browse files
committed
PHPC-265: BSON encoding unsupoprted types (Resource) should fail
1 parent ebc15ae commit d64698a

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

src/bson.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -723,8 +723,7 @@ void phongo_bson_append(bson_t *bson, php_phongo_bson_flags_t flags, const char
723723
if (bson_utf8_validate(Z_STRVAL_P(entry), Z_STRLEN_P(entry), true)) {
724724
bson_append_utf8(bson, key, key_len, Z_STRVAL_P(entry), Z_STRLEN_P(entry));
725725
} else {
726-
/* FIXME: Broken Broken Broken */
727-
printf("BROKEN BROKEN BROKEN UTF8\n");
726+
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "Got invalid UTF-8 value serializing '%s'", Z_STRVAL_P(entry));
728727
}
729728
break;
730729

@@ -752,8 +751,7 @@ void phongo_bson_append(bson_t *bson, php_phongo_bson_flags_t flags, const char
752751
break;
753752

754753
default:
755-
/* FIXME: Resource? */
756-
printf("I DON'T SUPPORT THAT TYPE!\n");
754+
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "Got unsupported type '%s'", zend_get_type_by_const(entry_type));
757755
}
758756
}
759757

tests/bson/bson-unknown-001.phpt

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
--TEST--
2+
BSON Serializing a PHP resource should throw exception
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
try {
10+
$a = array("stderr" => STDERR);
11+
12+
$b = BSON\fromArray($a);
13+
} catch(MongoDB\Driver\UnexpectedValueException $e) {
14+
echo $e->getMessage(), "\n";
15+
}
16+
var_dump($a);
17+
18+
try {
19+
$a = array("stderr" => STDERR, "stdout" => STDOUT);
20+
21+
$b = BSON\fromArray($a);
22+
} catch(MongoDB\Driver\UnexpectedValueException $e) {
23+
echo $e->getMessage(), "\n";
24+
}
25+
var_dump($a);
26+
27+
28+
?>
29+
===DONE===
30+
<?php exit(0); ?>
31+
--EXPECTF--
32+
Got unsupported type 'resource'
33+
array(1) {
34+
["stderr"]=>
35+
resource(3) of type (stream)
36+
}
37+
Got unsupported type 'resource'
38+
array(2) {
39+
["stderr"]=>
40+
resource(3) of type (stream)
41+
["stdout"]=>
42+
resource(2) of type (stream)
43+
}
44+
===DONE===

0 commit comments

Comments
 (0)