Skip to content
Closed
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
4 changes: 2 additions & 2 deletions Zend/zend.c
Original file line number Diff line number Diff line change
Expand Up @@ -1298,14 +1298,14 @@ ZEND_API void zend_type_error(const char *format, ...) /* {{{ */
va_end(va);
} /* }}} */

ZEND_API void zend_internal_type_error(zend_bool strict, const char *format, ...) /* {{{ */
ZEND_API void zend_internal_type_error(zend_bool throw_exception, const char *format, ...) /* {{{ */
{
va_list va;
char *message = NULL;

va_start(va, format);
zend_vspprintf(&message, 0, format, va);
if (strict) {
if (throw_exception) {
zend_throw_exception(zend_get_type_exception(), message, E_ERROR);
} else {
zend_error(E_WARNING, message);
Expand Down
2 changes: 1 addition & 1 deletion Zend/zend.h
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ extern ZEND_API zend_string *(*zend_resolve_path)(const char *filename, int file

ZEND_API void zend_error(int type, const char *format, ...) ZEND_ATTRIBUTE_FORMAT(printf, 2, 3);
ZEND_API void zend_type_error(const char *format, ...);
ZEND_API void zend_internal_type_error(zend_bool strict, const char *format, ...);
ZEND_API void zend_internal_type_error(zend_bool throw_exception, const char *format, ...);

void zenderror(const char *error);

Expand Down
44 changes: 33 additions & 11 deletions Zend/zend_API.c
Original file line number Diff line number Diff line change
Expand Up @@ -761,13 +761,16 @@ static int zend_parse_arg(int arg_num, zval *arg, va_list *va, const char **spec
if (!(flags & ZEND_PARSE_PARAMS_QUIET) && (*expected_type || error)) {
const char *space;
const char *class_name = get_active_class_name(&space);
zend_bool throw_exception =
ZEND_ARG_USES_STRICT_TYPES() || (flags & ZEND_PARSE_PARAMS_THROW);

if (error) {
zend_internal_type_error(ZEND_ARG_USES_STRICT_TYPES(), "%s%s%s() expects parameter %d %s",
class_name, space, get_active_function_name(), arg_num, error);
zend_internal_type_error(throw_exception, "%s%s%s() expects parameter %d %s",
class_name, space, get_active_function_name(), arg_num, error);
efree(error);
} else {
zend_internal_type_error(ZEND_ARG_USES_STRICT_TYPES(), "%s%s%s() expects parameter %d to be %s, %s given",
zend_internal_type_error(throw_exception,
"%s%s%s() expects parameter %d to be %s, %s given",
class_name, space, get_active_function_name(), arg_num, expected_type,
zend_zval_type_name(arg));
}
Expand Down Expand Up @@ -881,7 +884,9 @@ static int zend_parse_va_args(int num_args, const char *type_spec, va_list *va,
if (!(flags & ZEND_PARSE_PARAMS_QUIET)) {
zend_function *active_function = EG(current_execute_data)->func;
const char *class_name = active_function->common.scope ? active_function->common.scope->name->val : "";
zend_internal_type_error(ZEND_ARG_USES_STRICT_TYPES(), "%s%s%s() expects %s %d parameter%s, %d given",
zend_bool throw_exception =
ZEND_ARG_USES_STRICT_TYPES() || (flags & ZEND_PARSE_PARAMS_THROW);
zend_internal_type_error(throw_exception, "%s%s%s() expects %s %d parameter%s, %d given",
class_name,
class_name[0] ? "::" : "",
active_function->common.function_name->val,
Expand Down Expand Up @@ -944,18 +949,19 @@ static int zend_parse_va_args(int num_args, const char *type_spec, va_list *va,
}
/* }}} */

#define RETURN_IF_ZERO_ARGS(num_args, type_spec, flags) { \
#define RETURN_IF_ZERO_ARGS(num_args, type_spec, flags) do { \
int __num_args = (num_args); \
\
if (0 == (type_spec)[0] && 0 != __num_args && !(flags & ZEND_PARSE_PARAMS_QUIET)) { \
const char *__space; \
const char * __class_name = get_active_class_name(&__space); \
zend_internal_type_error(ZEND_ARG_USES_STRICT_TYPES(), "%s%s%s() expects exactly 0 parameters, %d given", \
__class_name, __space, \
get_active_function_name(), __num_args); \
zend_bool throw_exception = \
ZEND_ARG_USES_STRICT_TYPES() || (flags & ZEND_PARSE_PARAMS_THROW); \
zend_internal_type_error(throw_exception, \
"%s%s%s() expects exactly 0 parameters, %d given", \
__class_name, __space, get_active_function_name(), __num_args); \
return FAILURE; \
}\
}
} \
} while(0)

ZEND_API int zend_parse_parameters_ex(int flags, int num_args, const char *type_spec, ...) /* {{{ */
{
Expand Down Expand Up @@ -988,6 +994,22 @@ ZEND_API int zend_parse_parameters(int num_args, const char *type_spec, ...) /*
}
/* }}} */

ZEND_API int zend_parse_parameters_throw(int num_args, const char *type_spec, ...) /* {{{ */
{
va_list va;
int retval;
int flags = ZEND_PARSE_PARAMS_THROW;

RETURN_IF_ZERO_ARGS(num_args, type_spec, flags);

va_start(va, type_spec);
retval = zend_parse_va_args(num_args, type_spec, &va, flags);
va_end(va);

return retval;
}
/* }}} */

ZEND_API int zend_parse_method_parameters(int num_args, zval *this_ptr, const char *type_spec, ...) /* {{{ */
{
va_list va;
Expand Down
2 changes: 2 additions & 0 deletions Zend/zend_API.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,10 @@ ZEND_API int zend_copy_parameters_array(int param_count, zval *argument_array);
/* Parameter parsing API -- andrei */

#define ZEND_PARSE_PARAMS_QUIET (1<<1)
#define ZEND_PARSE_PARAMS_THROW (1<<2)
ZEND_API int zend_parse_parameters(int num_args, const char *type_spec, ...);
ZEND_API int zend_parse_parameters_ex(int flags, int num_args, const char *type_spec, ...);
ZEND_API int zend_parse_parameters_throw(int num_args, const char *type_spec, ...);
ZEND_API char *zend_zval_type_name(const zval *arg);

ZEND_API int zend_parse_method_parameters(int num_args, zval *this_ptr, const char *type_spec, ...);
Expand Down
40 changes: 24 additions & 16 deletions ext/date/php_date.c
Original file line number Diff line number Diff line change
Expand Up @@ -2663,10 +2663,12 @@ PHP_METHOD(DateTime, __construct)
size_t time_str_len = 0;
zend_error_handling error_handling;

zend_replace_error_handling(EH_THROW, NULL, &error_handling);
if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|sO!", &time_str, &time_str_len, &timezone_object, date_ce_timezone)) {
php_date_initialize(Z_PHPDATE_P(getThis()), time_str, time_str_len, NULL, timezone_object, 1);
if (FAILURE == zend_parse_parameters_throw(ZEND_NUM_ARGS(), "|sO!", &time_str, &time_str_len, &timezone_object, date_ce_timezone)) {
return;
}

zend_replace_error_handling(EH_THROW, NULL, &error_handling);
php_date_initialize(Z_PHPDATE_P(getThis()), time_str, time_str_len, NULL, timezone_object, 1);
zend_restore_error_handling(&error_handling);
}
/* }}} */
Expand All @@ -2681,10 +2683,12 @@ PHP_METHOD(DateTimeImmutable, __construct)
size_t time_str_len = 0;
zend_error_handling error_handling;

zend_replace_error_handling(EH_THROW, NULL, &error_handling);
if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|sO!", &time_str, &time_str_len, &timezone_object, date_ce_timezone)) {
php_date_initialize(Z_PHPDATE_P(getThis()), time_str, time_str_len, NULL, timezone_object, 1);
if (FAILURE == zend_parse_parameters_throw(ZEND_NUM_ARGS(), "|sO!", &time_str, &time_str_len, &timezone_object, date_ce_timezone)) {
return;
}

zend_replace_error_handling(EH_THROW, NULL, &error_handling);
php_date_initialize(Z_PHPDATE_P(getThis()), time_str, time_str_len, NULL, timezone_object, 1);
zend_restore_error_handling(&error_handling);
}
/* }}} */
Expand Down Expand Up @@ -3641,11 +3645,13 @@ PHP_METHOD(DateTimeZone, __construct)
php_timezone_obj *tzobj;
zend_error_handling error_handling;

zend_replace_error_handling(EH_THROW, NULL, &error_handling);
if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "s", &tz, &tz_len)) {
tzobj = Z_PHPTIMEZONE_P(getThis());
timezone_initialize(tzobj, tz);
if (FAILURE == zend_parse_parameters_throw(ZEND_NUM_ARGS(), "s", &tz, &tz_len)) {
return;
}

zend_replace_error_handling(EH_THROW, NULL, &error_handling);
tzobj = Z_PHPTIMEZONE_P(getThis());
timezone_initialize(tzobj, tz);
zend_restore_error_handling(&error_handling);
}
/* }}} */
Expand Down Expand Up @@ -4070,13 +4076,15 @@ PHP_METHOD(DateInterval, __construct)
timelib_rel_time *reltime;
zend_error_handling error_handling;

if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "s", &interval_string, &interval_string_length) == FAILURE) {
return;
}

zend_replace_error_handling(EH_THROW, NULL, &error_handling);
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &interval_string, &interval_string_length) == SUCCESS) {
if (date_interval_initialize(&reltime, interval_string, interval_string_length) == SUCCESS) {
diobj = Z_PHPINTERVAL_P(getThis());
diobj->diff = reltime;
diobj->initialized = 1;
}
if (date_interval_initialize(&reltime, interval_string, interval_string_length) == SUCCESS) {
diobj = Z_PHPINTERVAL_P(getThis());
diobj->diff = reltime;
diobj->initialized = 1;
}
zend_restore_error_handling(&error_handling);
}
Expand Down
15 changes: 7 additions & 8 deletions ext/date/tests/DateTimeZone_construct_error.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,17 @@ echo "*** Testing DateTimeZone() : error conditions ***\n";
echo "\n-- Testing new DateTimeZone() with more than expected no. of arguments --\n";
$timezone = "GMT";
$extra_arg = 99;
var_dump( new DateTimeZone($timezone, $extra_arg) );
try {
new DateTimeZone($timezone, $extra_arg);
} catch (TypeException $e) {
echo $e->getMessage(), "\n";
}

?>
===DONE===
--EXPECTF--
*** Testing DateTimeZone() : error conditions ***

-- Testing new DateTimeZone() with more than expected no. of arguments --

Fatal error: Uncaught exception 'Exception' with message 'DateTimeZone::__construct() expects exactly 1 parameter, 2 given' in %s:%d
Stack trace:
#0 %s(%d): DateTimeZone->__construct('GMT', 99)
#1 {main}
thrown in %s on line %d

DateTimeZone::__construct() expects exactly 1 parameter, 2 given
===DONE===
2 changes: 1 addition & 1 deletion ext/date/tests/DateTimeZone_construct_variation1.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ foreach($inputs as $variation =>$timezone) {
echo "\n-- $variation --\n";
try {
var_dump( new DateTimezone($timezone) );
} catch(Exception $e) {
} catch (BaseException $e) {
$msg = $e->getMessage();
echo "FAILED: " . $msg . "\n";
}
Expand Down
14 changes: 7 additions & 7 deletions ext/date/tests/DateTime_construct_error.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ echo "\n-- Testing new DateTime() with more than expected no. of arguments --\n"
$time = "GMT";
$timezone = timezone_open("GMT");
$extra_arg = 99;
var_dump( new DateTime($time, $timezone, $extra_arg) );
try {
var_dump( new DateTime($time, $timezone, $extra_arg) );
} catch (TypeException $e) {
echo $e->getMessage(), "\n";
}

?>
===DONE===
--EXPECTF--
*** Testing date_create() : error conditions ***

-- Testing new DateTime() with more than expected no. of arguments --

Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct() expects at most 2 parameters, 3 given' in %s:%d
Stack trace:
#0 %s(%d): DateTime->__construct('GMT', Object(DateTimeZone), 99)
#1 {main}
thrown in %s on line %d
DateTime::__construct() expects at most 2 parameters, 3 given
===DONE===
4 changes: 2 additions & 2 deletions ext/date/tests/DateTime_construct_variation1.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,14 @@ foreach($inputs as $variation =>$time) {

try {
var_dump( new DateTime($time) );
} catch(Exception $e) {
} catch (BaseException $e) {
$msg = $e->getMessage();
echo "FAILED: " . $msg . "\n";
}

try {
var_dump( new DateTime($time, $timezone) );
} catch(Exception$e) {
} catch (BaseException$e) {
$msg = $e->getMessage();
echo "FAILED: " . $msg . "\n";
}
Expand Down
2 changes: 1 addition & 1 deletion ext/date/tests/DateTime_construct_variation2.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ foreach($inputs as $variation =>$timezone) {

try {
var_dump( new DateTime($time, $timezone) );
} catch(Exception $e) {
} catch (BaseException $e) {
$msg = $e->getMessage();
echo "FAILED: " . $msg . "\n";
}
Expand Down
8 changes: 2 additions & 6 deletions ext/dom/attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,17 @@ const zend_function_entry php_dom_attr_class_functions[] = {
/* {{{ proto void DOMAttr::__construct(string name, [string value]); */
PHP_METHOD(domattr, __construct)
{
zval *id;
zval *id = getThis();
xmlAttrPtr nodep = NULL;
xmlNodePtr oldnode = NULL;
dom_object *intern;
char *name, *value = NULL;
size_t name_len, value_len, name_valid;
zend_error_handling error_handling;

zend_replace_error_handling(EH_THROW, dom_domexception_class_entry, &error_handling);
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os|s", &id, dom_attr_class_entry, &name, &name_len, &value, &value_len) == FAILURE) {
zend_restore_error_handling(&error_handling);
if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "s|s", &name, &name_len, &value, &value_len) == FAILURE) {
return;
}

zend_restore_error_handling(&error_handling);
intern = Z_DOMOBJ_P(id);

name_valid = xmlValidateName((xmlChar *) name, 0);
Expand Down
8 changes: 2 additions & 6 deletions ext/dom/cdatasection.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,16 @@ const zend_function_entry php_dom_cdatasection_class_functions[] = {
PHP_METHOD(domcdatasection, __construct)
{

zval *id;
zval *id = getThis();
xmlNodePtr nodep = NULL, oldnode = NULL;
dom_object *intern;
char *value = NULL;
size_t value_len;
zend_error_handling error_handling;

zend_replace_error_handling(EH_THROW, dom_domexception_class_entry, &error_handling);
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &id, dom_cdatasection_class_entry, &value, &value_len) == FAILURE) {
zend_restore_error_handling(&error_handling);
if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "s", &value, &value_len) == FAILURE) {
return;
}

zend_restore_error_handling(&error_handling);
nodep = xmlNewCDataBlock(NULL, (xmlChar *) value, value_len);

if (!nodep) {
Expand Down
8 changes: 2 additions & 6 deletions ext/dom/comment.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,16 @@ const zend_function_entry php_dom_comment_class_functions[] = {
PHP_METHOD(domcomment, __construct)
{

zval *id;
zval *id = getThis();
xmlNodePtr nodep = NULL, oldnode = NULL;
dom_object *intern;
char *value = NULL;
size_t value_len;
zend_error_handling error_handling;

zend_replace_error_handling(EH_THROW, dom_domexception_class_entry, &error_handling);
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O|s", &id, dom_comment_class_entry, &value, &value_len) == FAILURE) {
zend_restore_error_handling(&error_handling);
if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "|s", &value, &value_len) == FAILURE) {
return;
}

zend_restore_error_handling(&error_handling);
nodep = xmlNewComment((xmlChar *) value);

if (!nodep) {
Expand Down
8 changes: 2 additions & 6 deletions ext/dom/document.c
Original file line number Diff line number Diff line change
Expand Up @@ -1253,21 +1253,17 @@ PHP_FUNCTION(dom_document_rename_node)
PHP_METHOD(domdocument, __construct)
{

zval *id;
zval *id = getThis();
xmlDoc *docp = NULL, *olddoc;
dom_object *intern;
char *encoding, *version = NULL;
size_t encoding_len = 0, version_len = 0;
int refcount;
zend_error_handling error_handling;

zend_replace_error_handling(EH_THROW, dom_domexception_class_entry, &error_handling);
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O|ss", &id, dom_document_class_entry, &version, &version_len, &encoding, &encoding_len) == FAILURE) {
zend_restore_error_handling(&error_handling);
if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "|ss", &version, &version_len, &encoding, &encoding_len) == FAILURE) {
return;
}

zend_restore_error_handling(&error_handling);
docp = xmlNewDoc((xmlChar *) version);

if (!docp) {
Expand Down
8 changes: 2 additions & 6 deletions ext/dom/documentfragment.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,14 @@ const zend_function_entry php_dom_documentfragment_class_functions[] = {
PHP_METHOD(domdocumentfragment, __construct)
{

zval *id;
zval *id = getThis();
xmlNodePtr nodep = NULL, oldnode = NULL;
dom_object *intern;
zend_error_handling error_handling;

zend_replace_error_handling(EH_THROW, dom_domexception_class_entry, &error_handling);
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &id, dom_documentfragment_class_entry) == FAILURE) {
zend_restore_error_handling(&error_handling);
if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "") == FAILURE) {
return;
}

zend_restore_error_handling(&error_handling);
nodep = xmlNewDocFragment(NULL);

if (!nodep) {
Expand Down
Loading