Skip to content

PHPC-2111: Replace zend_parse_parameters calls with macros #1338

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 4 commits into from
Jul 26, 2022
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
9 changes: 1 addition & 8 deletions php_phongo.c
Original file line number Diff line number Diff line change
Expand Up @@ -482,14 +482,7 @@ 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_PARSE_PARAMETERS_NONE();

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

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;
}
zend_restore_error_handling(&error_handling);
PHONGO_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_STRING(data, data_len)
Z_PARAM_LONG(type)
PHONGO_PARSE_PARAMETERS_END();

php_phongo_binary_init(intern, data, data_len, type);
} /* }}} */
Expand All @@ -116,17 +113,13 @@ 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) {
zend_restore_error_handling(&error_handling);
return;
}
zend_restore_error_handling(&error_handling);
PHONGO_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ARRAY(array)
PHONGO_PARSE_PARAMETERS_END();

object_init_ex(return_value, php_phongo_binary_ce);

Expand All @@ -140,15 +133,9 @@ 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);
PHONGO_PARSE_PARAMETERS_NONE();

intern = Z_BINARY_OBJ_P(getThis());

Expand All @@ -159,17 +146,11 @@ 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);
PHONGO_PARSE_PARAMETERS_NONE();

RETURN_STRINGL(intern->data, intern->data_len);
} /* }}} */
Expand All @@ -178,17 +159,11 @@ 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);
PHONGO_PARSE_PARAMETERS_NONE();

RETURN_LONG(intern->type);
} /* }}} */
Expand All @@ -197,17 +172,11 @@ 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);
PHONGO_PARSE_PARAMETERS_NONE();

intern = Z_BINARY_OBJ_P(getThis());

Expand All @@ -227,20 +196,14 @@ 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);
PHONGO_PARSE_PARAMETERS_NONE();

array_init_size(&retval, 2);
ADD_ASSOC_STRINGL(&retval, "data", intern->data, intern->data_len);
Expand All @@ -261,7 +224,6 @@ static PHP_METHOD(Binary, serialize)
*/
static PHP_METHOD(Binary, unserialize)
{
zend_error_handling error_handling;
php_phongo_binary_t* intern;
char* serialized;
size_t serialized_len;
Expand All @@ -270,12 +232,9 @@ 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;
}
zend_restore_error_handling(&error_handling);
PHONGO_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STRING(serialized, serialized_len)
PHONGO_PARSE_PARAMETERS_END();

PHP_VAR_UNSERIALIZE_INIT(var_hash);
if (!php_var_unserialize(&props, (const unsigned char**) &serialized, (unsigned char*) serialized + serialized_len, &var_hash)) {
Expand Down
34 changes: 6 additions & 28 deletions src/BSON/DBPointer.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,11 @@ HashTable* php_phongo_dbpointer_get_properties_hash(phongo_compat_object_handler
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);
PHONGO_PARSE_PARAMETERS_NONE();

intern = Z_DBPOINTER_OBJ_P(getThis());

Expand All @@ -117,17 +111,11 @@ 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);
PHONGO_PARSE_PARAMETERS_NONE();

intern = Z_DBPOINTER_OBJ_P(getThis());

Expand All @@ -145,20 +133,14 @@ 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);
PHONGO_PARSE_PARAMETERS_NONE();

array_init_size(&retval, 2);
ADD_ASSOC_STRINGL(&retval, "ref", intern->ref, intern->ref_len);
Expand All @@ -179,7 +161,6 @@ static PHP_METHOD(DBPointer, serialize)
*/
static PHP_METHOD(DBPointer, unserialize)
{
zend_error_handling error_handling;
php_phongo_dbpointer_t* intern;
char* serialized;
size_t serialized_len;
Expand All @@ -188,12 +169,9 @@ 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;
}
zend_restore_error_handling(&error_handling);
PHONGO_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STRING(serialized, serialized_len)
PHONGO_PARSE_PARAMETERS_END();

PHP_VAR_UNSERIALIZE_INIT(var_hash);
if (!php_var_unserialize(&props, (const unsigned char**) &serialized, (unsigned char*) serialized + serialized_len, &var_hash)) {
Expand Down
54 changes: 12 additions & 42 deletions src/BSON/Decimal128.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,15 @@ static HashTable* php_phongo_decimal128_get_properties_hash(phongo_compat_object
Construct a new BSON Decimal128 type */
static PHP_METHOD(Decimal128, __construct)
{
zend_error_handling error_handling;
php_phongo_decimal128_t* intern;
char* value;
size_t value_len;

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;
}
zend_restore_error_handling(&error_handling);
PHONGO_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STRING(value, value_len)
PHONGO_PARSE_PARAMETERS_END();

php_phongo_decimal128_init(intern, value);
} /* }}} */
Expand All @@ -105,17 +101,13 @@ 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) {
zend_restore_error_handling(&error_handling);
return;
}
zend_restore_error_handling(&error_handling);
PHONGO_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ARRAY(array)
PHONGO_PARSE_PARAMETERS_END();

object_init_ex(return_value, php_phongo_decimal128_ce);

Expand All @@ -129,18 +121,12 @@ 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);
PHONGO_PARSE_PARAMETERS_NONE();

bson_decimal128_to_string(&intern->decimal, outbuf);

Expand All @@ -151,16 +137,10 @@ 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);
PHONGO_PARSE_PARAMETERS_NONE();

intern = Z_DECIMAL128_OBJ_P(getThis());

Expand All @@ -173,7 +153,6 @@ 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 @@ -182,12 +161,7 @@ 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);
PHONGO_PARSE_PARAMETERS_NONE();

bson_decimal128_to_string(&intern->decimal, outbuf);
array_init_size(&retval, 1);
Expand All @@ -208,7 +182,6 @@ static PHP_METHOD(Decimal128, serialize)
*/
static PHP_METHOD(Decimal128, unserialize)
{
zend_error_handling error_handling;
php_phongo_decimal128_t* intern;
char* serialized;
size_t serialized_len;
Expand All @@ -217,12 +190,9 @@ 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;
}
zend_restore_error_handling(&error_handling);
PHONGO_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STRING(serialized, serialized_len)
PHONGO_PARSE_PARAMETERS_END();

PHP_VAR_UNSERIALIZE_INIT(var_hash);
if (!php_var_unserialize(&props, (const unsigned char**) &serialized, (unsigned char*) serialized + serialized_len, &var_hash)) {
Expand Down
Loading