Skip to content

Commit 63b440f

Browse files
committed
Throw directly instead of replacing error handler in ext/date
1 parent ed33262 commit 63b440f

7 files changed

+29
-43
lines changed

ext/date/php_date.c

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2215,10 +2215,10 @@ PHPAPI int php_date_initialize(php_date_obj *dateobj, const char *time_str, size
22152215
/* update last errors and warnings */
22162216
update_errors_warnings(err);
22172217

2218-
2218+
/* If called from a constructor throw an exception */
22192219
if ((flags & PHP_DATE_INIT_CTOR) && err && err->error_count) {
22202220
/* spit out the first library error message, at least */
2221-
php_error_docref(NULL, E_WARNING, "Failed to parse time string (%s) at position %d (%c): %s", time_str,
2221+
zend_throw_error(zend_ce_exception, "Failed to parse time string (%s) at position %d (%c): %s", time_str,
22222222
err->error_messages[0].position, err->error_messages[0].character, err->error_messages[0].message);
22232223
}
22242224
if (err && err->error_count) {
@@ -2386,17 +2386,14 @@ PHP_METHOD(DateTime, __construct)
23862386
zval *timezone_object = NULL;
23872387
char *time_str = NULL;
23882388
size_t time_str_len = 0;
2389-
zend_error_handling error_handling;
23902389

23912390
ZEND_PARSE_PARAMETERS_START(0, 2)
23922391
Z_PARAM_OPTIONAL
23932392
Z_PARAM_STRING(time_str, time_str_len)
23942393
Z_PARAM_OBJECT_OF_CLASS_OR_NULL(timezone_object, date_ce_timezone)
23952394
ZEND_PARSE_PARAMETERS_END();
23962395

2397-
zend_replace_error_handling(EH_THROW, NULL, &error_handling);
23982396
php_date_initialize(Z_PHPDATE_P(ZEND_THIS), time_str, time_str_len, NULL, timezone_object, PHP_DATE_INIT_CTOR);
2399-
zend_restore_error_handling(&error_handling);
24002397
}
24012398
/* }}} */
24022399

@@ -2406,17 +2403,14 @@ PHP_METHOD(DateTimeImmutable, __construct)
24062403
zval *timezone_object = NULL;
24072404
char *time_str = NULL;
24082405
size_t time_str_len = 0;
2409-
zend_error_handling error_handling;
24102406

24112407
ZEND_PARSE_PARAMETERS_START(0, 2)
24122408
Z_PARAM_OPTIONAL
24132409
Z_PARAM_STRING(time_str, time_str_len)
24142410
Z_PARAM_OBJECT_OF_CLASS_OR_NULL(timezone_object, date_ce_timezone)
24152411
ZEND_PARSE_PARAMETERS_END();
24162412

2417-
zend_replace_error_handling(EH_THROW, NULL, &error_handling);
24182413
php_date_initialize(Z_PHPDATE_P(ZEND_THIS), time_str, time_str_len, NULL, timezone_object, PHP_DATE_INIT_CTOR);
2419-
zend_restore_error_handling(&error_handling);
24202414
}
24212415
/* }}} */
24222416

@@ -3678,35 +3672,35 @@ PHP_FUNCTION(timezone_location_get)
36783672
}
36793673
/* }}} */
36803674

