Skip to content

Commit 223d1b9

Browse files
committed
Merge pull request #623
2 parents f1d2514 + 7a79f14 commit 223d1b9

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

php_phongo.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,20 +1166,24 @@ static bool php_phongo_apply_rp_options_to_uri(mongoc_uri_t *uri, bson_t *option
11661166

11671167
/* Handle maxStalenessSeconds, and make sure it is not combined with primary
11681168
* readPreference */
1169-
if (bson_iter_init_find_case(&iter, options, MONGOC_URI_MAXSTALENESSSECONDS) && BSON_ITER_HOLDS_INT32(&iter)) {
1170-
int32_t max_staleness_seconds = bson_iter_int32(&iter);
1169+
if (bson_iter_init_find_case(&iter, options, MONGOC_URI_MAXSTALENESSSECONDS) && BSON_ITER_HOLDS_INT(&iter)) {
1170+
int64_t max_staleness_seconds = bson_iter_as_int64(&iter);
11711171

11721172
if (max_staleness_seconds != MONGOC_NO_MAX_STALENESS) {
11731173

11741174
if (max_staleness_seconds < MONGOC_SMALLEST_MAX_STALENESS_SECONDS) {
1175-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected maxStalenessSeconds to be >= %d, %" PRId32 " given", MONGOC_SMALLEST_MAX_STALENESS_SECONDS, max_staleness_seconds);
1175+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected maxStalenessSeconds to be >= %d, %" PRId64 " given", MONGOC_SMALLEST_MAX_STALENESS_SECONDS, max_staleness_seconds);
11761176
mongoc_read_prefs_destroy(new_rp);
11771177

11781178
return false;
11791179
}
11801180

1181-
/* max_staleness_seconds is fetched as an INT32, so there is no need to check
1182-
* if it exists INT32_MAX as we do in the ReadPreference constructor. */
1181+
if (max_staleness_seconds > INT32_MAX) {
1182+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected maxStalenessSeconds to be <= %d, %" PRId64 " given", INT32_MAX, max_staleness_seconds);
1183+
mongoc_read_prefs_destroy(new_rp);
1184+
1185+
return false;
1186+
}
11831187

11841188
if (mongoc_read_prefs_get_mode(new_rp) == MONGOC_READ_PRIMARY) {
11851189
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Primary read preference mode conflicts with maxStalenessSeconds");

tests/manager/manager-ctor-read_preference-error-002.phpt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ echo throws(function() {
1313
$manager = new MongoDB\Driver\Manager('mongodb://127.0.0.1/?maxStalenessSeconds=1231');
1414
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
1515

16+
echo throws(function() {
17+
$manager = new MongoDB\Driver\Manager('mongodb://127.0.0.1/?readPreference=secondary&maxStalenessSeconds=2147483648');
18+
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
19+
1620
echo throws(function() {
1721
$manager = new MongoDB\Driver\Manager(null, ['maxstalenessseconds' => 1231]);
1822
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
@@ -42,6 +46,8 @@ Failed to parse MongoDB URI: 'mongodb://127.0.0.1/?maxstalenessseconds=1231'. In
4246
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
4347
Failed to parse MongoDB URI: 'mongodb://127.0.0.1/?maxStalenessSeconds=1231'. Invalid readPreferences.
4448
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
49+
Failed to parse MongoDB URI: 'mongodb://127.0.0.1/?readPreference=secondary&maxStalenessSeconds=2147483648'. Unknown option or value for 'maxStalenessSeconds=2147483648'.
50+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
4551
Primary read preference mode conflicts with maxStalenessSeconds
4652
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
4753
Primary read preference mode conflicts with maxStalenessSeconds
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
MongoDB\Driver\Manager::__construct(): invalid read preference (maxStalenessSeconds range)
3+
--SKIPIF--
4+
<?php if (8 !== PHP_INT_SIZE) { die('skip Only for 64-bit platform'); } ?>
5+
--FILE--
6+
<?php
7+
8+
require_once __DIR__ . '/../utils/tools.php';
9+
10+
echo throws(function() {
11+
$manager = new MongoDB\Driver\Manager(null, ['readPreference' => 'secondary', 'maxStalenessSeconds' => 2147483648]);
12+
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
13+
14+
?>
15+
===DONE===
16+
<?php exit(0); ?>
17+
--EXPECTF--
18+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
19+
Expected maxStalenessSeconds to be <= 2147483647, 2147483648 given
20+
===DONE===

0 commit comments

Comments
 (0)