From: Derick Rethans Date: Sun, 12 Dec 2010 17:17:16 +0000 (+0000) Subject: - Added the + modifier to parseFromFormat to allow trailing text in the X-Git-Tag: php-5.4.0alpha1~191^2~521 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=8ece8ad3141c24429b5ca78cee09b43c9b4c77e8;p=php - Added the + modifier to parseFromFormat to allow trailing text in the string to parse without throwing an error. #- Patch by Stas, test case by me. --- diff --git a/NEWS b/NEWS index fdee5bb76a..a5795eb9b0 100644 --- a/NEWS +++ b/NEWS @@ -121,6 +121,10 @@ PHP NEWS . Added support for CURLOPT_MAX_RECV_SPEED_LARGE and CURLOPT_MAX_SEND_SPEED_LARGE. FR #51815. (Pierrick) +- Improved Date extension: + . Added the + modifier to parseFromFormat to allow trailing text in the + string to parse without throwing an error. (Stas, Derick) + - Improved DBA extension: . Added Tokyo Cabinet abstract DB support. (Michael Maclean) . Added Berkeley DB 5 support. (Johannes, Chris Jones) diff --git a/ext/date/lib/parse_date.c b/ext/date/lib/parse_date.c index 5667b6273d..48b2fa6e32 100644 --- a/ext/date/lib/parse_date.c +++ b/ext/date/lib/parse_date.c @@ -1,4 +1,4 @@ -/* Generated by re2c 0.13.5 on Sat Nov 13 14:56:03 2010 */ +/* Generated by re2c 0.13.5 on Sun Dec 12 16:09:49 2010 */ #line 1 "ext/date/lib/parse_date.re" /* +----------------------------------------------------------------------+ @@ -24824,6 +24824,7 @@ timelib_time *timelib_parse_from_format(char *format, char *string, int len, tim timelib_sll tmp; Scanner in; Scanner *s = ∈ + int allow_extra = 0; memset(&in, 0, sizeof(in)); in.errors = malloc(sizeof(struct timelib_error_container)); @@ -25049,6 +25050,10 @@ timelib_time *timelib_parse_from_format(char *format, char *string, int len, tim timelib_eat_until_separator((char **) &ptr); break; + case '+': /* allow extra chars in the format */ + allow_extra = 1; + break; + default: if (*fptr != *ptr) { add_pbf_error(s, "The format separator does not match", string, begin); @@ -25058,7 +25063,15 @@ timelib_time *timelib_parse_from_format(char *format, char *string, int len, tim fptr++; } if (*ptr) { - add_pbf_error(s, "Trailing data", string, ptr); + if (allow_extra) { + add_pbf_warning(s, "Trailing data", string, ptr); + } else { + add_pbf_error(s, "Trailing data", string, ptr); + } + } + /* ignore trailing +'s */ + while (*fptr == '+') { + fptr++; } if (*fptr) { add_pbf_error(s, "Data missing", string, ptr); diff --git a/ext/date/lib/parse_date.re b/ext/date/lib/parse_date.re index 0a711d1f27..31dcf3636b 100644 --- a/ext/date/lib/parse_date.re +++ b/ext/date/lib/parse_date.re @@ -1831,6 +1831,7 @@ timelib_time *timelib_parse_from_format(char *format, char *string, int len, tim timelib_sll tmp; Scanner in; Scanner *s = ∈ + int allow_extra = 0; memset(&in, 0, sizeof(in)); in.errors = malloc(sizeof(struct timelib_error_container)); @@ -2056,6 +2057,10 @@ timelib_time *timelib_parse_from_format(char *format, char *string, int len, tim timelib_eat_until_separator((char **) &ptr); break; + case '+': /* allow extra chars in the format */ + allow_extra = 1; + break; + default: if (*fptr != *ptr) { add_pbf_error(s, "The format separator does not match", string, begin); @@ -2065,7 +2070,15 @@ timelib_time *timelib_parse_from_format(char *format, char *string, int len, tim fptr++; } if (*ptr) { - add_pbf_error(s, "Trailing data", string, ptr); + if (allow_extra) { + add_pbf_warning(s, "Trailing data", string, ptr); + } else { + add_pbf_error(s, "Trailing data", string, ptr); + } + } + /* ignore trailing +'s */ + while (*fptr == '+') { + fptr++; } if (*fptr) { add_pbf_error(s, "Data missing", string, ptr); diff --git a/ext/date/tests/bug51866.phpt b/ext/date/tests/bug51866.phpt new file mode 100644 index 0000000000..8630b08faf --- /dev/null +++ b/ext/date/tests/bug51866.phpt @@ -0,0 +1,159 @@ +--TEST-- +Bug #51866 (Lenient parsing with parseFromFormat) +--FILE-- + + int(0) + ["warnings"]=> + array(0) { + } + ["error_count"]=> + int(1) + ["errors"]=> + array(1) { + [10]=> + string(13) "Trailing data" + } +} + + +string(6) "Y-m-d+" +string(19) "2001-11-29 13:20:01" +object(DateTime)#2 (3) { + ["date"]=> + string(19) "2001-11-29 %d:%d:%d" + ["timezone_type"]=> + int(3) + ["timezone"]=> + string(13) "Europe/London" +} +array(4) { + ["warning_count"]=> + int(1) + ["warnings"]=> + array(1) { + [10]=> + string(13) "Trailing data" + } + ["error_count"]=> + int(0) + ["errors"]=> + array(0) { + } +} + + +string(7) "Y-m-d +" +string(19) "2001-11-29 13:20:01" +object(DateTime)#3 (3) { + ["date"]=> + string(19) "2001-11-29 %d:%d:%d" + ["timezone_type"]=> + int(3) + ["timezone"]=> + string(13) "Europe/London" +} +array(4) { + ["warning_count"]=> + int(1) + ["warnings"]=> + array(1) { + [11]=> + string(13) "Trailing data" + } + ["error_count"]=> + int(0) + ["errors"]=> + array(0) { + } +} + + +string(6) "Y-m-d+" +string(10) "2001-11-29" +object(DateTime)#2 (3) { + ["date"]=> + string(19) "2001-11-29 %d:%d:%d" + ["timezone_type"]=> + int(3) + ["timezone"]=> + string(13) "Europe/London" +} +array(4) { + ["warning_count"]=> + int(0) + ["warnings"]=> + array(0) { + } + ["error_count"]=> + int(0) + ["errors"]=> + array(0) { + } +} + + +string(7) "Y-m-d +" +string(10) "2001-11-29" +bool(false) +array(4) { + ["warning_count"]=> + int(0) + ["warnings"]=> + array(0) { + } + ["error_count"]=> + int(1) + ["errors"]=> + array(1) { + [10]=> + string(12) "Data missing" + } +} + + +string(7) "Y-m-d +" +string(11) "2001-11-29 " +object(DateTime)#2 (3) { + ["date"]=> + string(19) "2001-11-29 %d:%d:%d" + ["timezone_type"]=> + int(3) + ["timezone"]=> + string(13) "Europe/London" +} +array(4) { + ["warning_count"]=> + int(0) + ["warnings"]=> + array(0) { + } + ["error_count"]=> + int(0) + ["errors"]=> + array(0) { + } +}