3681-
static int date_interval_initialize(timelib_rel_time **rt, /*const*/ char *format, size_t format_length) /* {{{ */
3675+
static bool date_interval_initialize(timelib_rel_time **rt, /*const*/ char *format, size_t format_length) /* {{{ */
36823676
{
36833677
timelib_time *b = NULL, *e = NULL;
36843678
timelib_rel_time *p = NULL;
36853679
int r = 0;
3686-
int retval = 0;
3680+
bool retval = false;
36873681
timelib_error_container *errors;
36883682

36893683
timelib_strtointerval(format, format_length, &b, &e, &p, &r, &errors);
36903684

36913685
if (errors->error_count > 0) {
3692-
php_error_docref(NULL, E_WARNING, "Unknown or bad format (%s)", format);
3693-
retval = FAILURE;
3686+
zend_throw_error(zend_ce_exception, "Unknown or bad format (%s)", format);
3687+
retval = false;
36943688
if (p) {
36953689
timelib_rel_time_dtor(p);
36963690
}
36973691
} else {
36983692
if(p) {
36993693
*rt = p;
3700-
retval = SUCCESS;
3694+
retval = true;
37013695
} else {
37023696
if(b && e) {
37033697
timelib_update_ts(b, NULL);
37043698
timelib_update_ts(e, NULL);
37053699
*rt = timelib_diff(b, e);
3706-
retval = SUCCESS;
3700+
retval = true;
37073701
} else {
3708-
php_error_docref(NULL, E_WARNING, "Failed to parse interval (%s)", format);
3709-
retval = FAILURE;
3702+
zend_throw_error(zend_ce_exception, "Failed to parse interval (%s)", format);
3703+
retval = false;
37103704
}
37113705
}
37123706
}
@@ -3845,20 +3839,19 @@ PHP_METHOD(DateInterval, __construct)
38453839
{
38463840
zend_string *interval_string = NULL;
38473841
timelib_rel_time *reltime;
3848-
zend_error_handling error_handling;
38493842

38503843
ZEND_PARSE_PARAMETERS_START(1, 1)
38513844
Z_PARAM_STR(interval_string)
38523845
ZEND_PARSE_PARAMETERS_END();
38533846

3854-
zend_replace_error_handling(EH_THROW, NULL, &error_handling);
3855-
if (date_interval_initialize(&reltime, ZSTR_VAL(interval_string), ZSTR_LEN(interval_string)) == SUCCESS) {
3856-
php_interval_obj *diobj = Z_PHPINTERVAL_P(ZEND_THIS);
3857-
diobj->diff = reltime;
3858-
diobj->initialized = 1;
3859-
diobj->civil_or_wall = PHP_DATE_WALL;
3847+
if (!date_interval_initialize(&reltime, ZSTR_VAL(interval_string), ZSTR_LEN(interval_string))) {
3848+
RETURN_THROWS();
38603849
}
3861-
zend_restore_error_handling(&error_handling);
3850+
3851+
php_interval_obj *diobj = Z_PHPINTERVAL_P(ZEND_THIS);
3852+
diobj->diff = reltime;
3853+
diobj->initialized = 1;
3854+
diobj->civil_or_wall = PHP_DATE_WALL;
38623855
}
38633856
/* }}} */
38643857

@@ -4109,19 +4102,17 @@ PHP_FUNCTION(date_interval_format)
41094102
}
41104103
/* }}} */
41114104

4112-
static int date_period_initialize(timelib_time **st, timelib_time **et, timelib_rel_time **d, zend_long *recurrences, /*const*/ char *format, size_t format_length) /* {{{ */
4105+
static void date_period_initialize(timelib_time **st, timelib_time **et, timelib_rel_time **d, zend_long *recurrences, /*const*/ char *format, size_t format_length) /* {{{ */
41134106
{
41144107
timelib_time *b = NULL, *e = NULL;
41154108
timelib_rel_time *p = NULL;
41164109
int r = 0;
4117-
int retval = 0;
41184110
timelib_error_container *errors;
41194111

41204112
timelib_strtointerval(format, format_length, &b, &e, &p, &r, &errors);
41214113

41224114
if (errors->error_count > 0) {
4123-
php_error_docref(NULL, E_WARNING, "Unknown or bad format (%s)", format);
4124-
retval = FAILURE;
4115+
zend_throw_error(zend_ce_exception, "Unknown or bad format (%s)", format);
41254116
if (b) {
41264117
timelib_time_dtor(b);
41274118
}
@@ -4136,10 +4127,8 @@ static int date_period_initialize(timelib_time **st, timelib_time **et, timelib_
41364127
*et = e;
41374128
*d = p;
41384129
*recurrences = r;
4139-
retval = SUCCESS;
41404130
}
41414131
timelib_error_container_dtor(errors);
4142-
return retval;
41434132
} /* }}} */
41444133

41454134
/* {{{ Creates new DatePeriod object. */
@@ -4152,7 +4141,6 @@ PHP_METHOD(DatePeriod, __construct)
41524141
char *isostr = NULL;
41534142
size_t isostr_len = 0;
41544143
timelib_time *clone;
4155-
zend_error_handling error_handling;
41564144

41574145
if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "OOl|l", &start, date_ce_interface, &interval, date_ce_interval, &recurrences, &options) == FAILURE) {
41584146
if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "OOO|l", &start, date_ce_interface, &interval, date_ce_interval, &end, date_ce_interface, &options) == FAILURE) {
@@ -4167,9 +4155,7 @@ PHP_METHOD(DatePeriod, __construct)
41674155
dpobj->current = NULL;
41684156

41694157
if (isostr) {
4170-
zend_replace_error_handling(EH_THROW, NULL, &error_handling);
41714158
date_period_initialize(&(dpobj->start), &(dpobj->end), &(dpobj->interval), &recurrences, isostr, isostr_len);
4172-
zend_restore_error_handling(&error_handling);
41734159
if (EG(exception)) {
41744160
RETURN_THROWS();
41754161
}

ext/date/tests/bug44562.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ foreach ( $dp as $d )
2121

2222
?>
2323
--EXPECT--
24-
DatePeriod::__construct(): Unknown or bad format (2D)
24+
Unknown or bad format (2D)
2525
string(24) "2008-07-20T22:44:53+0200"
2626
string(24) "2008-07-21T22:44:53+0200"
2727
string(24) "2008-07-22T22:44:53+0200"

ext/date/tests/bug52808.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ object(DateInterval)#%d (16) {
127127
["have_special_relative"]=>
128128
int(0)
129129
}
130-
DateInterval::__construct(): Failed to parse interval (2007-05-11T15:30:00Z/)
131-
DateInterval::__construct(): Failed to parse interval (2007-05-11T15:30:00Z)
132-
DateInterval::__construct(): Unknown or bad format (2007-05-11T15:30:00Z/:00Z)
130+
Failed to parse interval (2007-05-11T15:30:00Z/)
131+
Failed to parse interval (2007-05-11T15:30:00Z)
132+
Unknown or bad format (2007-05-11T15:30:00Z/:00Z)
133133
==DONE==

ext/date/tests/bug54283.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ try {
1212
?>
1313
--EXPECTF--
1414
Deprecated: DatePeriod::__construct(): Passing null to parameter #1 ($start) of type string is deprecated in %s on line %d
15-
string(51) "DatePeriod::__construct(): Unknown or bad format ()"
15+
string(24) "Unknown or bad format ()"

ext/date/tests/bug62500.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ int(3)
2626

2727
Warning: Undefined property: Crasher::$2 in %s on line %d
2828
NULL
29-
string(%s) "DateInterval::__construct(): Unknown or bad format (blah)"
29+
string(28) "Unknown or bad format (blah)"

ext/date/tests/date_interval_bad_format_leak.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ try {
2323

2424
?>
2525
--EXPECT--
26-
DateInterval::__construct(): Unknown or bad format (P3"D)
27-
DatePeriod::__construct(): Unknown or bad format (P3"D)
28-
DatePeriod::__construct(): Unknown or bad format (2008-03-01T12:00:00Z1)
26+
Unknown or bad format (P3"D)
27+
Unknown or bad format (P3"D)
28+
Unknown or bad format (2008-03-01T12:00:00Z1)

ext/date/tests/oo_001.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ echo "DONE\n";
6565
--EXPECTF--
6666
string(19) "%d-%d-%d %d:%d:%d"
6767
The DateTime object has not been correctly initialized by its constructor
68-
DateTime::__construct(): Failed to parse time string (1am todax) at position 4 (t): The timezone could not be found in the database
68+
Failed to parse time string (1am todax) at position 4 (t): The timezone could not be found in the database
6969
string(3) "UTC"
7070
The DateTimeZone object has not been correctly initialized by its constructor
7171
DateTimeZone::__construct(): Unknown or bad timezone (GottaFindThisOne)

0 commit comments

Comments
 (0)