Skip to content

Commit ff00668

Browse files
committed
PHPC-731: Parse Timestamp argument as strings to accept large integers
1 parent 26ca213 commit ff00668

File tree

2 files changed

+62
-6
lines changed

2 files changed

+62
-6
lines changed

src/BSON/Timestamp.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,27 +141,28 @@ static bool php_phongo_timestamp_init_from_hash(php_phongo_timestamp_t *intern,
141141
return false;
142142
}
143143

144-
/* {{{ proto void Timestamp::__construct(integer $increment, integer $timestamp)
144+
/* {{{ proto void Timestamp::__construct(string $increment, string $timestamp)
145145
Construct a new BSON timestamp type, which consists of a 4-byte increment and
146146
4-byte timestamp. */
147147
PHP_METHOD(Timestamp, __construct)
148148
{
149149
php_phongo_timestamp_t *intern;
150150
zend_error_handling error_handling;
151-
phongo_long increment;
152-
phongo_long timestamp;
153-
151+
char *s_increment;
152+
phongo_zpp_char_len s_increment_len;
153+
char *s_timestamp;
154+
phongo_zpp_char_len s_timestamp_len;
154155

155156
zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling TSRMLS_CC);
156157
intern = Z_TIMESTAMP_OBJ_P(getThis());
157158

158-
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &increment, &timestamp) == FAILURE) {
159+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &s_increment, &s_increment_len, &s_timestamp, &s_timestamp_len) == FAILURE) {
159160
zend_restore_error_handling(&error_handling TSRMLS_CC);
160161
return;
161162
}
162163
zend_restore_error_handling(&error_handling TSRMLS_CC);
163164

164-
php_phongo_timestamp_init(intern, increment, timestamp TSRMLS_CC);
165+
php_phongo_timestamp_init_from_string(intern, s_increment, s_increment_len, s_timestamp, s_timestamp_len TSRMLS_CC);
165166
}
166167
/* }}} */
167168

tests/bson/bson-timestamp-005.phpt

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
--TEST--
2+
MongoDB\BSON\Timestamp constructor requires positive unsigned 32-bit integers (as string)
3+
--FILE--
4+
<?php
5+
6+
$tests = [
7+
new MongoDB\BSON\Timestamp("2147483647", "0"),
8+
new MongoDB\BSON\Timestamp(0, "2147483647"),
9+
new MongoDB\BSON\Timestamp("4294967295", "0"),
10+
new MongoDB\BSON\Timestamp("0", "4294967295"),
11+
];
12+
13+
foreach ($tests as $test) {
14+
printf("Test %s\n", $test);
15+
var_dump($test);
16+
echo "\n";
17+
}
18+
19+
?>
20+
===DONE===
21+
<?php exit(0); ?>
22+
--EXPECTF--
23+
Test [2147483647:0]
24+
object(MongoDB\BSON\Timestamp)#%d (%d) {
25+
["increment"]=>
26+
string(10) "2147483647"
27+
["timestamp"]=>
28+
string(1) "0"
29+
}
30+
31+
Test [0:2147483647]
32+
object(MongoDB\BSON\Timestamp)#%d (%d) {
33+
["increment"]=>
34+
string(1) "0"
35+
["timestamp"]=>
36+
string(10) "2147483647"
37+
}
38+
39+
Test [4294967295:0]
40+
object(MongoDB\BSON\Timestamp)#%d (%d) {
41+
["increment"]=>
42+
string(10) "4294967295"
43+
["timestamp"]=>
44+
string(1) "0"
45+
}
46+
47+
Test [0:4294967295]
48+
object(MongoDB\BSON\Timestamp)#%d (%d) {
49+
["increment"]=>
50+
string(1) "0"
51+
["timestamp"]=>
52+
string(10) "4294967295"
53+
}
54+
55+
===DONE===

0 commit comments

Comments
 (0)