Skip to content

Change warnings to Errors in Date extension #4976

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

Closed
wants to merge 1 commit into from
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
8 changes: 4 additions & 4 deletions Zend/tests/bug54043.phpt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--TEST--
Bug #54043: Remove inconsitency of internal exceptions and user defined exceptions
Bug #54043: Remove inconsistency of internal exceptions and user defined exceptions
--FILE--
<?php

Expand All @@ -8,13 +8,13 @@ $timeZone = new DateTimeZone('UTC');

try {
$dateTime = new DateTime($time, $timeZone);
} catch (Exception $e) {
var_dump($e->getMessage());
} catch (\Error $e) {
echo get_class($e) . ': ' . $e->getMessage() . \PHP_EOL;
}

var_dump(error_get_last());

?>
--EXPECT--
string(105) "DateTime::__construct(): Failed to parse time string (9999-11-33) at position 9 (3): Unexpected character"
Error: Failed to parse time string (9999-11-33) at position 9 (3): Unexpected character
NULL
61 changes: 33 additions & 28 deletions ext/date/php_date.c
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,8 @@ static PHP_INI_MH(OnUpdate_date_timezone)
if (stage == PHP_INI_STAGE_RUNTIME) {
if (!timelib_timezone_id_is_valid(DATEG(default_timezone), DATE_TIMEZONEDB)) {
if (DATEG(default_timezone) && *DATEG(default_timezone)) {
php_error_docref(NULL, E_WARNING, "Invalid date.timezone value '%s', we selected the timezone 'UTC' for now.", DATEG(default_timezone));
zend_value_error("Invalid date.timezone value '%s'", DATEG(default_timezone));
return FAILURE;
}
} else {
DATEG(timezone_valid) = 1;
Expand Down Expand Up @@ -683,7 +684,7 @@ static char* guess_timezone(const timelib_tzdb *tzdb)
}

if (!timelib_timezone_id_is_valid(DATEG(default_timezone), tzdb)) {
php_error_docref(NULL, E_WARNING, "Invalid date.timezone value '%s', we selected the timezone 'UTC' for now.", DATEG(default_timezone));
zend_value_error("Invalid date.timezone value '%s'", DATEG(default_timezone));
return "UTC";
}

Expand All @@ -702,7 +703,7 @@ PHPAPI timelib_tzinfo *get_timezone_info(void)
tz = guess_timezone(DATE_TIMEZONEDB);
tzi = php_date_parse_tzfile(tz, DATE_TIMEZONEDB);
if (! tzi) {
php_error_docref(NULL, E_ERROR, "Timezone database is corrupt - this should *never* happen!");
zend_throw_error(NULL, "Timezone database is corrupt - this should *never* happen!");
}
return tzi;
}
Expand Down Expand Up @@ -1099,8 +1100,8 @@ PHP_FUNCTION(idate)
ZEND_PARSE_PARAMETERS_END();

if (ZSTR_LEN(format) != 1) {
php_error_docref(NULL, E_WARNING, "idate format is one char");
RETURN_FALSE;
zend_value_error("idate format is one char");
return;
}

if (ZEND_NUM_ARGS() == 1) {
Expand All @@ -1109,8 +1110,8 @@ PHP_FUNCTION(idate)

ret = php_idate(ZSTR_VAL(format)[0], ts, 0);
if (ret == -1) {
php_error_docref(NULL, E_WARNING, "Unrecognized date format token.");
RETURN_FALSE;
zend_value_error("Unrecognized date format token");
return;
}
RETURN_LONG(ret);
}
Expand Down Expand Up @@ -1897,7 +1898,7 @@ static int date_object_compare_date(zval *d1, zval *d2) /* {{{ */
o2 = Z_PHPDATE_P(d2);

