Skip to content

Commit a54fbcd

Browse files
committed
PHPC-1411: Emit warning when truncating 64-bit value
1 parent cdff70f commit a54fbcd

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/MongoDB/WriteConcern.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,14 +269,23 @@ static PHP_METHOD(WriteConcern, getW)
269269
static PHP_METHOD(WriteConcern, getWtimeout)
270270
{
271271
php_phongo_writeconcern_t* intern;
272+
int64_t wtimeout;
272273

273274
intern = Z_WRITECONCERN_OBJ_P(getThis());
274275

275276
if (zend_parse_parameters_none() == FAILURE) {
276277
return;
277278
}
278279

279-
RETURN_LONG(mongoc_write_concern_get_wtimeout_int64(intern->write_concern));
280+
wtimeout = mongoc_write_concern_get_wtimeout_int64(intern->write_concern);
281+
282+
#if SIZEOF_LONG == 4
283+
if (wtimeout > INT32_MAX || wtimeout < INT32_MIN) {
284+
zend_error(E_WARNING, "Truncating 64-bit value for wTimeoutMS");
285+
}
286+
#endif
287+
288+
RETURN_LONG(wtimeout);
280289
} /* }}} */
281290

282291
/* {{{ proto null|boolean MongoDB\Driver\WriteConcern::getJournal()
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
MongoDB\Driver\WriteConcern::getWtimeout() emits warning on truncation of 64-bit value
3+
--SKIPIF--
4+
<?php if (4 !== PHP_INT_SIZE) { die('skip Only for 32-bit platform'); } ?>
5+
--FILE--
6+
<?php
7+
8+
require_once __DIR__ . '/../utils/tools.php';
9+
10+
$manager = new MongoDB\Driver\Manager('mongodb://127.0.0.1/?w=2&wtimeoutms=4294967296');
11+
12+
echo raises(function() use ($manager) {
13+
var_dump($manager->getWriteConcern()->getWtimeout());
14+
}, E_WARNING), "\n";
15+
16+
?>
17+
===DONE===
18+
<?php exit(0); ?>
19+
--EXPECT--
20+
OK: Got E_WARNING
21+
Truncating 64-bit value for wTimeoutMS
22+
===DONE===

0 commit comments

Comments
 (0)