Skip to content

Commit

Permalink
Fix crash when non-object passed to 2nd argument of TimecopDateTime::…
Browse files Browse the repository at this point in the history
…__construct() (Fix for Issue #9)
  • Loading branch information
hnw committed Dec 23, 2016
1 parent bad5ffa commit 150ea43
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
36 changes: 36 additions & 0 deletions tests/issue_009.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
--TEST--
Check for issue #9 (Issue with using timecop constructor)
--SKIPIF--
<?php
extension_loaded('timecop') or die('skip timecop not available');
if (version_compare(PHP_VERSION, '5.3.0') < 0) {
die("skip requires PHP >= 5.3.0");
}
--INI--
date.timezone=GMT
timecop.func_override=1
--FILE--
<?php
class Date extends \DateTime
{
public function __construct($time='now', \DateTimeZone $timezone=null)
{
parent::__construct($time, $timezone);
$this->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
4 changes: 2 additions & 2 deletions timecop_php5.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}
Expand Down
6 changes: 3 additions & 3 deletions timecop_php7.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}

Expand All @@ -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);
}
Expand Down

0 comments on commit 150ea43

Please sign in to comment.