if (!o1->time || !o2->time) {
php_error_docref(NULL, E_WARNING, "Trying to compare an incomplete DateTime or DateTimeImmutable object");
zend_throw_error(NULL, "Trying to compare an incomplete DateTime or DateTimeImmutable object");
return 1;
}
if (!o1->time->sse_uptodate) {
Expand Down Expand Up @@ -2324,7 +2325,7 @@ PHPAPI int php_date_initialize(php_date_obj *dateobj, /*const*/ char *time_str,

if (ctor && 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", time_str,
zend_throw_error(NULL, "Failed to parse time string (%s) at position %d (%c): %s", time_str,
err->error_messages[0].position, err->error_messages[0].character, err->error_messages[0].message);
}
if (err && err->error_count) {
Expand Down Expand Up @@ -2855,7 +2856,7 @@ static int php_date_modify(zval *object, char *modify, size_t modify_len) /* {{{
dateobj = Z_PHPDATE_P(object);

if (!(dateobj->time)) {
php_error_docref(NULL, E_WARNING, "The DateTime object has not been correctly initialized by its constructor");
zend_throw_error(NULL, "The DateTime object has not been correctly initialized by its constructor");
return 0;
}

Expand All @@ -2865,7 +2866,7 @@ static int php_date_modify(zval *object, char *modify, size_t modify_len) /* {{{
update_errors_warnings(err);
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,
zend_throw_error(NULL, "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].message);
timelib_time_dtor(tmp_time);
return 0;
Expand Down Expand Up @@ -3022,7 +3023,7 @@ static void php_date_sub(zval *object, zval *interval, zval *return_value) /* {{
DATE_CHECK_INITIALIZED(intobj->initialized, DateInterval);

if (intobj->diff->have_special_relative) {
php_error_docref(NULL, E_WARNING, "Only non-special relative time specifications are supported for subtraction");
zend_throw_error(NULL, "Only non-special relative time specifications are supported for subtraction");
return;
}

Expand Down Expand Up @@ -3474,14 +3475,14 @@ static int timezone_initialize(php_timezone_obj *tzobj, /*const*/ char *tz, size
char *orig_tz = tz;

if (strlen(tz) != tz_len) {
php_error_docref(NULL, E_WARNING, "Timezone must not contain null bytes");
zend_value_error("Timezone must not contain null bytes");
efree(dummy_t);
return FAILURE;
}

dummy_t->z = timelib_parse_zone(&tz, &dst, dummy_t, &not_found, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
if (not_found) {
php_error_docref(NULL, E_WARNING, "Unknown or bad timezone (%s)", orig_tz);
zend_value_error("Unknown or bad timezone (%s)", orig_tz);
efree(dummy_t);
return FAILURE;
} else {
Expand Down Expand Up @@ -3792,7 +3793,7 @@ static int date_interval_initialize(timelib_rel_time **rt, /*const*/ char *forma
timelib_strtointerval(format, format_length, &b, &e, &p, &r, &errors);

if (errors->error_count > 0) {
php_error_docref(NULL, E_WARNING, "Unknown or bad format (%s)", format);
zend_value_error("Unknown or bad format (%s)", format);
retval = FAILURE;
} else {
if(p) {
Expand All @@ -3805,7 +3806,7 @@ static int date_interval_initialize(timelib_rel_time **rt, /*const*/ char *forma
*rt = timelib_diff(b, e);
retval = SUCCESS;
} else {
php_error_docref(NULL, E_WARNING, "Failed to parse interval (%s)", format);
zend_throw_error(NULL, "Failed to parse interval (%s)", format);
retval = FAILURE;
}
}
Expand Down Expand Up @@ -4097,7 +4098,7 @@ PHP_FUNCTION(date_interval_create_from_date_string)
time = timelib_strtotime(ZSTR_VAL(time_str), ZSTR_LEN(time_str), &err, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);

if (err->error_count > 0) {
php_error_docref(NULL, E_WARNING, "Unknown or bad format (%s) at position %d (%c): %s", ZSTR_VAL(time_str),
zend_value_error("Unknown or bad format (%s) at position %d (%c): %s", ZSTR_VAL(time_str),
err->error_messages[0].position, err->error_messages[0].character ? err->error_messages[0].character : ' ', err->error_messages[0].message);
RETVAL_FALSE;
goto cleanup;
Expand Down Expand Up @@ -4215,7 +4216,7 @@ static int date_period_initialize(timelib_time **st, timelib_time **et, timelib_
timelib_strtointerval(format, format_length, &b, &e, &p, &r, &errors);

if (errors->error_count > 0) {
php_error_docref(NULL, E_WARNING, "Unknown or bad format (%s)", format);
zend_value_error("Unknown or bad format (%s)", format);
retval = FAILURE;
} else {
*st = b;
Expand Down Expand Up @@ -4246,7 +4247,7 @@ PHP_METHOD(DatePeriod, __construct)
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) {
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) {
if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "s|l", &isostr, &isostr_len, &options) == FAILURE) {
php_error_docref(NULL, E_WARNING, "This constructor accepts either (DateTimeInterface, DateInterval, int) OR (DateTimeInterface, DateInterval, DateTime) OR (string) as arguments.");
zend_type_error("This constructor accepts either (DateTimeInterface, DateInterval, int) OR (DateTimeInterface, DateInterval, DateTime) OR (string) as arguments.");
zend_restore_error_handling(&error_handling);
return;
}
Expand All @@ -4259,13 +4260,16 @@ PHP_METHOD(DatePeriod, __construct)
if (isostr) {
date_period_initialize(&(dpobj->start), &(dpobj->end), &(dpobj->interval), &recurrences, isostr, isostr_len);
if (dpobj->start == NULL) {
php_error_docref(NULL, E_WARNING, "The ISO interval '%s' did not contain a start date.", isostr);
zend_value_error("The ISO interval '%s' did not contain a start date.", isostr);
return;
}
if (dpobj->interval == NULL) {
php_error_docref(NULL, E_WARNING, "The ISO interval '%s' did not contain an interval.", isostr);
zend_value_error("The ISO interval '%s' did not contain an interval.", isostr);
return;
}
if (dpobj->end == NULL && recurrences == 0) {
php_error_docref(NULL, E_WARNING, "The ISO interval '%s' did not contain an end date or a recurrence count.", isostr);
zend_value_error("The ISO interval '%s' did not contain an end date or a recurrence count.", isostr);
return;
}

if (dpobj->start) {
Expand Down Expand Up @@ -4304,7 +4308,8 @@ PHP_METHOD(DatePeriod, __construct)
}

if (dpobj->end == NULL && recurrences < 1) {
php_error_docref(NULL, E_WARNING, "The recurrence count '%d' is invalid. Needs to be > 0", (int) recurrences);
zend_value_error("The recurrence count '%d' is invalid. Needs to be > 0", (int) recurrences);
return;
}

/* options */
Expand Down Expand Up @@ -4447,6 +4452,7 @@ PHP_FUNCTION(timezone_identifiers_list)

/* Extra validation */
if (what == PHP_DATE_TIMEZONE_PER_COUNTRY && option_len != 2) {
/* Upgrade to warning/exception? */
php_error_docref(NULL, E_NOTICE, "A two-letter ISO 3166-1 compatible country code is expected");
RETURN_FALSE;
}
Expand Down Expand Up @@ -4596,16 +4602,15 @@ static void php_do_date_sunrise_sunset(INTERNAL_FUNCTION_PARAMETERS, int calc_su
case 6:
break;
default:
php_error_docref(NULL, E_WARNING, "invalid format");
RETURN_FALSE;
break;
zend_value_error("Invalid format");
return;
}
if (retformat != SUNFUNCS_RET_TIMESTAMP &&
retformat != SUNFUNCS_RET_STRING &&
retformat != SUNFUNCS_RET_DOUBLE)
{
php_error_docref(NULL, E_WARNING, "Wrong return format given, pick one of SUNFUNCS_RET_TIMESTAMP, SUNFUNCS_RET_STRING or SUNFUNCS_RET_DOUBLE");
RETURN_FALSE;
zend_value_error("Wrong return format given, pick one of SUNFUNCS_RET_TIMESTAMP, SUNFUNCS_RET_STRING or SUNFUNCS_RET_DOUBLE");
return;
}
altitude = 90 - zenith;

Expand Down
2 changes: 1 addition & 1 deletion ext/date/php_date.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function strtotime(string $time, int $now = UNKNOWN): int|false {}

function date(string $format, int $timestamp = UNKNOWN): string {}

function idate(string $format, int $timestamp = UNKNOWN): int|false {}
function idate(string $format, int $timestamp = UNKNOWN): int {}

function gmdate(string $format, int $timestamp = UNKNOWN): string {}

Expand Down
2 changes: 1 addition & 1 deletion ext/date/php_date_arginfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_date, 0, 1, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, timestamp, IS_LONG, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_idate, 0, 1, MAY_BE_LONG|MAY_BE_FALSE)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_idate, 0, 1, IS_LONG, 0)
ZEND_ARG_TYPE_INFO(0, format, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, timestamp, IS_LONG, 0)
ZEND_END_ARG_INFO()
Expand Down
55 changes: 34 additions & 21 deletions ext/date/tests/005.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,43 @@ date_default_timezone_set('UTC');

$t = mktime(0,0,0, 6, 27, 2006);

var_dump(idate(1,1));
var_dump(idate(""));
var_dump(idate(0));
try {
var_dump(idate(1,1));
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}

try {
var_dump(idate(""));
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}

try {
var_dump(idate(0));
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}

var_dump(idate("B", $t));
var_dump(idate("[", $t));
var_dump(idate("'"));

echo "Done\n";
?>
--EXPECTF--
Warning: idate(): Unrecognized date format token. in %s on line %d
bool(false)
try {
var_dump(idate("[", $t));
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}

Warning: idate(): idate format is one char in %s on line %d
bool(false)
try {
var_dump(idate("'"));
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}

Warning: idate(): Unrecognized date format token. in %s on line %d
bool(false)
?>
--EXPECT--
Unrecognized date format token
idate format is one char
Unrecognized date format token
int(41)

Warning: idate(): Unrecognized date format token. in %s on line %d
bool(false)

Warning: idate(): Unrecognized date format token. in %s on line %d
bool(false)
Done
Unrecognized date format token
Unrecognized date format token
14 changes: 7 additions & 7 deletions ext/date/tests/DatePeriod_wrong_constructor.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ Havard Eide <nucleuz@gmail.com>
date.timezone=UTC
--FILE--
<?php
new DatePeriod();
try {
new DatePeriod();
} catch (\TypeError $exception) {
echo $exception->getMessage(), "\n";
}
?>
--EXPECTF--
Fatal error: Uncaught Exception: DatePeriod::__construct(): This constructor accepts either (DateTimeInterface, DateInterval, int) OR (DateTimeInterface, DateInterval, DateTime) OR (string) as arguments. in %s:%d
Stack trace:
#0 %s(%d): DatePeriod->__construct()
#1 {main}
thrown in %s on line %d
--EXPECT--
This constructor accepts either (DateTimeInterface, DateInterval, int) OR (DateTimeInterface, DateInterval, DateTime) OR (string) as arguments.
10 changes: 5 additions & 5 deletions ext/date/tests/DatePeriod_wrong_recurrence_on_constructor.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ DatePeriod: Test wrong recurrence parameter on __construct
<?php
try {
new DatePeriod(new DateTime('yesterday'), new DateInterval('P1D'), 0);
} catch (Exception $exception) {
} catch (\ValueError $exception) {
echo $exception->getMessage(), "\n";
}

try {
new DatePeriod(new DateTime('yesterday'), new DateInterval('P1D'),-1);
} catch (Exception $exception) {
} catch (\ValueError $exception) {
echo $exception->getMessage(), "\n";
}
?>
--EXPECTF--
DatePeriod::__construct(): The recurrence count '0' is invalid. Needs to be > 0
DatePeriod::__construct(): The recurrence count '-1' is invalid. Needs to be > 0
--EXPECT--
The recurrence count '0' is invalid. Needs to be > 0
The recurrence count '-1' is invalid. Needs to be > 0
4 changes: 2 additions & 2 deletions ext/date/tests/bug44562.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ try
{
$dp = new DatePeriod('2D');
}
catch ( Exception $e )
catch (\ValueError $e )
{
echo $e->getMessage(), "\n";
}
Expand All @@ -24,7 +24,7 @@ foreach ( $dp as $d )

?>
--EXPECT--
DatePeriod::__construct(): Unknown or bad format (2D)
The ISO interval '2D' did not contain a start date.
string(24) "2008-07-20T22:44:53+0200"
string(24) "2008-07-21T22:44:53+0200"
string(24) "2008-07-22T22:44:53+0200"
Expand Down
Loading