Skip to content

Commit 1f785d2

Browse files
committed
Use consistent string parsing for Timestamp and UTCDateTime init
bson_ascii_strtoll() will support range checking in libbson 1.5.0 (CDRIVER-1377), so there is no longer any benefit of using _atoi64() on Windows. This will make parsing consistent across both platforms, since _atoi64() does not report errors for non-integer strings (e.g. "1.2").
1 parent e0b83d8 commit 1f785d2

8 files changed

+78
-21
lines changed

src/BSON/Timestamp.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,22 +91,14 @@ static bool php_phongo_timestamp_init_from_string(php_phongo_timestamp_t *intern
9191
* available on all platforms (e.g. HP-UX), and atoll() provides no error
9292
* reporting at all. */
9393

94-
#if defined(PHP_WIN32)
95-
increment = _atoi64(s_increment);
96-
#else
9794
increment = bson_ascii_strtoll(s_increment, &endptr, 10);
98-
#endif
9995

10096
if (errno || (endptr && endptr != ((const char *)s_increment + s_increment_len))) {
10197
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Error parsing \"%s\" as 64-bit integer increment for %s initialization", s_increment, ZSTR_VAL(php_phongo_timestamp_ce->name));
10298
return false;
10399
}
104100

105-
#if defined(PHP_WIN32)
106-
timestamp = _atoi64(s_timestamp);
107-
#else
108101
timestamp = bson_ascii_strtoll(s_timestamp, &endptr, 10);
109-
#endif
110102

111103
if (errno || (endptr && endptr != ((const char *)s_timestamp + s_timestamp_len))) {
112104
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Error parsing \"%s\" as 64-bit integer timestamp for %s initialization", s_timestamp, ZSTR_VAL(php_phongo_timestamp_ce->name));

