Skip to content

Commit a520c00

Browse files
committed
PHPC-1629: Check if write concern is valid during construction and unserialization
1 parent d3dd832 commit a520c00

File tree

3 files changed

+56
-4
lines changed

3 files changed

+56
-4
lines changed

src/MongoDB/WriteConcern.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,23 @@ static bool php_phongo_writeconcern_init_from_hash(php_phongo_writeconcern_t* in
8080

8181
if ((j = zend_hash_str_find(props, "j", sizeof("j") - 1))) {
8282
if (Z_TYPE_P(j) == IS_TRUE || Z_TYPE_P(j) == IS_FALSE) {
83+
if (zend_is_true(j) && (mongoc_write_concern_get_w(intern->write_concern) == MONGOC_WRITE_CONCERN_W_UNACKNOWLEDGED || mongoc_write_concern_get_w(intern->write_concern) == MONGOC_WRITE_CONCERN_W_ERRORS_IGNORED)) {
84+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Cannot enable journaling when using w = 0");
85+
goto failure;
86+
}
87+
8388
mongoc_write_concern_set_journal(intern->write_concern, zend_is_true(j));
8489
} else {
8590
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "%s initialization requires \"j\" field to be boolean", ZSTR_VAL(php_phongo_writeconcern_ce->name));
8691
goto failure;
8792
}
8893
}
8994

95+
if (!mongoc_write_concern_is_valid(intern->write_concern)) {
96+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Write concern is not valid");
97+
goto failure;
98+
}
99+
90100
return true;
91101

92102
failure:
@@ -135,11 +145,12 @@ static PHP_METHOD(WriteConcern, __construct)
135145
switch (ZEND_NUM_ARGS()) {
136146
case 3:
137147
if (Z_TYPE_P(journal) != IS_NULL) {
138-
#ifdef ZEND_ENGINE_3
148+
if (zend_is_true(journal) && (mongoc_write_concern_get_w(intern->write_concern) == MONGOC_WRITE_CONCERN_W_UNACKNOWLEDGED || mongoc_write_concern_get_w(intern->write_concern) == MONGOC_WRITE_CONCERN_W_ERRORS_IGNORED)) {
149+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Cannot enable journaling when using w = 0");
150+
return;
151+
}
152+
139153
mongoc_write_concern_set_journal(intern->write_concern, zend_is_true(journal));
140-
#else
141-
mongoc_write_concern_set_journal(intern->write_concern, Z_BVAL_P(journal));
142-
#endif
143154
}
144155
/* fallthrough */
145156
case 2:
@@ -150,6 +161,11 @@ static PHP_METHOD(WriteConcern, __construct)
150161

151162
mongoc_write_concern_set_wtimeout_int64(intern->write_concern, (int64_t) wtimeout);
152163
}
164+
165+
if (!mongoc_write_concern_is_valid(intern->write_concern)) {
166+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Write concern is not valid");
167+
return;
168+
}
153169
} /* }}} */
154170

155171
/* {{{ proto void MongoDB\BSON\WriteConcern::__set_state(array $properties)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
MongoDB\Driver\WriteConcern construction (journaling with unacknowledged w)
3+
--FILE--
4+
<?php
5+
6+
require_once __DIR__ . '/../utils/tools.php';
7+
8+
echo throws(function() {
9+
new MongoDB\Driver\WriteConcern(0, 0, true);
10+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
11+
12+
?>
13+
===DONE===
14+
<?php exit(0); ?>
15+
--EXPECT--
16+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
17+
Cannot enable journaling when using w = 0
18+
===DONE===
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
MongoDB\Driver\WriteConcern serialization errors
3+
--FILE--
4+
<?php
5+
6+
require_once __DIR__ . '/../utils/tools.php';
7+
8+
echo throws(function() {
9+
unserialize('C:27:"MongoDB\Driver\WriteConcern":30:{a:2:{s:1:"w";i:0;s:1:"j";b:1;}}');
10+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
11+
12+
?>
13+
===DONE===
14+
<?php exit(0); ?>
15+
--EXPECT--
16+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
17+
Cannot enable journaling when using w = 0
18+
===DONE===

0 commit comments

Comments
 (0)