com php-src: Fixed whitespace part of bug #64874 ("json_d ecode handles whitespace and case-sensitivity incorre ctly"): ext/json/json.c
ext/json/tests/bug648 74_part1.phpt
| From: | Adam Harvey | Date: | Tue, 17 Sep 2013 19:09:52 +0000 |
| Subject: | com php-src: Fixed whitespace part of bug #64874 ("json_d ecode handles whitespace and case-sensitivity incorre ctly"): ext/json/json.c ext/json/tests/bug648 74_part1.phpt |
||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-73927@lists.php.net to get a copy of this message | ||
Commit: 2524428504ec9e154b01db7f6040e42c1bc89ada
Author: Andrea Faulds <ajf@ajf.me> Tue, 10 Sep 2013 21:22:06 +0000
Parents: 7beef74a827f393753852f7239af75af236fb4f8
Branches: PHP-5.4 PHP-5.5 PHP-5.6 master
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=2524428504ec9e154b01db7f6040e42c1bc89ada
Log:
Fixed whitespace part of bug #64874 ("json_decode handles whitespace and case-sensitivity
incorrectly")
Bugs:
https://bugs.php.net/64874
Changed paths:
M ext/json/json.c
A ext/json/tests/bug64874_part1.phpt
Diff:
diff --git a/ext/json/json.c b/ext/json/json.c
index 8c8963d..a786fef 100644
--- a/ext/json/json.c
+++ b/ext/json/json.c
@@ -684,21 +684,35 @@ PHP_JSON_API void php_json_decode_ex(zval *return_value, char *str, int
str_len,
double d;
int type, overflow_info;
long p;
+ char *trim = str;
+ int trim_len = str_len;
+
+ /* Increment trimmed string pointer to strip leading whitespace */
+ /* JSON RFC says to consider as whitespace: space, tab, LF or CR */
+ while (trim_len && (*trim == ' ' || *trim == '\t' || *trim ==
'\n' || *trim == '\r')) {
+ trim++;
+ trim_len--;
+ }
+
+ /* Decrement trimmed string length to strip trailing whitespace */
+ while (trim_len && (trim[trim_len - 1] == ' ' || trim[trim_len - 1] ==
'\t' || trim[trim_len - 1] == '\n' || trim[trim_len - 1] == '\r')) {
+ trim_len--;
+ }
RETVAL_NULL();
- if (str_len == 4) {
- if (!strcasecmp(str, "null")) {
+ if (trim_len == 4) {
+ if (!strncasecmp(trim, "null", trim_len)) {
/* We need to explicitly clear the error because its an actual NULL and not an error */
jp->error_code = PHP_JSON_ERROR_NONE;
RETVAL_NULL();
- } else if (!strcasecmp(str, "true")) {
+ } else if (!strncasecmp(trim, "true", trim_len)) {
RETVAL_BOOL(1);
}
- } else if (str_len == 5 && !strcasecmp(str, "false")) {
+ } else if (trim_len == 5 && !strncasecmp(trim, "false", trim_len)) {
RETVAL_BOOL(0);
}
- if ((type = is_numeric_string_ex(str, str_len, &p, &d, 0, &overflow_info)) != 0) {
+ if ((type = is_numeric_string_ex(trim, trim_len, &p, &d, 0, &overflow_info)) != 0) {
if (type == IS_LONG) {
RETVAL_LONG(p);
} else if (type == IS_DOUBLE) {
@@ -711,10 +725,10 @@ PHP_JSON_API void php_json_decode_ex(zval *return_value, char *str, int
str_len,
int i;
zend_bool is_float = 0;
- for (i = (str[0] == '-' ? 1 : 0); i < str_len; i++) {
+ for (i = (trim[0] == '-' ? 1 : 0); i < trim_len; i++) {
/* Not using isdigit() because it's locale specific,
* but we expect JSON input to always be UTF-8. */
- if (str[i] < '0' || str[i] > '9') {
+ if (trim[i] < '0' || trim[i] > '9') {
is_float = 1;
break;
}
@@ -723,7 +737,7 @@ PHP_JSON_API void php_json_decode_ex(zval *return_value, char *str, int str_len,
if (is_float) {
RETVAL_DOUBLE(d);
} else {
- RETVAL_STRINGL(str, str_len, 1);
+ RETVAL_STRINGL(trim, trim_len, 1);
}
} else {
RETVAL_DOUBLE(d);
diff --git a/ext/json/tests/bug64874_part1.phpt b/ext/json/tests/bug64874_part1.phpt
new file mode 100644
index 0000000..4047eb8
--- /dev/null
+++ b/ext/json/tests/bug64874_part1.phpt
@@ -0,0 +1,51 @@
+--TEST--
+Whitespace part of bug #64874 ("json_decode handles whitespace and case-sensitivity
incorrectly")
+--SKIPIF--
+<?php if (!extension_loaded("json")) print "skip"; ?>
+--FILE--
+<?php
+function decode($json) {
+ var_dump(json_decode($json));
+ var_dump(json_last_error() !== 0);
+ echo "\n";
+}
+
+// Leading whitespace should be ignored
+decode(" true");
+decode("\ttrue");
+decode("\ntrue");
+decode("\rtrue");
+
+// So should trailing whitespace
+decode("true ");
+decode("true\t");
+decode("true\n");
+decode("true\r");
+
+echo "Done\n";
+--EXPECT--
+bool(true)
+bool(false)
+
+bool(true)
+bool(false)
+
+bool(true)
+bool(false)
+
+bool(true)
+bool(false)
+
+bool(true)
+bool(false)
+
+bool(true)
+bool(false)
+
+bool(true)
+bool(false)
+
+bool(true)
+bool(false)
+
+Done