Skip to content

Commit 3f8b851

Browse files
committed
PHPC-426: WC ctor should throw for invalid $w and $wtimeout args
1 parent 7757f11 commit 3f8b851

File tree

5 files changed

+87
-4
lines changed

5 files changed

+87
-4
lines changed

src/MongoDB/WriteConcern.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,20 @@ PHP_METHOD(WriteConcern, __construct)
7474
intern->write_concern = mongoc_write_concern_new();
7575

7676
if (Z_TYPE_P(w) == IS_LONG) {
77+
if (Z_LVAL_P(w) < -3) {
78+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected w to be >= -3, %ld given", Z_LVAL_P(w));
79+
return;
80+
}
7781
mongoc_write_concern_set_w(intern->write_concern, Z_LVAL_P(w));
7882
} else if (Z_TYPE_P(w) == IS_STRING) {
7983
if (strcmp(Z_STRVAL_P(w), PHONGO_WRITE_CONCERN_W_MAJORITY) == 0) {
8084
mongoc_write_concern_set_w(intern->write_concern, MONGOC_WRITE_CONCERN_W_MAJORITY);
8185
} else {
8286
mongoc_write_concern_set_wtag(intern->write_concern, Z_STRVAL_P(w));
8387
}
88+
} else {
89+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected w to be integer or string, %s given", zend_get_type_by_const(Z_TYPE_P(w)));
90+
return;
8491
}
8592

8693
switch(ZEND_NUM_ARGS()) {
@@ -91,9 +98,12 @@ PHP_METHOD(WriteConcern, __construct)
9198
mongoc_write_concern_set_journal(intern->write_concern, journal);
9299
/* fallthrough */
93100
case 2:
94-
if (wtimeout > 0) {
95-
mongoc_write_concern_set_wtimeout(intern->write_concern, wtimeout);
101+
if (wtimeout < 0) {
102+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected wtimeout to be >= 0, %ld given", wtimeout);
103+
return;
96104
}
105+
106+
mongoc_write_concern_set_wtimeout(intern->write_concern, wtimeout);
97107
}
98108
}
99109
/* }}} */
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
--TEST--
2+
MongoDB\Driver\WriteConcern construction (invalid w type)
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
$tests = array(
10+
1.0,
11+
true,
12+
array(),
13+
new stdClass,
14+
null,
15+
);
16+
17+
foreach ($tests as $test) {
18+
echo throws(function() use ($test) {
19+
new MongoDB\Driver\WriteConcern($test);
20+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
21+
}
22+
23+
?>
24+
===DONE===
25+
<?php exit(0); ?>
26+
--EXPECTF--
27+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
28+
Expected w to be integer or string, double given
29+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
30+
Expected w to be integer or string, boolean given
31+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
32+
Expected w to be integer or string, array given
33+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
34+
Expected w to be integer or string, object given
35+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
36+
Expected w to be integer or string, %r(null|NULL)%r given
37+
===DONE===
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
MongoDB\Driver\WriteConcern construction (invalid w range)
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
echo throws(function() {
10+
new MongoDB\Driver\WriteConcern(-4);
11+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
12+
13+
?>
14+
===DONE===
15+
<?php exit(0); ?>
16+
--EXPECT--
17+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
18+
Expected w to be >= -3, -4 given
19+
===DONE===
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
MongoDB\Driver\WriteConcern construction (invalid wtimeout range)
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
echo throws(function() {
10+
new MongoDB\Driver\WriteConcern(1, -1);
11+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
12+
13+
?>
14+
===DONE===
15+
<?php exit(0); ?>
16+
--EXPECT--
17+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
18+
Expected wtimeout to be >= 0, -1 given
19+
===DONE===

tests/writeConcern/writeconcern-getwtimeout-001.phpt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ MongoDB\Driver\WriteConcern::getWtimeout()
77
require_once __DIR__ . "/../utils/basic.inc";
88

99
$tests = array(
10-
-1,
1110
0,
1211
1,
1312
);
@@ -26,7 +25,6 @@ var_dump($wc->getWtimeout());
2625
<?php exit(0); ?>
2726
--EXPECT--
2827
int(0)
29-
int(0)
3028
int(1)
3129
int(0)
3230
===DONE===

0 commit comments

Comments
 (0)