Skip to content

Commit 22a39d6

Browse files
committed
PHPC-313: BSON should throw when encountering 64-bit integer on 32-bit platform
1 parent 3849719 commit 22a39d6

File tree

3 files changed

+47
-46
lines changed

3 files changed

+47
-46
lines changed

phongo_compat.h

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -150,23 +150,13 @@
150150
#elif SIZEOF_PHONGO_LONG == 4
151151
# define ADD_INDEX_INT64(zval, index, value) \
152152
if (value > INT32_MAX || value < INT32_MIN) { \
153-
char *tmp; \
154-
int tmp_len; \
155-
mongoc_log(MONGOC_LOG_LEVEL_WARNING, MONGOC_LOG_DOMAIN, "Integer overflow detected on your platform: %lld", value); \
156-
tmp_len = spprintf(&tmp, 0, "%lld", value); \
157-
ADD_INDEX_STRINGL(zval, index, tmp, tmp_len); \
158-
efree(tmp); \
153+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Integer overflow detected on your platform: %lld", value); \
159154
} else { \
160155
add_index_long(zval, index, val); \
161156
}
162157
# define ADD_ASSOC_INT64(zval, key, value) \
163158
if (value > INT32_MAX || value < INT32_MIN) { \
164-
char *tmp; \
165-
int tmp_len; \
166-
mongoc_log(MONGOC_LOG_LEVEL_WARNING, MONGOC_LOG_DOMAIN, "Integer overflow detected on your platform: %lld", value); \
167-
tmp_len = spprintf(&tmp, 0, "%lld", value); \
168-
ADD_ASSOC_STRING_EX(zval, key, strlen(key), tmp, tmp_len); \
169-
efree(tmp); \
159+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Integer overflow detected on your platform: %lld", value); \
170160
} else { \
171161
add_assoc_long(zval, key, value); \
172162
}

tests/bson/bug0313.phpt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--TEST--
2+
PHPC-313: BSON should throw when encountering 64-bit integer on 32-bit platform
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+
$tests = [
11+
'{ "x": { "$numberLong": "4294967294" }}',
12+
'{ "x": { "$numberLong": "4294967295" }}',
13+
'{ "x": { "$numberLong": "9223372036854775807" }}',
14+
'{ "longFieldName": { "$numberLong": "4294967294" }}',
15+
'{ "longFieldName": { "$numberLong": "4294967295" }}',
16+
'{ "longFieldName": { "$numberLong": "9223372036854775807" }}',
17+
];
18+
19+
foreach ($tests as $json) {
20+
throws(function() use ($json) { var_dump(toPHP(fromJSON($json))); }, 'MongoDB\Driver\Exception\InvalidArgumentException');
21+
}
22+
23+
?>
24+
===DONE===
25+
<?php exit(0); ?>
26+
--EXPECT--
27+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
28+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
29+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
30+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
31+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
32+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
33+
===DONE===

tests/bson/bug0592.phpt

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
PHPC-592: Property name corrupted when unserializing 64-bit integer on 32-bit platform
33
--SKIPIF--
44
<?php if (4 !== PHP_INT_SIZE) { die('skip Only for 32-bit platform'); } ?>
5-
--INI--
6-
mongodb.debug=stderr
75
--FILE--
86
<?php
97

@@ -24,15 +22,19 @@ $tests = [
2422

2523
foreach ($tests as $json) {
2624
printf("Test %s\n", $json);
27-
var_dump(toPHP(fromJSON($json)));
25+
try {
26+
$encoded = toPHP(fromJSON($json));
27+
var_dump( $encoded );
28+
} catch ( MongoDB\Driver\Exception\InvalidArgumentException $e ) {
29+
echo "MongoDB\Driver\Exception\InvalidArgumentException: ", $e->getMessage(), "\n";
30+
}
2831
echo "\n";
2932
}
3033

3134
?>
3235
===DONE===
3336
<?php exit(0); ?>
3437
--EXPECTF--
35-
%a
3638
Test { "x": { "$numberLong": "-2147483648" }}
3739
object(stdClass)#%d (%d) {
3840
["x"]=>
@@ -46,25 +48,13 @@ object(stdClass)#%d (%d) {
4648
}
4749

4850
Test { "x": { "$numberLong": "4294967294" }}
49-
[%s] PHONGO-BSON: WARNING > Integer overflow detected on your platform: 4294967294
50-
object(stdClass)#%d (%d) {
51-
["x"]=>
52-
string(10) "4294967294"
53-
}
51+
MongoDB\Driver\Exception\InvalidArgumentException: Integer overflow detected on your platform: 4294967294
5452

5553
Test { "x": { "$numberLong": "4294967295" }}
56-
[%s] PHONGO-BSON: WARNING > Integer overflow detected on your platform: 4294967295
57-
object(stdClass)#%d (%d) {
58-
["x"]=>
59-
string(10) "4294967295"
60-
}
54+
MongoDB\Driver\Exception\InvalidArgumentException: Integer overflow detected on your platform: 4294967295
6155

6256
Test { "x": { "$numberLong": "9223372036854775807" }}
63-
[%s] PHONGO-BSON: WARNING > Integer overflow detected on your platform: 9223372036854775807
64-
object(stdClass)#%d (%d) {
65-
["x"]=>
66-
string(19) "9223372036854775807"
67-
}
57+
MongoDB\Driver\Exception\InvalidArgumentException: Integer overflow detected on your platform: 9223372036854775807
6858

6959
Test { "longFieldName": { "$numberLong": "-2147483648" }}
7060
object(stdClass)#%d (%d) {
@@ -79,24 +69,12 @@ object(stdClass)#%d (%d) {
7969
}
8070

8171
Test { "longFieldName": { "$numberLong": "4294967294" }}
82-
[%s] PHONGO-BSON: WARNING > Integer overflow detected on your platform: 4294967294
83-
object(stdClass)#%d (%d) {
84-
["longFieldName"]=>
85-
string(10) "4294967294"
86-
}
72+
MongoDB\Driver\Exception\InvalidArgumentException: Integer overflow detected on your platform: 4294967294
8773

8874
Test { "longFieldName": { "$numberLong": "4294967295" }}
89-
[%s] PHONGO-BSON: WARNING > Integer overflow detected on your platform: 4294967295
90-
object(stdClass)#%d (%d) {
91-
["longFieldName"]=>
92-
string(10) "4294967295"
93-
}
75+
MongoDB\Driver\Exception\InvalidArgumentException: Integer overflow detected on your platform: 4294967295
9476

9577
Test { "longFieldName": { "$numberLong": "9223372036854775807" }}
96-
[%s] PHONGO-BSON: WARNING > Integer overflow detected on your platform: 9223372036854775807
97-
object(stdClass)#%d (%d) {
98-
["longFieldName"]=>
99-
string(19) "9223372036854775807"
100-
}
78+
MongoDB\Driver\Exception\InvalidArgumentException: Integer overflow detected on your platform: 9223372036854775807
10179

10280
===DONE===

0 commit comments

Comments
 (0)