Skip to content

Commit cb30d5d

Browse files
committed
Merge pull request #243
2 parents 87c53a8 + 9f93fbd commit cb30d5d

15 files changed

+245
-18
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

php_phongo.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2044,7 +2044,7 @@ void php_phongo_new_datetime_from_utcdatetime(zval *object, int64_t milliseconds
20442044
efree(sec);
20452045
datetime_obj->time->f = milliseconds % 1000;
20462046
} /* }}} */
2047-
void php_phongo_new_timestamp_from_increment_and_timestamp(zval *object, int32_t increment, int32_t timestamp TSRMLS_DC) /* {{{ */
2047+
void php_phongo_new_timestamp_from_increment_and_timestamp(zval *object, uint32_t increment, uint32_t timestamp TSRMLS_DC) /* {{{ */
20482048
{
20492049
php_phongo_timestamp_t *intern;
20502050

@@ -2080,7 +2080,7 @@ void php_phongo_new_binary_from_binary_and_type(zval *object, const char *data,
20802080
intern = Z_BINARY_OBJ_P(object);
20812081
intern->data = estrndup(data, data_len);
20822082
intern->data_len = data_len;
2083-
intern->type = type;
2083+
intern->type = (uint8_t) type;
20842084
} /* }}} */
20852085
void php_phongo_new_regex_from_regex_and_options(zval *object, const char *pattern, const char *flags TSRMLS_DC) /* {{{ */
20862086
{

php_phongo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ void php_phongo_objectid_new_from_oid(zval *object, const bson_oid_t *oid TSRMLS
141141
void php_phongo_cursor_id_new_from_id(zval *object, int64_t cursorid TSRMLS_DC);
142142
void php_phongo_new_utcdatetime_from_epoch(zval *object, int64_t msec_since_epoch TSRMLS_DC);
143143
void php_phongo_new_datetime_from_utcdatetime(zval *object, int64_t milliseconds TSRMLS_DC);
144-
void php_phongo_new_timestamp_from_increment_and_timestamp(zval *object, int32_t increment, int32_t timestamp TSRMLS_DC);
144+
void php_phongo_new_timestamp_from_increment_and_timestamp(zval *object, uint32_t increment, uint32_t timestamp TSRMLS_DC);
145145
void php_phongo_new_javascript_from_javascript(int init, zval *object, const char *code, size_t code_len TSRMLS_DC);
146146
void php_phongo_new_javascript_from_javascript_and_scope(int init, zval *object, const char *code, size_t code_len, const bson_t *scope TSRMLS_DC);
147147
void php_phongo_new_binary_from_binary_and_type(zval *object, const char *data, size_t data_len, bson_subtype_t type TSRMLS_DC);

php_phongo_structs-5.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ typedef struct {
120120
zend_object std;
121121
char *data;
122122
int data_len;
123-
int type;
123+
uint8_t type;
124124
} php_phongo_binary_t;
125125
typedef struct {
126126
zend_object std;
@@ -156,8 +156,8 @@ typedef struct {
156156
} php_phongo_regex_t;
157157
typedef struct {
158158
zend_object std;
159-
int32_t increment;
160-
int32_t timestamp;
159+
uint32_t increment;
160+
uint32_t timestamp;
161161
} php_phongo_timestamp_t;
162162
typedef struct {
163163
zend_object std;

php_phongo_structs-7.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ typedef struct {
155155
zend_object std;
156156
} php_phongo_regex_t;
157157
typedef struct {
158-
int32_t increment;
159-
int32_t timestamp;
158+
uint32_t increment;
159+
uint32_t timestamp;
160160
zend_object std;
161161
} php_phongo_timestamp_t;
162162
typedef struct {

src/BSON/Binary.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ PHP_METHOD(Binary, __construct)
5454
zend_error_handling error_handling;
5555
char *data;
5656
phongo_zpp_char_len data_len;
57-
long type;
57+
phongo_long type;
5858

5959

6060
zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling TSRMLS_CC);
@@ -66,9 +66,14 @@ PHP_METHOD(Binary, __construct)
6666
}
6767
zend_restore_error_handling(&error_handling TSRMLS_CC);
6868

69+
if (type < 0 || type > UINT8_MAX) {
70+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected type to be an unsigned 8-bit integer, %" PHONGO_LONG_FORMAT " given", type);
71+
return;
72+
}
73+
6974
intern->data = estrndup(data, data_len);
7075
intern->data_len = data_len;
71-
intern->type = type;
76+
intern->type = (uint8_t) type;
7277
}
7378
/* }}} */
7479
/* {{{ proto string Binary::getData()

src/BSON/Timestamp.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ PHP_METHOD(Timestamp, __construct)
5252
{
5353
php_phongo_timestamp_t *intern;
5454
zend_error_handling error_handling;
55-
long increment;
56-
long timestamp;
55+
phongo_long increment;
56+
phongo_long timestamp;
5757

5858

5959
zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling TSRMLS_CC);
@@ -65,6 +65,16 @@ PHP_METHOD(Timestamp, __construct)
6565
}
6666
zend_restore_error_handling(&error_handling TSRMLS_CC);
6767

68+
if (increment < 0 || increment > UINT32_MAX) {
69+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected increment to be an unsigned 32-bit integer, %" PHONGO_LONG_FORMAT " given", increment);
70+
return;
71+
}
72+
73+
if (timestamp < 0 || timestamp > UINT32_MAX) {
74+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected timestamp to be an unsigned 32-bit integer, %" PHONGO_LONG_FORMAT " given", timestamp);
75+
return;
76+
}
77+
6878
intern->increment = increment;
6979
intern->timestamp = timestamp;
7080
}
@@ -84,7 +94,7 @@ PHP_METHOD(Timestamp, __toString)
8494
return;
8595
}
8696

87-
retval_len = spprintf(&retval, 0, "[%d:%d]", intern->increment, intern->timestamp);
97+
retval_len = spprintf(&retval, 0, "[%" PRIu32 ":%" PRIu32 "]", intern->increment, intern->timestamp);
8898
PHONGO_RETVAL_STRINGL(retval, retval_len);
8999
efree(retval);
90100
}

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

src/bson.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,15 @@ int64_t php_phongo_utcdatetime_get_milliseconds(zval *object TSRMLS_DC)
9393

9494
return intern->milliseconds;
9595
}
96-
int32_t php_phongo_timestamp_get_increment(zval *object TSRMLS_DC)
96+
uint32_t php_phongo_timestamp_get_increment(zval *object TSRMLS_DC)
9797
{
9898
php_phongo_timestamp_t *intern;
9999

100100
intern = Z_TIMESTAMP_OBJ_P(object);
101101

102102
return intern->increment;
103103
}
104-
int32_t php_phongo_timestamp_get_timestamp(zval *object TSRMLS_DC)
104+
uint32_t php_phongo_timestamp_get_timestamp(zval *object TSRMLS_DC)
105105
{
106106
php_phongo_timestamp_t *intern;
107107

@@ -142,7 +142,7 @@ int php_phongo_binary_get_data(zval *object, char **data TSRMLS_DC)
142142
*data = intern->data;
143143
return intern->data_len;
144144
}
145-
int php_phongo_binary_get_type(zval *object TSRMLS_DC)
145+
uint8_t php_phongo_binary_get_type(zval *object TSRMLS_DC)
146146
{
147147
php_phongo_binary_t *intern;
148148

tests/bson/bson-binary_error-004.phpt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
BSON BSON\Binary constructor requires unsigned 8-bit integer for type
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
use MongoDB\BSON as BSON;
10+
11+
echo throws(function() {
12+
new BSON\Binary('foo', -1);
13+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
14+
15+
echo throws(function() {
16+
new BSON\Binary('foo', 256);
17+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
18+
19+
?>
20+
===DONE===
21+
<?php exit(0); ?>
22+
--EXPECTF--
23+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
24+
Expected type to be an unsigned 8-bit integer, -1 given
25+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
26+
Expected type to be an unsigned 8-bit integer, 256 given
27+
===DONE===

tests/bson/bson-timestamp-003.phpt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
--TEST--
2+
BSON BSON\Timestamp constructor requires positive unsigned 32-bit integers
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
use MongoDB\BSON as BSON;
10+
11+
$tests = [
12+
new BSON\Timestamp(2147483647, 0),
13+
new BSON\Timestamp(0, 2147483647),
14+
];
15+
16+
foreach ($tests as $test) {
17+
printf("Test %s\n", $test);
18+
var_dump($test);
19+
echo "\n";
20+
}
21+
22+
?>
23+
===DONE===
24+
<?php exit(0); ?>
25+
--EXPECTF--
26+
Test [2147483647:0]
27+
object(%SBSON\Timestamp)#%d (%d) {
28+
["increment"]=>
29+
int(2147483647)
30+
["timestamp"]=>
31+
int(0)
32+
}
33+
34+
Test [0:2147483647]
35+
object(%SBSON\Timestamp)#%d (%d) {
36+
["increment"]=>
37+
int(0)
38+
["timestamp"]=>
39+
int(2147483647)
40+
}
41+
42+
===DONE===

tests/bson/bson-timestamp-004.phpt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
--TEST--
2+
BSON BSON\Timestamp constructor requires 64-bit integers to be positive unsigned 32-bit integers
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+
use MongoDB\BSON as BSON;
11+
12+
$tests = [
13+
new BSON\Timestamp(4294967295, 0),
14+
new BSON\Timestamp(0, 4294967295),
15+
];
16+
17+
foreach ($tests as $test) {
18+
printf("Test %s\n", $test);
19+
var_dump($test);
20+
echo "\n";
21+
}
22+
23+
?>
24+
===DONE===
25+
<?php exit(0); ?>
26+
--EXPECTF--
27+
Test [4294967295:0]
28+
object(%SBSON\Timestamp)#%d (%d) {
29+
["increment"]=>
30+
int(4294967295)
31+
["timestamp"]=>
32+
int(0)
33+
}
34+
35+
Test [0:4294967295]
36+
object(%SBSON\Timestamp)#%d (%d) {
37+
["increment"]=>
38+
int(0)
39+
["timestamp"]=>
40+
int(4294967295)
41+
}
42+
43+
===DONE===
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
--TEST--
2+
BSON BSON\Timestamp constructor requires positive unsigned 32-bit integers
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
use MongoDB\BSON as BSON;
10+
11+
echo throws(function() {
12+
new BSON\Timestamp(-1, 0);
13+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
14+
15+
echo throws(function() {
16+
new BSON\Timestamp(-2147483648, 0);
17+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
18+
19+
echo throws(function() {
20+
new BSON\Timestamp(0, -1);
21+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
22+
23+
echo throws(function() {
24+
new BSON\Timestamp(0, -2147483648);
25+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
26+
27+
?>
28+
===DONE===
29+
<?php exit(0); ?>
30+
--EXPECT--
31+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
32+
Expected increment to be an unsigned 32-bit integer, -1 given
33+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
34+
Expected increment to be an unsigned 32-bit integer, -2147483648 given
35+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
36+
Expected timestamp to be an unsigned 32-bit integer, -1 given
37+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
38+
Expected timestamp to be an unsigned 32-bit integer, -2147483648 given
39+
===DONE===
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
BSON BSON\Timestamp constructor requires 64-bit integers to be positive unsigned 32-bit integers
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+
use MongoDB\BSON as BSON;
11+
12+
echo throws(function() {
13+
new BSON\Timestamp(4294967296, 0);
14+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
15+
16+
echo throws(function() {
17+
new BSON\Timestamp(0, 4294967296);
18+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
19+
20+
?>
21+
===DONE===
22+
<?php exit(0); ?>
23+
--EXPECT--
24+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
25+
Expected increment to be an unsigned 32-bit integer, 4294967296 given
26+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
27+
Expected timestamp to be an unsigned 32-bit integer, 4294967296 given
28+
===DONE===
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)