php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Return to Bug #64313
Patch fix_suggestion_64313 revision 2013-03-01 21:16 UTC by jellofishi at gmail dot com

Patch fix_suggestion_64313 for Date/time related Bug #64313

Patch version 2013-03-01 21:16 UTC

Return to Bug #64313 | Download this patch
Patch Revisions:

Developer: jellofishi@gmail.com

diff --git a/ext/date/php_date.c b/ext/date/php_date.c
index 2e616b1..e1365d9 100644
--- a/ext/date/php_date.c
+++ b/ext/date/php_date.c
@@ -507,6 +507,9 @@ PHP_INI_END()
 
 zend_class_entry *date_ce_date, *date_ce_timezone, *date_ce_interval, *date_ce_period;
 
+/* Exceptions */
+zend_class_entry *date_time_exception_ce;
+zend_class_entry *date_time_zone_exception_ce;
 
 PHPAPI zend_class_entry *php_date_get_date_ce(void)
 {
@@ -2020,6 +2023,16 @@ static void date_register_classes(TSRMLS_D)
 	zend_declare_class_constant_long(date_ce_period, const_name, sizeof(const_name)-1, value TSRMLS_CC);
 
 	REGISTER_PERIOD_CLASS_CONST_STRING("EXCLUDE_START_DATE", PHP_DATE_PERIOD_EXCLUDE_START_DATE);
+	
+	/* DateTimeException */
+	zend_class_entry ce_date_time_exception;
+	INIT_CLASS_ENTRY(ce_date_time_exception, "DateTimeException", NULL);
+	date_time_exception_ce = zend_register_internal_class_ex(&ce_date_time_exception, zend_exception_get_default(TSRMLS_C), NULL TSRMLS_CC);
+	
+	/* DateTimeZoneException */
+	zend_class_entry ce_date_time_zone_exception;
+	INIT_CLASS_ENTRY(ce_date_time_zone_exception, "DateTimeZoneException", NULL);
+	date_time_zone_exception_ce = zend_register_internal_class_ex(&ce_date_time_zone_exception, zend_exception_get_default(TSRMLS_C), NULL TSRMLS_CC);
 }
 
 static inline zend_object_value date_object_new_date_ex(zend_class_entry *class_type, php_date_obj **ptr TSRMLS_DC)
@@ -2526,7 +2539,7 @@ PHP_METHOD(DateTime, __construct)
 	int time_str_len = 0;
 	zend_error_handling error_handling;
 
-	zend_replace_error_handling(EH_THROW, NULL, &error_handling TSRMLS_CC);
+	zend_replace_error_handling(EH_THROW, date_time_exception_ce, &error_handling TSRMLS_CC);
 	if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sO!", &time_str, &time_str_len, &timezone_object, date_ce_timezone)) {
 		php_date_initialize(zend_object_store_get_object(getThis() TSRMLS_CC), time_str, time_str_len, NULL, timezone_object, 1 TSRMLS_CC);
 	}
@@ -3248,7 +3261,7 @@ PHP_METHOD(DateTimeZone, __construct)
 	php_timezone_obj *tzobj;
 	zend_error_handling error_handling;
 	
-	zend_replace_error_handling(EH_THROW, NULL, &error_handling TSRMLS_CC);
+	zend_replace_error_handling(EH_THROW, date_time_zone_exception_ce, &error_handling TSRMLS_CC);
 	if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &tz, &tz_len)) {
 		if (SUCCESS == timezone_initialize(&tzi, tz TSRMLS_CC)) {
 			tzobj = zend_object_store_get_object(getThis() TSRMLS_CC);
diff --git a/ext/date/php_date.h b/ext/date/php_date.h
index a86d6c7..388736d 100644
--- a/ext/date/php_date.h
+++ b/ext/date/php_date.h
@@ -184,5 +184,8 @@ PHPAPI zend_class_entry *php_date_get_timezone_ce(void);
 PHPAPI zval *php_date_instantiate(zend_class_entry *pce, zval *object TSRMLS_DC);
 PHPAPI int php_date_initialize(php_date_obj *dateobj, /*const*/ char *time_str, int time_str_len, char *format, zval *timezone_object, int ctor TSRMLS_DC);
 
+/* Exceptions */
+extern zend_class_entry *date_time_exception_ce;
+extern zend_class_entry *date_time_zone_exception_ce;
 
 #endif /* PHP_DATE_H */
diff --git a/ext/date/tests/DateTimeZone_construct_error.phpt b/ext/date/tests/DateTimeZone_construct_error.phpt
index 8ffe322..9c3f23c 100644
--- a/ext/date/tests/DateTimeZone_construct_error.phpt
+++ b/ext/date/tests/DateTimeZone_construct_error.phpt
@@ -24,7 +24,7 @@ var_dump( new DateTimeZone($timezone, $extra_arg) );
 
 -- Testing new DateTimeZone() with more than expected no. of arguments --
 
-Fatal error: Uncaught exception 'Exception' with message 'DateTimeZone::__construct() expects exactly 1 parameter, 2 given' in %s:%d
+Fatal error: Uncaught exception 'DateTimeZoneException' with message 'DateTimeZone::__construct() expects exactly 1 parameter, 2 given' in %s:%d
 Stack trace:
 #0 %s(%d): DateTimeZone->__construct('GMT', 99)
 #1 {main}
diff --git a/ext/date/tests/DateTime_construct_error.phpt b/ext/date/tests/DateTime_construct_error.phpt
index ef79eb4..1ec3f8c 100644
--- a/ext/date/tests/DateTime_construct_error.phpt
+++ b/ext/date/tests/DateTime_construct_error.phpt
@@ -25,7 +25,7 @@ var_dump( new DateTime($time, $timezone, $extra_arg) );
 
 -- Testing new DateTime() with more than expected no. of arguments --
 
-Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct() expects at most 2 parameters, 3 given' in %s:%d
+Fatal error: Uncaught exception 'DateTimeException' with message 'DateTime::__construct() expects at most 2 parameters, 3 given' in %s:%d
 Stack trace:
 #0 %s(%d): DateTime->__construct('GMT', Object(DateTimeZone), 99)
 #1 {main}
diff --git a/ext/date/tests/bug64313.phpt b/ext/date/tests/bug64313.phpt
new file mode 100644
index 0000000..0516572
--- /dev/null
+++ b/ext/date/tests/bug64313.phpt
@@ -0,0 +1,21 @@
+--TEST--
+Bug #64313 	DateTime & DateTimeZone constructs should emit DateTimeException
+--INI--
+date.timezone=UTC
+--FILE--
+<?php
+try {
+	$date = new DateTime('ab');
+} catch (DateTimeException $exception) {
+	var_dump("DateTimeException OK");
+}
+
+try {
+	$datetimezone = new DateTimeZone('ab');
+} catch (DateTimeZoneException $exception) {
+	var_dump("DateTimeZoneException OK");
+}
+--EXPECT--
+string(20) "DateTimeException OK"
+string(24) "DateTimeZoneException OK"
+
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu May 02 01:01:30 2024 UTC