From 150ea43b4d2ba5f30515613bc67c89e5adf35f98 Mon Sep 17 00:00:00 2001 From: Yoshio HANAWA Date: Fri, 23 Dec 2016 15:51:56 +0900 Subject: [PATCH] Fix crash when non-object passed to 2nd argument of TimecopDateTime::__construct() (Fix for Issue #9) --- tests/issue_009.phpt | 36 ++++++++++++++++++++++++++++++++++++ timecop_php5.c | 4 ++-- timecop_php7.c | 6 +++--- 3 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 tests/issue_009.phpt diff --git a/tests/issue_009.phpt b/tests/issue_009.phpt new file mode 100644 index 0000000..24c79f3 --- /dev/null +++ b/tests/issue_009.phpt @@ -0,0 +1,36 @@ +--TEST-- +Check for issue #9 (Issue with using timecop constructor) +--SKIPIF-- += 5.3.0"); +} +--INI-- +date.timezone=GMT +timecop.func_override=1 +--FILE-- +setTime(0, 0, 0); + } + + public function __toString() { + return $this->format('Y-m-d'); + } +} +timecop_freeze(strtotime("2010-01-01")); +$dt1 = new Date(); +echo $dt1, "\n"; +$dt2 = new Date("2020-12-31"); +echo $dt2, "\n"; +$dt3 = new Date("2030-06-15", new \DateTimeZone("UTC")); +echo $dt3, "\n"; +--EXPECT-- +2010-01-01 +2020-12-31 +2030-06-15 diff --git a/timecop_php5.c b/timecop_php5.c index bea8f8e..74d1ee8 100644 --- a/timecop_php5.c +++ b/timecop_php5.c @@ -717,7 +717,7 @@ static int fix_datetime_timestamp(zval **datetime_obj, zval *time, zval *timezon * } * } */ - if (timezone_obj) { + if (timezone_obj && Z_TYPE_P(timezone_obj) == IS_OBJECT) { zval *zonename; zend_call_method_with_0_params(&timezone_obj, Z_OBJCE_PP(&timezone_obj), NULL, "getname", &zonename); if (zonename) { @@ -739,7 +739,7 @@ static int fix_datetime_timestamp(zval **datetime_obj, zval *time, zval *timezon * date_default_timezone_set($orig_zonename); * } */ - if (timezone_obj && orig_zonename) { + if (timezone_obj && Z_TYPE_P(timezone_obj) == IS_OBJECT) { zend_call_method_with_1_params(NULL, NULL, NULL, "date_default_timezone_set", NULL, orig_zonename); zval_ptr_dtor(&orig_zonename); } diff --git a/timecop_php7.c b/timecop_php7.c index e3785dd..16ec431 100644 --- a/timecop_php7.c +++ b/timecop_php7.c @@ -674,7 +674,7 @@ static int fix_datetime_timestamp(zval *datetime_obj, zval *time, zval *timezone * date_default_timezone_set($zonename); * } */ - if (timezone_obj) { + if (timezone_obj && Z_TYPE_P(timezone_obj) == IS_OBJECT) { zval zonename; zend_call_method_with_0_params(timezone_obj, Z_OBJCE_P(timezone_obj), NULL, "getname", &zonename); zend_call_method_with_0_params(NULL, NULL, NULL, "date_default_timezone_get", &orig_zonename); @@ -692,7 +692,7 @@ static int fix_datetime_timestamp(zval *datetime_obj, zval *time, zval *timezone * date_default_timezone_set($orig_zonename); * } */ - if (timezone_obj) { + if (timezone_obj && Z_TYPE_P(timezone_obj) == IS_OBJECT) { zend_call_method_with_1_params(NULL, NULL, NULL, "date_default_timezone_set", NULL, &orig_zonename); } @@ -718,7 +718,7 @@ static int fix_datetime_timestamp(zval *datetime_obj, zval *time, zval *timezone * date_default_timezone_set($orig_zonename); * } */ - if (timezone_obj) { + if (timezone_obj && Z_TYPE_P(timezone_obj) == IS_OBJECT) { zend_call_method_with_1_params(NULL, NULL, NULL, "date_default_timezone_set", NULL, &orig_zonename); zval_ptr_dtor(&orig_zonename); }