Skip to content

PHPC-1699: Ensure all argument parsing errors throw InvalidArgumentException #1173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions php_phongo.c
Original file line number Diff line number Diff line change
Expand Up @@ -3849,9 +3849,14 @@ PHP_FUNCTION(MongoDB_disabled___construct) /* {{{ */

PHP_FUNCTION(MongoDB_disabled___wakeup) /* {{{ */
{
zend_error_handling error_handling;

zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);
if (zend_parse_parameters_none() == FAILURE) {
zend_restore_error_handling(&error_handling);
return;
}
zend_restore_error_handling(&error_handling);

phongo_throw_exception(PHONGO_ERROR_RUNTIME, "%s", "MongoDB\\Driver objects cannot be serialized");
} /* }}} */
Expand Down
33 changes: 28 additions & 5 deletions src/BSON/Binary.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ static bool php_phongo_binary_init_from_hash(php_phongo_binary_t* intern, HashTa
Construct a new BSON binary type */
static PHP_METHOD(Binary, __construct)
{
php_phongo_binary_t* intern;
zend_error_handling error_handling;
php_phongo_binary_t* intern;
char* data;
size_t data_len;
zend_long type;

zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);
intern = Z_BINARY_OBJ_P(getThis());

zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sl", &data, &data_len, &type) == FAILURE) {
zend_restore_error_handling(&error_handling);
return;
Expand All @@ -95,13 +95,17 @@ static PHP_METHOD(Binary, __construct)
*/
static PHP_METHOD(Binary, __set_state)
{
zend_error_handling error_handling;
php_phongo_binary_t* intern;
HashTable* props;
zval* array;

zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);
if (zend_parse_parameters(ZEND_NUM_ARGS(), "a", &array) == FAILURE) {
RETURN_FALSE;
zend_restore_error_handling(&error_handling);
return;
}
zend_restore_error_handling(&error_handling);

object_init_ex(return_value, php_phongo_binary_ce);

Expand All @@ -115,11 +119,15 @@ static PHP_METHOD(Binary, __set_state)
Return the Binary's data string. */
static PHP_METHOD(Binary, __toString)
{
zend_error_handling error_handling;
php_phongo_binary_t* intern;

zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);
if (zend_parse_parameters_none() == FAILURE) {
zend_restore_error_handling(&error_handling);
return;
}
zend_restore_error_handling(&error_handling);

intern = Z_BINARY_OBJ_P(getThis());

Expand All @@ -130,13 +138,17 @@ static PHP_METHOD(Binary, __toString)
*/
static PHP_METHOD(Binary, getData)
{
zend_error_handling error_handling;
php_phongo_binary_t* intern;

intern = Z_BINARY_OBJ_P(getThis());

zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);
if (zend_parse_parameters_none() == FAILURE) {
zend_restore_error_handling(&error_handling);
return;
}
zend_restore_error_handling(&error_handling);

RETURN_STRINGL(intern->data, intern->data_len);
} /* }}} */
Expand All @@ -145,13 +157,17 @@ static PHP_METHOD(Binary, getData)
*/
static PHP_METHOD(Binary, getType)
{
zend_error_handling error_handling;
php_phongo_binary_t* intern;

intern = Z_BINARY_OBJ_P(getThis());

zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);
if (zend_parse_parameters_none() == FAILURE) {
zend_restore_error_handling(&error_handling);
return;
}
zend_restore_error_handling(&error_handling);

RETURN_LONG(intern->type);
} /* }}} */
Expand All @@ -160,13 +176,17 @@ static PHP_METHOD(Binary, getType)
*/
static PHP_METHOD(Binary, jsonSerialize)
{
zend_error_handling error_handling;
php_phongo_binary_t* intern;
char type[3];
int type_len;

zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);
if (zend_parse_parameters_none() == FAILURE) {
zend_restore_error_handling(&error_handling);
return;
}
zend_restore_error_handling(&error_handling);

intern = Z_BINARY_OBJ_P(getThis());

Expand All @@ -186,16 +206,20 @@ static PHP_METHOD(Binary, jsonSerialize)
*/
static PHP_METHOD(Binary, serialize)
{
zend_error_handling error_handling;
php_phongo_binary_t* intern;
zval retval;
php_serialize_data_t var_hash;
smart_str buf = { 0 };

intern = Z_BINARY_OBJ_P(getThis());

zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);
if (zend_parse_parameters_none() == FAILURE) {
zend_restore_error_handling(&error_handling);
return;
}
zend_restore_error_handling(&error_handling);

array_init_size(&retval, 2);
ADD_ASSOC_STRINGL(&retval, "data", intern->data, intern->data_len);
Expand All @@ -216,8 +240,8 @@ static PHP_METHOD(Binary, serialize)
*/
static PHP_METHOD(Binary, unserialize)
{
php_phongo_binary_t* intern;
zend_error_handling error_handling;
php_phongo_binary_t* intern;
char* serialized;
size_t serialized_len;
zval props;
Expand All @@ -226,7 +250,6 @@ static PHP_METHOD(Binary, unserialize)
intern = Z_BINARY_OBJ_P(getThis());

zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);

