Skip to content

Commit 9f93fbd

Browse files
committed
PHPC-593: Binary type is an unsigned 8-bit integer
This adds range checking to Binary's constructor. Internal structs and functions were changed to use uint8_t for readability.
1 parent c6574fa commit 9f93fbd

File tree

5 files changed

+37
-5
lines changed

5 files changed

+37
-5
lines changed

php_phongo.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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_structs-5.h

Lines changed: 1 addition & 1 deletion
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;

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.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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===

0 commit comments

Comments
 (0)