Skip to content

Commit 17759d6

Browse files
committed
ext/date: Throw exceptions directly
1 parent 6b3f302 commit 17759d6

File tree

1 file changed

+33
-29
lines changed

1 file changed

+33
-29
lines changed

ext/date/php_date.c

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3213,7 +3213,7 @@ PHP_FUNCTION(date_format)
32133213
}
32143214
/* }}} */
32153215

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

32343234
if (err && err->error_count) {
32353235
/* spit out the first library error message, at least */
3236-
php_error_docref(NULL, E_WARNING, "Failed to parse time string (%s) at position %d (%c): %s", modify,
3237-
err->error_messages[0].position,
3238-
err->error_messages[0].character ? err->error_messages[0].character : ' ',
3239-
err->error_messages[0].message);
3236+
if (should_throw) {
3237+
zend_string *func_name = get_active_function_or_method_name();
3238+
zend_throw_exception_ex(date_ce_date_malformed_string_exception, 0,
3239+
"%s(): Failed to parse time string (%s) at position %d (%c): %s",
3240+
ZSTR_VAL(func_name),
3241+
modify,
3242+
err->error_messages[0].position,
3243+
err->error_messages[0].character ? err->error_messages[0].character : ' ',
3244+
err->error_messages[0].message);
3245+
zend_string_release_ex(func_name, false);
3246+
} else {
3247+
php_error_docref(NULL, E_WARNING, "Failed to parse time string (%s) at position %d (%c): %s", modify,
3248+
err->error_messages[0].position,
3249+
err->error_messages[0].character ? err->error_messages[0].character : ' ',
3250+
err->error_messages[0].message);
3251+
}
32403252
timelib_time_dtor(tmp_time);
32413253
return 0;
32423254
}
@@ -3305,7 +3317,7 @@ PHP_FUNCTION(date_modify)
33053317
RETURN_THROWS();
33063318
}
33073319

3308-
if (!php_date_modify(object, modify, modify_len)) {
3320+
if (!php_date_modify(object, modify, modify_len, false)) {
33093321
RETURN_FALSE;
33103322
}
33113323

@@ -3319,21 +3331,16 @@ PHP_METHOD(DateTime, modify)
33193331
zval *object;
33203332
char *modify;
33213333
size_t modify_len;
3322-
zend_error_handling zeh;
33233334

33243335
object = ZEND_THIS;
33253336
ZEND_PARSE_PARAMETERS_START(1, 1)
33263337
Z_PARAM_STRING(modify, modify_len)
33273338
ZEND_PARSE_PARAMETERS_END();
33283339

3329-
zend_replace_error_handling(EH_THROW, date_ce_date_malformed_string_exception, &zeh);
3330-
if (!php_date_modify(object, modify, modify_len)) {
3331-
zend_restore_error_handling(&zeh);
3340+
if (!php_date_modify(object, modify, modify_len, true)) {
33323341
RETURN_THROWS();
33333342
}
33343343

3335-
zend_restore_error_handling(&zeh);
3336-
33373344
RETURN_OBJ_COPY(Z_OBJ_P(object));
33383345
}
33393346
/* }}} */
@@ -3344,7 +3351,6 @@ PHP_METHOD(DateTimeImmutable, modify)
33443351
zval *object, new_object;
33453352
char *modify;
33463353
size_t modify_len;
3347-
zend_error_handling zeh;
33483354

33493355
object = ZEND_THIS;
33503356
ZEND_PARSE_PARAMETERS_START(1, 1)
@@ -3353,15 +3359,11 @@ PHP_METHOD(DateTimeImmutable, modify)
33533359

33543360
date_clone_immutable(object, &new_object);
33553361

3356-
zend_replace_error_handling(EH_THROW, date_ce_date_malformed_string_exception, &zeh);
3357-
if (!php_date_modify(&new_object, modify, modify_len)) {
3362+
if (!php_date_modify(&new_object, modify, modify_len, true)) {
33583363
zval_ptr_dtor(&new_object);
3359-
zend_restore_error_handling(&zeh);
33603364
RETURN_THROWS();
33613365
}
33623366

3363-
zend_restore_error_handling(&zeh);
3364-
33653367
RETURN_OBJ(Z_OBJ(new_object));
33663368
}
33673369
/* }}} */
@@ -3418,7 +3420,7 @@ PHP_METHOD(DateTimeImmutable, add)
34183420
}
34193421
/* }}} */
34203422

