From 746f6bf73627884ea978ea2571213c83d8a63dea Mon Sep 17 00:00:00 2001 From: Andrea Faulds Date: Wed, 6 Mar 2019 00:49:24 +0100 Subject: [PATCH] Permit trailing whitespace in numeric strings --- .../trailing_whitespace_numeric_strings.phpt | 59 +++++++++++++++++++ Zend/zend_operators.c | 16 +++-- .../tests/general_functions/is_numeric.phpt | 6 +- 3 files changed, 74 insertions(+), 7 deletions(-) create mode 100644 Zend/tests/trailing_whitespace_numeric_strings.phpt diff --git a/Zend/tests/trailing_whitespace_numeric_strings.phpt b/Zend/tests/trailing_whitespace_numeric_strings.phpt new file mode 100644 index 000000000000..431a3f669112 --- /dev/null +++ b/Zend/tests/trailing_whitespace_numeric_strings.phpt @@ -0,0 +1,59 @@ +--TEST-- +Acceptance of whitespace in numeric strings +--FILE-- + +--EXPECT-- +OK! diff --git a/Zend/zend_operators.c b/Zend/zend_operators.c index bf2f5771fb1f..fa050ba5d13b 100644 --- a/Zend/zend_operators.c +++ b/Zend/zend_operators.c @@ -3077,11 +3077,19 @@ ZEND_API zend_uchar ZEND_FASTCALL _is_numeric_string_ex(const char *str, size_t } if (ptr != str + length) { - if (!allow_errors) { - return 0; + const char *endptr = ptr; + while (*endptr == ' ' || *endptr == '\t' || *endptr == '\n' || *endptr == '\r' || *endptr == '\v' || *endptr == '\f') { + endptr++; + length--; } - if (allow_errors == -1) { - zend_error(E_NOTICE, "A non well formed numeric value encountered"); + + if (ptr != str + length) { + if (!allow_errors) { + return 0; + } + if (allow_errors == -1) { + zend_error(E_NOTICE, "A non well formed numeric value encountered"); + } } } diff --git a/ext/standard/tests/general_functions/is_numeric.phpt b/ext/standard/tests/general_functions/is_numeric.phpt index 0019cf18d2fb..5c6986de6060 100644 --- a/ext/standard/tests/general_functions/is_numeric.phpt +++ b/ext/standard/tests/general_functions/is_numeric.phpt @@ -78,6 +78,7 @@ $numerics = array( '-1', '1e2', ' 1', + "1 ", '2974394749328742328432', '-1e-2', "0123", @@ -118,7 +119,6 @@ $not_numerics = array( array(), array("string"), "", - "1 ", "- 1", "1.2.4", "1e7.6", @@ -313,6 +313,8 @@ bool(true) bool(true) -- Iteration 76 -- bool(true) +-- Iteration 77 -- +bool(true) *** Testing is_numeric() on non numeric types *** -- Iteration 1 -- @@ -371,8 +373,6 @@ bool(false) bool(false) -- Iteration 28 -- bool(false) --- Iteration 29 -- -bool(false) *** Testing error conditions ***