Skip to content

Commit c17001a

Browse files
committed
PHP-1295: Memory leak when generating ID for inserts
1 parent bccdd27 commit c17001a

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/MongoDB/WriteBatch.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,11 @@ PHP_METHOD(WriteBatch, insert)
110110

111111
if (bson_iter_init_find(&iter, bson_out, "_id")) {
112112
php_phongo_objectid_new_from_oid(return_value, bson_iter_oid(&iter) TSRMLS_CC);
113+
bson_clear(&bson_out);
113114
return;
114115
}
116+
117+
bson_clear(&bson_out);
115118
}
116119
}
117120
/* }}} */

tests/batch/write-0002.phpt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
--TEST--
2+
MongoDB\Write\Batch: #002 Get the generated ID
3+
--SKIPIF--
4+
<?php require "tests/utils/basic-skipif.inc"?>
5+
--FILE--
6+
<?php
7+
require_once "tests/utils/basic.inc";
8+
9+
$mm = new MongoDB\Manager(MONGODB_URI);
10+
11+
$hannes = array("name" => "Hannes", "country" => "USA", "gender" => "male");
12+
$hayley = array("name" => "Hayley", "country" => "USA", "gender" => "female");
13+
$ordered = true;
14+
15+
$insertBatch = new \MongoDB\WriteBatch($ordered);
16+
$hannes_id = $insertBatch->insert($hannes);
17+
$hayley_id = $insertBatch->insert($hayley);
18+
19+
$w = 1;
20+
$wtimeout = 1000;
21+
$writeConcern = new \MongoDB\WriteConcern($w, $wtimeout);
22+
$result = $mm->executeWriteBatch("db.collection", $insertBatch, $writeConcern);
23+
24+
assert($result instanceof \MongoDB\WriteResult);
25+
26+
printf(
27+
"Inserted %d documents to %s\n",
28+
$result->getNumInserted(),
29+
$result->getServer()->getHost()
30+
);
31+
printf("hannes: %s\nhayley: %s\n", $hannes_id, $hayley_id);
32+
?>
33+
===DONE===
34+
<?php exit(0); ?>
35+
--EXPECTF--
36+
Inserted 2 documents to localhost
37+
hannes: %s
38+
hayley: %s
39+
===DONE===

0 commit comments

Comments
 (0)