]> granicus.if.org Git - php/commitdiff
Fixed bug #50020 (DateInterval:createDateFromString() silently fails)
authorDerick Rethans <github@derickrethans.nl>
Thu, 28 Feb 2019 13:50:35 +0000 (13:50 +0000)
committerDerick Rethans <github@derickrethans.nl>
Thu, 28 Feb 2019 13:50:35 +0000 (13:50 +0000)
NEWS
ext/date/php_date.c
ext/date/tests/date_interval_create_from_date_string_broken.phpt [new file with mode: 0644]
ext/date/tests/date_interval_create_from_date_string_nullparam.phpt

diff --git a/NEWS b/NEWS
index c7faa0b9078483284320946a4bcb5b43b7c7eb5e..7b2e47330e5c1fad7b3d2d03b03923ea6c779e92 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -7,11 +7,9 @@ PHP                                                                        NEWS
   . Fixed bug #77652 (Anonymous classes can lose their interface information).
     (Nikita)
 
-- Standard:
-  . Fixed bug #77664 (Segmentation fault when using undefined constant in
-    custom wrapper). (Laruence)
-  . Fixed bug #77669 (Crash in extract() when overwriting extracted array).
-    (Nikita)
+- Date:
+  . Fixed bug #50020 (DateInterval:createDateFromString() silently fails).
+    (Derick)
 
 - MySQLi:
   . Fixed bug #77597 (mysqli_fetch_field hangs scripts). (Nikita)
@@ -19,6 +17,12 @@ PHP                                                                        NEWS
 - sodium:
   . Fixed bug #77646 (sign_detached() strings not terminated). (Frank)
 
+- Standard:
+  . Fixed bug #77664 (Segmentation fault when using undefined constant in
+    custom wrapper). (Laruence)
+  . Fixed bug #77669 (Crash in extract() when overwriting extracted array).
+    (Nikita)
+
 07 Mar 2019, PHP 7.2.16
 
 - Core:
index c8479b5164e99b08639de8d5d2e940269a8df642..5cc3f794cdf03bffaa42a85337b7571fd002c66c 100644 (file)
@@ -4435,12 +4435,21 @@ PHP_FUNCTION(date_interval_create_from_date_string)
                Z_PARAM_STR(time_str)
        ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
 
-       php_date_instantiate(date_ce_interval, return_value);
-
        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),
+                       err->error_messages[0].position, err->error_messages[0].character ? err->error_messages[0].character : ' ', err->error_messages[0].message);
+               RETVAL_FALSE;
+               goto cleanup;
+       }
+
+       php_date_instantiate(date_ce_interval, return_value);
        diobj = Z_PHPINTERVAL_P(return_value);
        diobj->diff = timelib_rel_time_clone(&time->relative);
        diobj->initialized = 1;
+
+cleanup:
        timelib_time_dtor(time);
        timelib_error_container_dtor(err);
 }
diff --git a/ext/date/tests/date_interval_create_from_date_string_broken.phpt b/ext/date/tests/date_interval_create_from_date_string_broken.phpt
new file mode 100644 (file)
index 0000000..c065de0
--- /dev/null
@@ -0,0 +1,10 @@
+--TEST--
+Test date_interval_create_from_date_string() function : nonsense data
+--FILE--
+<?php
+$i = date_interval_create_from_date_string("foobar");
+var_dump($i);
+?>
+--EXPECTF--
+Warning: date_interval_create_from_date_string(): Unknown or bad format (foobar) at position 0 (f): The timezone could not be found in the database in %sdate_interval_create_from_date_string_broken.php on line 2
+bool(false)
index bb7bf94ce2dbf63167a81c4b6a6ce17481a99664..e03386ad3c4f44c4585521e632ba59a51b975ec9 100644 (file)
@@ -4,41 +4,9 @@ Test date_interval_create_from_date_string() function : null parameter
 Rodrigo Prado de Jesus <royopa [at] gmail [dot] com>
 --FILE--
 <?php
-$i = date_interval_create_from_date_string(null); //returns a empty object
+$i = date_interval_create_from_date_string(null);
 var_dump($i);
 ?>
 --EXPECTF--
-object(DateInterval)#%d (16) {
-  ["y"]=>
-  int(0)
-  ["m"]=>
-  int(0)
-  ["d"]=>
-  int(0)
-  ["h"]=>
-  int(0)
-  ["i"]=>
-  int(0)
-  ["s"]=>
-  int(0)
-  ["f"]=>
-  float(0)
-  ["weekday"]=>
-  int(0)
-  ["weekday_behavior"]=>
-  int(0)
-  ["first_last_day_of"]=>
-  int(0)
-  ["invert"]=>
-  int(0)
-  ["days"]=>
-  int(0)
-  ["special_type"]=>
-  int(0)
-  ["special_amount"]=>
-  int(0)
-  ["have_weekday_relative"]=>
-  int(0)
-  ["have_special_relative"]=>
-  int(0)
-}
+Warning: date_interval_create_from_date_string(): Unknown or bad format () at position 0 ( ): Empty string in %sdate_interval_create_from_date_string_nullparam.php on line 2
+bool(false)