Skip to content

Commit 3f8f974

Browse files
committed
Merge pull request #297
2 parents 84f7251 + 5995d4d commit 3f8f974

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

src/BSON/ObjectID.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,16 @@ PHP_METHOD(ObjectID, __construct)
6767
zend_restore_error_handling(&error_handling TSRMLS_CC);
6868

6969
if (id) {
70-
zend_str_tolower(id, id_len);
71-
if (bson_oid_is_valid(id, id_len)) {
70+
char *tid = zend_str_tolower_dup(id, id_len);
71+
if (bson_oid_is_valid(tid, id_len)) {
7272
bson_oid_t oid;
7373

74-
bson_oid_init_from_string(&oid, id);
74+
bson_oid_init_from_string(&oid, tid);
7575
bson_oid_to_string(&oid, intern->oid);
7676
} else {
7777
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "%s", "Invalid BSON ID provided");
7878
}
79+
efree(tid);
7980
} else {
8081
bson_oid_t oid;
8182

tests/bson/bug0672.phpt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
PHPC-672: ObjectID constructor should not modify string argument's memory
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
$id = '56925B7330616224D0000001';
10+
var_dump(new MongoDB\BSON\ObjectID($id));
11+
var_dump($id);
12+
13+
$invalidId = 'T123456';
14+
throws(function() use ($invalidId) {
15+
new MongoDB\BSON\ObjectID($invalidId);
16+
}, "MongoDB\\Driver\\Exception\\InvalidArgumentException");
17+
var_dump($invalidId);
18+
19+
?>
20+
===DONE===
21+
<?php exit(0); ?>
22+
--EXPECTF--
23+
object(MongoDB\BSON\ObjectID)#%d (%d) {
24+
["oid"]=>
25+
string(24) "56925b7330616224d0000001"
26+
}
27+
string(24) "56925B7330616224D0000001"
28+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
29+
string(7) "T123456"
30+
===DONE===

0 commit comments

Comments
 (0)