3421-
static void php_date_sub(zval *object, zval *interval, zval *return_value) /* {{{ */
3423+
static void php_date_sub(zval *object, zval *interval, zval *return_value, const bool should_throw) /* {{{ */
34223424
{
34233425
php_date_obj *dateobj;
34243426
php_interval_obj *intobj;
@@ -3430,7 +3432,15 @@ static void php_date_sub(zval *object, zval *interval, zval *return_value) /* {{
34303432
DATE_CHECK_INITIALIZED(intobj->initialized, Z_OBJCE_P(interval));
34313433

34323434
if (intobj->diff->have_weekday_relative || intobj->diff->have_special_relative) {
3433-
php_error_docref(NULL, E_WARNING, "Only non-special relative time specifications are supported for subtraction");
3435+
if (should_throw) {
3436+
zend_string *func_name = get_active_function_or_method_name();
3437+
zend_throw_exception_ex(date_ce_date_invalid_operation_exception, 0,
3438+
"%s(): Only non-special relative time specifications are supported for subtraction",
3439+
ZSTR_VAL(func_name));
3440+
zend_string_release_ex(func_name, false);
3441+
} else {
3442+
php_error_docref(NULL, E_WARNING, "Only non-special relative time specifications are supported for subtraction");
3443+
}
34343444
return;
34353445
}
34363446

@@ -3452,7 +3462,7 @@ PHP_FUNCTION(date_sub)
34523462
RETURN_THROWS();
34533463
}
34543464

3455-
php_date_sub(object, interval, return_value);
3465+
php_date_sub(object, interval, return_value, false);
34563466
RETURN_OBJ_COPY(Z_OBJ_P(object));
34573467
}
34583468
/* }}} */
@@ -3461,15 +3471,12 @@ PHP_FUNCTION(date_sub)
34613471
PHP_METHOD(DateTime, sub)
34623472
{
34633473
zval *object, *interval;
3464-
zend_error_handling zeh;
34653474

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

3470-
zend_replace_error_handling(EH_THROW, date_ce_date_invalid_operation_exception, &zeh);
3471-
php_date_sub(object, interval, return_value);
3472-
zend_restore_error_handling(&zeh);
3479+
php_date_sub(object, interval, return_value, true);
34733480

34743481
RETURN_OBJ_COPY(Z_OBJ_P(object));
34753482
}
@@ -3479,7 +3486,6 @@ PHP_METHOD(DateTime, sub)
34793486
PHP_METHOD(DateTimeImmutable, sub)
34803487
{
34813488
zval *object, *interval, new_object;
3482-
zend_error_handling zeh;
34833489

34843490
object = ZEND_THIS;
34853491
ZEND_PARSE_PARAMETERS_START(1, 1)
@@ -3488,9 +3494,7 @@ PHP_METHOD(DateTimeImmutable, sub)
34883494

34893495
date_clone_immutable(object, &new_object);
34903496

3491-
zend_replace_error_handling(EH_THROW, date_ce_date_invalid_operation_exception, &zeh);
3492-
php_date_sub(&new_object, interval, return_value);
3493-
zend_restore_error_handling(&zeh);
3497+
php_date_sub(&new_object, interval, return_value, true);
34943498

34953499
RETURN_OBJ(Z_OBJ(new_object));
34963500
}

0 commit comments

Comments
 (0)