if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &serialized, &serialized_len) == FAILURE) {
zend_restore_error_handling(&error_handling);
return;
Expand Down
15 changes: 13 additions & 2 deletions src/BSON/DBPointer.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,17 @@ static bool php_phongo_dbpointer_init_from_hash(php_phongo_dbpointer_t* intern,
Return the DBPointer's namespace string and ObjectId. */
static PHP_METHOD(DBPointer, __toString)
{
zend_error_handling error_handling;
php_phongo_dbpointer_t* intern;
char* retval;
int retval_len;

zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);
if (zend_parse_parameters_none() == FAILURE) {
zend_restore_error_handling(&error_handling);
return;
}
zend_restore_error_handling(&error_handling);

intern = Z_DBPOINTER_OBJ_P(getThis());

Expand All @@ -90,13 +94,17 @@ static PHP_METHOD(DBPointer, __toString)
*/
static PHP_METHOD(DBPointer, jsonSerialize)
{
zend_error_handling error_handling;
php_phongo_dbpointer_t* intern;
zval zdb_pointer;
zval zoid;

zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);
if (zend_parse_parameters_none() == FAILURE) {
zend_restore_error_handling(&error_handling);
return;
}
zend_restore_error_handling(&error_handling);

intern = Z_DBPOINTER_OBJ_P(getThis());

Expand All @@ -114,16 +122,20 @@ static PHP_METHOD(DBPointer, jsonSerialize)
*/
static PHP_METHOD(DBPointer, serialize)
{
zend_error_handling error_handling;
php_phongo_dbpointer_t* intern;
zval retval;
php_serialize_data_t var_hash;
smart_str buf = { 0 };

intern = Z_DBPOINTER_OBJ_P(getThis());

zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);
if (zend_parse_parameters_none() == FAILURE) {
zend_restore_error_handling(&error_handling);
return;
}
zend_restore_error_handling(&error_handling);

array_init_size(&retval, 2);
ADD_ASSOC_STRINGL(&retval, "ref", intern->ref, intern->ref_len);
Expand All @@ -144,8 +156,8 @@ static PHP_METHOD(DBPointer, serialize)
*/
static PHP_METHOD(DBPointer, unserialize)
{
php_phongo_dbpointer_t* intern;
zend_error_handling error_handling;
php_phongo_dbpointer_t* intern;
char* serialized;
size_t serialized_len;
zval props;
Expand All @@ -154,7 +166,6 @@ static PHP_METHOD(DBPointer, unserialize)
intern = Z_DBPOINTER_OBJ_P(getThis());

zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);

if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &serialized, &serialized_len) == FAILURE) {
zend_restore_error_handling(&error_handling);
return;
Expand Down
25 changes: 20 additions & 5 deletions src/BSON/Decimal128.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ static bool php_phongo_decimal128_init_from_hash(php_phongo_decimal128_t* intern
Construct a new BSON Decimal128 type */
static PHP_METHOD(Decimal128, __construct)
{
php_phongo_decimal128_t* intern;
zend_error_handling error_handling;
php_phongo_decimal128_t* intern;
char* value;
size_t value_len;

zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);
intern = Z_DECIMAL128_OBJ_P(getThis());

zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &value, &value_len) == FAILURE) {
zend_restore_error_handling(&error_handling);
return;
Expand All @@ -81,13 +81,17 @@ static PHP_METHOD(Decimal128, __construct)
*/
static PHP_METHOD(Decimal128, __set_state)
{
zend_error_handling error_handling;
php_phongo_decimal128_t* intern;
HashTable* props;
zval* array;

zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);
if (zend_parse_parameters(ZEND_NUM_ARGS(), "a", &array) == FAILURE) {
RETURN_FALSE;
zend_restore_error_handling(&error_handling);
return;
}
zend_restore_error_handling(&error_handling);

object_init_ex(return_value, php_phongo_decimal128_ce);

Expand All @@ -101,14 +105,18 @@ static PHP_METHOD(Decimal128, __set_state)
*/
static PHP_METHOD(Decimal128, __toString)
{
zend_error_handling error_handling;
php_phongo_decimal128_t* intern;
char outbuf[BSON_DECIMAL128_STRING];

intern = Z_DECIMAL128_OBJ_P(getThis());

zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);
if (zend_parse_parameters_none() == FAILURE) {
zend_restore_error_handling(&error_handling);
return;
}
zend_restore_error_handling(&error_handling);

bson_decimal128_to_string(&intern->decimal, outbuf);

Expand All @@ -119,12 +127,16 @@ static PHP_METHOD(Decimal128, __toString)
*/
static PHP_METHOD(Decimal128, jsonSerialize)
{
zend_error_handling error_handling;
php_phongo_decimal128_t* intern;
char outbuf[BSON_DECIMAL128_STRING] = "";

zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);
if (zend_parse_parameters_none() == FAILURE) {
zend_restore_error_handling(&error_handling);
return;
}
zend_restore_error_handling(&error_handling);

intern = Z_DECIMAL128_OBJ_P(getThis());

Expand All @@ -137,6 +149,7 @@ static PHP_METHOD(Decimal128, jsonSerialize)
*/
static PHP_METHOD(Decimal128, serialize)
{
zend_error_handling error_handling;
php_phongo_decimal128_t* intern;
zval retval;
php_serialize_data_t var_hash;
Expand All @@ -145,9 +158,12 @@ static PHP_METHOD(Decimal128, serialize)

intern = Z_DECIMAL128_OBJ_P(getThis());

zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);
if (zend_parse_parameters_none() == FAILURE) {
zend_restore_error_handling(&error_handling);
return;
}
zend_restore_error_handling(&error_handling);

bson_decimal128_to_string(&intern->decimal, outbuf);
array_init_size(&retval, 1);
Expand All @@ -168,8 +184,8 @@ static PHP_METHOD(Decimal128, serialize)
*/
static PHP_METHOD(Decimal128, unserialize)
{
php_phongo_decimal128_t* intern;
zend_error_handling error_handling;
php_phongo_decimal128_t* intern;
char* serialized;
size_t serialized_len;
zval props;
Expand All @@ -178,7 +194,6 @@ static PHP_METHOD(Decimal128, unserialize)
intern = Z_DECIMAL128_OBJ_P(getThis());

zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);

if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &serialized, &serialized_len) == FAILURE) {
zend_restore_error_handling(&error_handling);
return;
Expand Down
Loading