Skip to content

Commit 4d8e3e3

Browse files
committed
PHPC-595: Throw exception if wtimeout > INT32_MAX
This also defines a compatility constant for printing phongo_long, which may vary by PHP version and architecture.
1 parent 87c53a8 commit 4d8e3e3

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

phongo_compat.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@
124124
# define phongo_char_pdup(str) pestrdup(filename->val, 1)
125125
# define phongo_char_free(str) zend_string_release(str)
126126
# define phongo_long zend_long
127+
#if SIZEOF_ZEND_LONG == 8
128+
# define PHONGO_LONG_FORMAT PRId64
129+
#elif SIZEOF_ZEND_LONG == 4
130+
# define PHONGO_LONG_FORMAT PRId32
131+
#else
132+
# error Unsupported architecture (integers are neither 32-bit nor 64-bit)
133+
#endif
127134
# define SIZEOF_PHONGO_LONG SIZEOF_ZEND_LONG
128135
# define phongo_str(str) str->val
129136
# define phongo_create_object_retval zend_object*
@@ -153,6 +160,7 @@
153160
# define phongo_char_pdup(str) pestrdup(filename, 1)
154161
# define phongo_char_free(str) _efree(str ZEND_FILE_LINE_CC ZEND_FILE_LINE_CC)
155162
# define phongo_long long
163+
# define PHONGO_LONG_FORMAT "ld"
156164
# define SIZEOF_PHONGO_LONG SIZEOF_LONG
157165
# define phongo_str(str) str
158166
# define phongo_create_object_retval zend_object_value

src/MongoDB/WriteConcern.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ PHP_METHOD(WriteConcern, __construct)
5454
php_phongo_writeconcern_t *intern;
5555
zend_error_handling error_handling;
5656
zval *w, *journal;
57-
long wtimeout = 0;
57+
phongo_long wtimeout = 0;
5858
SUPPRESS_UNUSED_WARNING(return_value) SUPPRESS_UNUSED_WARNING(return_value_ptr) SUPPRESS_UNUSED_WARNING(return_value_used)
5959

6060

@@ -99,7 +99,12 @@ PHP_METHOD(WriteConcern, __construct)
9999
/* fallthrough */
100100
case 2:
101101
if (wtimeout < 0) {
102-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected wtimeout to be >= 0, %ld given", wtimeout);
102+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected wtimeout to be >= 0, %" PHONGO_LONG_FORMAT " given", wtimeout);
103+
return;
104+
}
105+
106+
if (wtimeout > INT32_MAX) {
107+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected wtimeout to be <= %" PRId32 ", %" PHONGO_LONG_FORMAT " given", INT32_MAX, wtimeout);
103108
return;
104109
}
105110

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
MongoDB\Driver\WriteConcern construction (invalid wtimeout range)
3+
--SKIPIF--
4+
<?php if (8 !== PHP_INT_SIZE) { die('skip Only for 64-bit platform'); } ?>
5+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
6+
--FILE--
7+
<?php
8+
require_once __DIR__ . "/../utils/basic.inc";
9+
10+
echo throws(function() {
11+
new MongoDB\Driver\WriteConcern(1, 2147483648);
12+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
13+
14+
?>
15+
===DONE===
16+
<?php exit(0); ?>
17+
--EXPECT--
18+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
19+
Expected wtimeout to be <= 2147483647, 2147483648 given
20+
===DONE===

0 commit comments

Comments
 (0)