src/BSON/UTCDateTime.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,7 @@ static bool php_phongo_utcdatetime_init_from_string(php_phongo_utcdatetime_t *in
7777

7878
errno = 0;
7979

80-
#if defined(PHP_WIN32)
81-
milliseconds = _atoi64(s_milliseconds);
82-
#else
8380
milliseconds = bson_ascii_strtoll(s_milliseconds, &endptr, 10);
84-
#endif
8581

8682
/* errno will set errno if conversion fails; however, we do not need to
8783
* specify the type of error.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
MongoDB\BSON\Timestamp unserialization requires strings to parse as 64-bit integers
3+
--FILE--
4+
<?php
5+
6+
require_once __DIR__ . '/../utils/tools.php';
7+
8+
echo throws(function() {
9+
unserialize('C:22:"MongoDB\BSON\Timestamp":60:{a:2:{s:9:"increment";s:4:"1.23";s:9:"timestamp";s:4:"5678";}}');
10+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
11+
12+
echo throws(function() {
13+
unserialize('C:22:"MongoDB\BSON\Timestamp":60:{a:2:{s:9:"increment";s:4:"1234";s:9:"timestamp";s:4:"5.67";}}');
14+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
15+
16+
?>
17+
===DONE===
18+
<?php exit(0); ?>
19+
--EXPECT--
20+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
21+
Error parsing "1.23" as 64-bit integer increment for MongoDB\BSON\Timestamp initialization
22+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
23+
Error parsing "5.67" as 64-bit integer timestamp for MongoDB\BSON\Timestamp initialization
24+
===DONE===
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--TEST--
2-
MongoDB\BSON\Timestamp::__set_state() with broken numeric strings
2+
MongoDB\BSON\Timestamp::__set_state() requires strings to parse as 64-bit integers
33
--SKIPIF--
44
<?php if (8 !== PHP_INT_SIZE) { die('skip Only for 64-bit platform'); } ?>
55
--FILE--
@@ -8,19 +8,19 @@ MongoDB\BSON\Timestamp::__set_state() with broken numeric strings
88
require_once __DIR__ . '/../utils/tools.php';
99

1010
echo throws(function() {
11-
MongoDB\BSON\Timestamp::__set_state(['increment' => 'broken', 'timestamp' => '5678']);
11+
MongoDB\BSON\Timestamp::__set_state(['increment' => '1.23', 'timestamp' => '5678']);
1212
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
1313

1414
echo throws(function() {
15-
MongoDB\BSON\Timestamp::__set_state(['increment' => '1234', 'timestamp' => 'broken']);
15+
MongoDB\BSON\Timestamp::__set_state(['increment' => '1234', 'timestamp' => '5.67']);
1616
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
1717

1818
?>
1919
===DONE===
2020
<?php exit(0); ?>
2121
--EXPECT--
2222
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
23-
Error parsing "broken" as 64-bit integer increment for MongoDB\BSON\Timestamp initialization
23+
Error parsing "1.23" as 64-bit integer increment for MongoDB\BSON\Timestamp initialization
2424
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
25-
Error parsing "broken" as 64-bit integer timestamp for MongoDB\BSON\Timestamp initialization
25+
Error parsing "5.67" as 64-bit integer timestamp for MongoDB\BSON\Timestamp initialization
2626
===DONE===
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
MongoDB\BSON\Timestamp constructor requires strings to parse as 64-bit integers
3+
--FILE--
4+
<?php
5+
6+
require_once __DIR__ . '/../utils/tools.php';
7+
8+
echo throws(function() {
9+
new MongoDB\BSON\Timestamp('1.23', '5678');
10+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
11+
12+
echo throws(function() {
13+
new MongoDB\BSON\Timestamp('1234', '5.67');
14+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
15+
16+
?>
17+
===DONE===
18+
<?php exit(0); ?>
19+
--EXPECT--
20+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
21+
Error parsing "1.23" as 64-bit integer increment for MongoDB\BSON\Timestamp initialization
22+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
23+
Error parsing "5.67" as 64-bit integer timestamp for MongoDB\BSON\Timestamp initialization
24+
===DONE===

tests/bson/bson-utcdatetime-serialization_error-002.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ MongoDB\BSON\UTCDateTime unserialization requires "milliseconds" string to parse
66
require_once __DIR__ . '/../utils/tools.php';
77

88
echo throws(function() {
9-
unserialize('C:24:"MongoDB\BSON\UTCDateTime":40:{a:1:{s:12:"milliseconds";s:7:"INVALID";}}');
9+
unserialize('C:24:"MongoDB\BSON\UTCDateTime":42:{a:1:{s:12:"milliseconds";s:9:"1234.5678";}}');
1010
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
1111

1212
/* TODO: Add tests for out-of-range values once CDRIVER-1377 is resolved */
@@ -16,5 +16,5 @@ echo throws(function() {
1616
<?php exit(0); ?>
1717
--EXPECT--
1818
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
19-
Error parsing "INVALID" as 64-bit integer for MongoDB\BSON\UTCDateTime initialization
19+
Error parsing "1234.5678" as 64-bit integer for MongoDB\BSON\UTCDateTime initialization
2020
===DONE===

tests/bson/bson-utcdatetime-set_state_error-002.phpt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
MongoDB\BSON\UTCDateTime::__set_state() requires "milliseconds" string to parse as 64-bit integer
33
--FILE--
44
<?php
5+
56
require_once __DIR__ . '/../utils/tools.php';
67

78
echo throws(function() {
8-
MongoDB\BSON\UTCDateTime::__set_state(['milliseconds' => 'INVALID']);
9+
MongoDB\BSON\UTCDateTime::__set_state(['milliseconds' => '1234.5678']);
910
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
1011

1112
/* TODO: Add tests for out-of-range values once CDRIVER-1377 is resolved */
@@ -15,5 +16,5 @@ echo throws(function() {
1516
<?php exit(0); ?>
1617
--EXPECT--
1718
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
18-
Error parsing "INVALID" as 64-bit integer for MongoDB\BSON\UTCDateTime initialization
19+
Error parsing "1234.5678" as 64-bit integer for MongoDB\BSON\UTCDateTime initialization
1920
===DONE===
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
MongoDB\BSON\UTCDateTime constructor requires strings to parse as 64-bit integers
3+
--FILE--
4+
<?php
5+
6+
require_once __DIR__ . '/../utils/tools.php';
7+
8+
echo throws(function() {
9+
new MongoDB\BSON\UTCDateTime('1234.5678');
10+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
11+
12+
/* TODO: Add tests for out-of-range values once CDRIVER-1377 is resolved */
13+
14+
?>
15+
===DONE===
16+
<?php exit(0); ?>
17+
--EXPECT--
18+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
19+
Error parsing "1234.5678" as 64-bit integer for MongoDB\BSON\UTCDateTime initialization
20+
===DONE===

0 commit comments

Comments
 (0)