]> granicus.if.org Git - php/commitdiff
Fix bug #61537 (json_encode() incorrectly truncates/discards information) and
authorAdam Harvey <adam@pwd.net.au>
Mon, 2 Apr 2012 01:46:18 +0000 (09:46 +0800)
committerAdam Harvey <adam@pwd.net.au>
Wed, 11 Apr 2012 00:29:39 +0000 (08:29 +0800)
remove a test case that's now mooted by this fix.

NEWS
ext/json/json.c
ext/json/php_json.h
ext/json/tests/bug43941.phpt [deleted file]
ext/json/tests/bug54058.phpt
ext/json/tests/bug61537.phpt [new file with mode: 0644]

diff --git a/NEWS b/NEWS
index 42f7cd98ed8d46f150ebf0f5ec39dc1541cb8b12..05cc254c65de70678ce2076d7a0ebe39a3f414ea 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -8,6 +8,14 @@ PHP                                                                        NEWS
     $_SERVER['SCRIPT_FILENAME'] when using router). (Laruence)
   . "Connection: close" instead of "Connection: closed" (Gustavo)
 
+- JSON
+  . Fixed bug #61537 (json_encode() incorrectly truncates/discards
+    information). (Adam)
+
+?? ??? 2012, PHP 5.3.11
+
+(merge after 5.3.11 release)
+
 - Core:
   . Fixed bug #61660 (bin2hex(hex2bin($data)) != $data). (Nikita Popov)
   . Fixed bug #61650 (ini parser crashes when using ${xxxx} ini variables
index fc1fcb7f1f5e6ee4cc135eaef468b4a32767633b..853611e7aae3ed5dd7094f5999f1840b74c194d7 100644 (file)
@@ -96,6 +96,7 @@ static PHP_MINIT_FUNCTION(json)
        REGISTER_LONG_CONSTANT("JSON_UNESCAPED_SLASHES", PHP_JSON_UNESCAPED_SLASHES, CONST_CS | CONST_PERSISTENT);
        REGISTER_LONG_CONSTANT("JSON_PRETTY_PRINT", PHP_JSON_PRETTY_PRINT, CONST_CS | CONST_PERSISTENT);
        REGISTER_LONG_CONSTANT("JSON_UNESCAPED_UNICODE", PHP_JSON_UNESCAPED_UNICODE, CONST_CS | CONST_PERSISTENT);
+       REGISTER_LONG_CONSTANT("JSON_PARTIAL_OUTPUT_ON_ERROR", PHP_JSON_PARTIAL_OUTPUT_ON_ERROR, CONST_CS | CONST_PERSISTENT);
 
        REGISTER_LONG_CONSTANT("JSON_ERROR_NONE", PHP_JSON_ERROR_NONE, CONST_CS | CONST_PERSISTENT);
        REGISTER_LONG_CONSTANT("JSON_ERROR_DEPTH", PHP_JSON_ERROR_DEPTH, CONST_CS | CONST_PERSISTENT);
@@ -389,9 +390,7 @@ static void json_escape_string(smart_str *buf, char *s, int len, int options TSR
                }
                if (ulen < 0) {
                        JSON_G(error_code) = PHP_JSON_ERROR_UTF8;
-                       if (!PG(display_errors)) {
-                               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid UTF-8 sequence in argument");
-                       }
+                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid UTF-8 sequence in argument");
                        smart_str_appendl(buf, "null", 4);
                } else {
                        smart_str_appendl(buf, "\"\"", 2);
@@ -689,7 +688,11 @@ static PHP_FUNCTION(json_encode)
 
        php_json_encode(&buf, parameter, options TSRMLS_CC);
 
-       ZVAL_STRINGL(return_value, buf.c, buf.len, 1);
+       if (JSON_G(error_code) != PHP_JSON_ERROR_NONE && options ^ PHP_JSON_PARTIAL_OUTPUT_ON_ERROR) {
+               ZVAL_FALSE(return_value);
+       } else {
+               ZVAL_STRINGL(return_value, buf.c, buf.len, 1);
+       }
 
        smart_str_free(&buf);
 }
index ef3e4b5a7996449b004e4a0f005d21b90791cb1f..20426c01531be9e5f198e71df5fd40902583ed7e 100644 (file)
@@ -63,6 +63,7 @@ extern zend_class_entry *php_json_serializable_ce;
 #define PHP_JSON_UNESCAPED_SLASHES     (1<<6)
 #define PHP_JSON_PRETTY_PRINT  (1<<7)
 #define PHP_JSON_UNESCAPED_UNICODE     (1<<8)
+#define PHP_JSON_PARTIAL_OUTPUT_ON_ERROR       (1<<9)
 
 /* Internal flags */
 #define PHP_JSON_OUTPUT_ARRAY  0
diff --git a/ext/json/tests/bug43941.phpt b/ext/json/tests/bug43941.phpt
deleted file mode 100644 (file)
index 0f86d1d..0000000
+++ /dev/null
@@ -1,21 +0,0 @@
---TEST--
-Bug #43941 (json_encode() invalid UTF-8)
---SKIPIF--
-<?php if (!extension_loaded("json")) print "skip"; ?>
---FILE--
-<?php
-
-var_dump(json_encode("abc"));
-var_dump(json_encode("ab\xE0"));
-var_dump(json_encode("ab\xE0c"));
-var_dump(json_encode(array("ab\xE0", "ab\xE0c", "abc")));
-
-echo "Done\n";
-?>
---EXPECTF--
-string(5) ""abc""
-string(4) "null"
-string(4) "null"
-string(17) "[null,null,"abc"]"
-Done
-
index 3b1136bdd95e78236ad9db8a086f3f16ee0ef15d..08c7f579ab90b2344078832cc1638064f14230d3 100644 (file)
@@ -29,7 +29,14 @@ json_encode($c);
 var_dump(json_last_error());
 ?>
 --EXPECTF--
+Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d
 int(5)
+
+Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d
 int(5)
+
+Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d
 int(5)
+
+Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d
 int(5)
diff --git a/ext/json/tests/bug61537.phpt b/ext/json/tests/bug61537.phpt
new file mode 100644 (file)
index 0000000..e2abdda
--- /dev/null
@@ -0,0 +1,30 @@
+--TEST--
+Bug #61537 (json_encode() incorrectly truncates/discards information)
+--SKIPIF--
+<?php if (!extension_loaded("json")) print "skip"; ?>
+--FILE--
+<?php
+$invalid_utf8 = "\x9f";
+var_dump(json_encode($invalid_utf8), json_last_error());
+var_dump(json_encode($invalid_utf8, JSON_PARTIAL_OUTPUT_ON_ERROR), json_last_error());
+
+$invalid_utf8 = "an invalid sequen\xce in the middle of a string";
+var_dump(json_encode($invalid_utf8), json_last_error());
+var_dump(json_encode($invalid_utf8, JSON_PARTIAL_OUTPUT_ON_ERROR), json_last_error());
+?>
+--EXPECTF--
+Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d
+bool(false)
+int(5)
+
+Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d
+string(4) "null"
+int(5)
+
+Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d
+bool(false)
+int(5)
+
+Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d
+string(4) "null"
+int(5)