-
Notifications
You must be signed in to change notification settings - Fork 209
PHPC-741: Consistent exceptions for BSON init methods #346
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
Changes from all commits
5c65399
7d3dadb
e38c21d
3cd5857
6825553
04765b4
160c65d
9c514b5
f1e057d
40f2603
b6754f6
66822bf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,10 +46,12 @@ PHONGO_API zend_class_entry *php_phongo_binary_ce; | |
|
||
zend_object_handlers php_phongo_handler_binary; | ||
|
||
/* Initialize the object from a string and return whether it was successful. */ | ||
static bool php_phongo_binary_init(php_phongo_binary_t *intern, const char *data, phongo_zpp_char_len data_len, phongo_long type) | ||
/* Initialize the object and return whether it was successful. An exception will | ||
* be thrown on error. */ | ||
static bool php_phongo_binary_init(php_phongo_binary_t *intern, const char *data, phongo_zpp_char_len data_len, phongo_long type TSRMLS_DC) | ||
{ | ||
if (type < 0 || type > UINT8_MAX) { | ||
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected type to be an unsigned 8-bit integer, %" PHONGO_LONG_FORMAT " given", type); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This message was previously only used in the constructor; however, the exception may now be thrown from I've started doing this with other new messages (e.g. Javascript now allowing null bytes in its code argument) for added clarity, but did not want to change existing messages before discussing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To clarify, my main concern was with
Output:
Given this behavior, I think we're OK with not having the class name in every message. |
||
return false; | ||
} | ||
|
||
|
@@ -60,24 +62,27 @@ static bool php_phongo_binary_init(php_phongo_binary_t *intern, const char *data | |
return true; | ||
} | ||
|
||
/* Initialize the object from a HashTable and return whether it was successful. */ | ||
static bool php_phongo_binary_init_from_hash(php_phongo_binary_t *intern, HashTable *props) | ||
/* Initialize the object from a HashTable and return whether it was successful. | ||
* An exception will be thrown on error. */ | ||
static bool php_phongo_binary_init_from_hash(php_phongo_binary_t *intern, HashTable *props TSRMLS_DC) | ||
{ | ||
#if PHP_VERSION_ID >= 70000 | ||
zval *data, *type; | ||
|
||
if ((data = zend_hash_str_find(props, "data", sizeof("data")-1)) && Z_TYPE_P(data) == IS_STRING && | ||
(type = zend_hash_str_find(props, "type", sizeof("type")-1)) && Z_TYPE_P(type) == IS_LONG) { | ||
return php_phongo_binary_init(intern, Z_STRVAL_P(data), Z_STRLEN_P(data), Z_LVAL_P(type)); | ||
return php_phongo_binary_init(intern, Z_STRVAL_P(data), Z_STRLEN_P(data), Z_LVAL_P(type) TSRMLS_CC); | ||
} | ||
#else | ||
zval **data, **type; | ||
|
||
if (zend_hash_find(props, "data", sizeof("data"), (void**) &data) == SUCCESS && Z_TYPE_PP(data) == IS_STRING && | ||
zend_hash_find(props, "type", sizeof("type"), (void**) &type) == SUCCESS && Z_TYPE_PP(type) == IS_LONG) { | ||
return php_phongo_binary_init(intern, Z_STRVAL_PP(data), Z_STRLEN_PP(data), Z_LVAL_PP(type)); | ||
return php_phongo_binary_init(intern, Z_STRVAL_PP(data), Z_STRLEN_PP(data), Z_LVAL_PP(type) TSRMLS_CC); | ||
} | ||
#endif | ||
|
||
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "%s initialization requires \"data\" string and \"type\" integer fields", ZSTR_VAL(php_phongo_binary_ce->name)); | ||
return false; | ||
} | ||
|
||
|
@@ -101,9 +106,7 @@ PHP_METHOD(Binary, __construct) | |
} | ||
zend_restore_error_handling(&error_handling TSRMLS_CC); | ||
|
||
if (!php_phongo_binary_init(intern, data, data_len, type)) { | ||
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected type to be an unsigned 8-bit integer, %" PHONGO_LONG_FORMAT " given", type); | ||
} | ||
php_phongo_binary_init(intern, data, data_len, type TSRMLS_CC); | ||
} | ||
/* }}} */ | ||
|
||
|
@@ -124,9 +127,7 @@ PHP_METHOD(Binary, __set_state) | |
intern = Z_BINARY_OBJ_P(return_value); | ||
props = Z_ARRVAL_P(array); | ||
|
||
if (!php_phongo_binary_init_from_hash(intern, props)) { | ||
php_error(E_ERROR, "Invalid serialization data for Binary object"); | ||
} | ||
php_phongo_binary_init_from_hash(intern, props TSRMLS_CC); | ||
} | ||
/* }}} */ | ||
|
||
|
@@ -144,9 +145,7 @@ PHP_METHOD(Binary, __wakeup) | |
intern = Z_BINARY_OBJ_P(getThis()); | ||
props = zend_std_get_properties(getThis() TSRMLS_CC); | ||
|
||
if (!php_phongo_binary_init_from_hash(intern, props)) { | ||
php_error(E_ERROR, "Invalid serialization data for Binary object"); | ||
} | ||
php_phongo_binary_init_from_hash(intern, props TSRMLS_CC); | ||
} | ||
/* }}} */ | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,34 +46,39 @@ PHONGO_API zend_class_entry *php_phongo_decimal128_ce; | |
|
||
zend_object_handlers php_phongo_handler_decimal128; | ||
|
||
/* Initialize the object from a string and return whether it was successful. */ | ||
static bool php_phongo_decimal128_init(php_phongo_decimal128_t *intern, const char *value) | ||
/* Initialize the object and return whether it was successful. An exception will | ||
* be thrown on error. */ | ||
static bool php_phongo_decimal128_init(php_phongo_decimal128_t *intern, const char *value TSRMLS_DC) | ||
{ | ||
if (bson_decimal128_from_string(value, &intern->decimal)) { | ||
intern->initialized = true; | ||
|
||
return true; | ||
if (!bson_decimal128_from_string(value, &intern->decimal)) { | ||
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Error parsing decimal string: %s", value); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
return false; | ||
} | ||
|
||
return false; | ||
intern->initialized = true; | ||
|
||
return true; | ||
} | ||
|
||
/* Initialize the object from a HashTable and return whether it was successful. */ | ||
static bool php_phongo_decimal128_init_from_hash(php_phongo_decimal128_t *intern, HashTable *props) | ||
/* Initialize the object from a HashTable and return whether it was successful. | ||
* An exception will be thrown on error. */ | ||
static bool php_phongo_decimal128_init_from_hash(php_phongo_decimal128_t *intern, HashTable *props TSRMLS_DC) | ||
{ | ||
#if PHP_VERSION_ID >= 70000 | ||
zval *dec; | ||
|
||
if ((dec = zend_hash_str_find(props, "dec", sizeof("dec")-1)) && Z_TYPE_P(dec) == IS_STRING) { | ||
return php_phongo_decimal128_init(intern, Z_STRVAL_P(dec)); | ||
return php_phongo_decimal128_init(intern, Z_STRVAL_P(dec) TSRMLS_CC); | ||
} | ||
#else | ||
zval **dec; | ||
|
||
if (zend_hash_find(props, "dec", sizeof("dec"), (void**) &dec) == SUCCESS && Z_TYPE_PP(dec) == IS_STRING) { | ||
return php_phongo_decimal128_init(intern, Z_STRVAL_PP(dec)); | ||
return php_phongo_decimal128_init(intern, Z_STRVAL_PP(dec) TSRMLS_CC); | ||
} | ||
#endif | ||
|
||
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "%s initialization requires \"dec\" string field", ZSTR_VAL(php_phongo_decimal128_ce->name)); | ||
return false; | ||
} | ||
|
||
|
@@ -96,9 +101,7 @@ PHP_METHOD(Decimal128, __construct) | |
} | ||
zend_restore_error_handling(&error_handling TSRMLS_CC); | ||
|
||
if (!php_phongo_decimal128_init(intern, value)) { | ||
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Error parsing decimal string: %s", value); | ||
} | ||
php_phongo_decimal128_init(intern, value TSRMLS_CC); | ||
} | ||
/* }}} */ | ||
|
||
|
@@ -119,9 +122,7 @@ PHP_METHOD(Decimal128, __set_state) | |
intern = Z_DECIMAL128_OBJ_P(return_value); | ||
props = Z_ARRVAL_P(array); | ||
|
||
if (!php_phongo_decimal128_init_from_hash(intern, props)) { | ||
php_error(E_ERROR, "Invalid serialization data for Decimal128 object"); | ||
} | ||
php_phongo_decimal128_init_from_hash(intern, props TSRMLS_CC); | ||
} | ||
/* }}} */ | ||
|
||
|
@@ -158,9 +159,7 @@ PHP_METHOD(Decimal128, __wakeup) | |
intern = Z_DECIMAL128_OBJ_P(getThis()); | ||
props = zend_std_get_properties(getThis() TSRMLS_CC); | ||
|
||
if (!php_phongo_decimal128_init_from_hash(intern, props)) { | ||
php_error(E_ERROR, "Invalid serialization data for Decimal128 object"); | ||
} | ||
php_phongo_decimal128_init_from_hash(intern, props TSRMLS_CC); | ||
} | ||
/* }}} */ | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A number of the exception messages for PHPC-741 involved reporting
zend_class_entry->name
. On PHP 7, this required accessingzend_string->val
. Digging a little deeper, I found that we were inconsistently using either thephongo_str()
macro or using#if
blocks to conditionally useZSTR_VAL()
for PHP 7. Simply backportingZSTR_VAL()
to PHP 5.x allowed me to remove our custom macro and delete those#if
blocks.