Skip to content

ext/date: Throw exceptions directly #19166

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
62 changes: 33 additions & 29 deletions ext/date/php_date.c
Original file line number Diff line number Diff line change
Expand Up @@ -3213,7 +3213,7 @@ PHP_FUNCTION(date_format)
}
/* }}} */

static bool php_date_modify(zval *object, char *modify, size_t modify_len) /* {{{ */
static bool php_date_modify(zval *object, char *modify, size_t modify_len, const bool should_throw) /* {{{ */
{
php_date_obj *dateobj;
timelib_time *tmp_time;
Expand All @@ -3233,10 +3233,22 @@ static bool php_date_modify(zval *object, char *modify, size_t modify_len) /* {{

if (err && err->error_count) {
/* spit out the first library error message, at least */
php_error_docref(NULL, E_WARNING, "Failed to parse time string (%s) at position %d (%c): %s", modify,
err->error_messages[0].position,
err->error_messages[0].character ? err->error_messages[0].character : ' ',
err->error_messages[0].message);
if (should_throw) {
zend_string *func_name = get_active_function_or_method_name();
zend_throw_exception_ex(date_ce_date_malformed_string_exception, 0,
"%s(): Failed to parse time string (%s) at position %d (%c): %s",
ZSTR_VAL(func_name),
modify,
err->error_messages[0].position,
err->error_messages[0].character ? err->error_messages[0].character : ' ',
err->error_messages[0].message);
zend_string_release_ex(func_name, false);
} else {
php_error_docref(NULL, E_WARNING, "Failed to parse time string (%s) at position %d (%c): %s", modify,
err->error_messages[0].position,
err->error_messages[0].character ? err->error_messages[0].character : ' ',
err->error_messages[0].message);
}
timelib_time_dtor(tmp_time);
return 0;
}
Expand Down Expand Up @@ -3305,7 +3317,7 @@ PHP_FUNCTION(date_modify)
RETURN_THROWS();
}

if (!php_date_modify(object, modify, modify_len)) {
if (!php_date_modify(object, modify, modify_len, false)) {
RETURN_FALSE;
}

Expand All @@ -3319,21 +3331,16 @@ PHP_METHOD(DateTime, modify)
zval *object;
char *modify;
size_t modify_len;
zend_error_handling zeh;

object = ZEND_THIS;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STRING(modify, modify_len)
ZEND_PARSE_PARAMETERS_END();

zend_replace_error_handling(EH_THROW, date_ce_date_malformed_string_exception, &zeh);
if (!php_date_modify(object, modify, modify_len)) {
zend_restore_error_handling(&zeh);
if (!php_date_modify(object, modify, modify_len, true)) {
RETURN_THROWS();
}

zend_restore_error_handling(&zeh);

RETURN_OBJ_COPY(Z_OBJ_P(object));
}
/* }}} */
Expand All @@ -3344,7 +3351,6 @@ PHP_METHOD(DateTimeImmutable, modify)
zval *object, new_object;
char *modify;
size_t modify_len;
zend_error_handling zeh;

object = ZEND_THIS;
ZEND_PARSE_PARAMETERS_START(1, 1)
Expand All @@ -3353,15 +3359,11 @@ PHP_METHOD(DateTimeImmutable, modify)

date_clone_immutable(object, &new_object);

zend_replace_error_handling(EH_THROW, date_ce_date_malformed_string_exception, &zeh);
if (!php_date_modify(&new_object, modify, modify_len)) {
if (!php_date_modify(&new_object, modify, modify_len, true)) {
zval_ptr_dtor(&new_object);
zend_restore_error_handling(&zeh);
RETURN_THROWS();
}

zend_restore_error_handling(&zeh);

RETURN_OBJ(Z_OBJ(new_object));
}
/* }}} */
Expand Down Expand Up @@ -3418,7 +3420,7 @@ PHP_METHOD(DateTimeImmutable, add)
}
/* }}} */

static void php_date_sub(zval *object, zval *interval, zval *return_value) /* {{{ */
static void php_date_sub(zval *object, zval *interval, zval *return_value, const bool should_throw) /* {{{ */
{
php_date_obj *dateobj;
php_interval_obj *intobj;
Expand All @@ -3430,7 +3432,15 @@ static void php_date_sub(zval *object, zval *interval, zval *return_value) /* {{
DATE_CHECK_INITIALIZED(intobj->initialized, Z_OBJCE_P(interval));

if (intobj->diff->have_weekday_relative || intobj->diff->have_special_relative) {
php_error_docref(NULL, E_WARNING, "Only non-special relative time specifications are supported for subtraction");
if (should_throw) {
zend_string *func_name = get_active_function_or_method_name();
zend_throw_exception_ex(date_ce_date_invalid_operation_exception, 0,
"%s(): Only non-special relative time specifications are supported for subtraction",
ZSTR_VAL(func_name));
zend_string_release_ex(func_name, false);
} else {
php_error_docref(NULL, E_WARNING, "Only non-special relative time specifications are supported for subtraction");
}
return;
}

Expand All @@ -3452,7 +3462,7 @@ PHP_FUNCTION(date_sub)
RETURN_THROWS();
}

php_date_sub(object, interval, return_value);
php_date_sub(object, interval, return_value, false);
RETURN_OBJ_COPY(Z_OBJ_P(object));
}
/* }}} */
Expand All @@ -3461,15 +3471,12 @@ PHP_FUNCTION(date_sub)
PHP_METHOD(DateTime, sub)
{
zval *object, *interval;
zend_error_handling zeh;

if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "OO", &object, date_ce_date, &interval, date_ce_interval) == FAILURE) {
RETURN_THROWS();
}

zend_replace_error_handling(EH_THROW, date_ce_date_invalid_operation_exception, &zeh);
php_date_sub(object, interval, return_value);
zend_restore_error_handling(&zeh);
php_date_sub(object, interval, return_value, true);

RETURN_OBJ_COPY(Z_OBJ_P(object));
}
Expand All @@ -3479,7 +3486,6 @@ PHP_METHOD(DateTime, sub)
PHP_METHOD(DateTimeImmutable, sub)
{
zval *object, *interval, new_object;
zend_error_handling zeh;

object = ZEND_THIS;
ZEND_PARSE_PARAMETERS_START(1, 1)
Expand All @@ -3488,9 +3494,7 @@ PHP_METHOD(DateTimeImmutable, sub)

date_clone_immutable(object, &new_object);

zend_replace_error_handling(EH_THROW, date_ce_date_invalid_operation_exception, &zeh);
php_date_sub(&new_object, interval, return_value);
zend_restore_error_handling(&zeh);
php_date_sub(&new_object, interval, return_value, true);

RETURN_OBJ(Z_OBJ(new_object));
}
Expand Down