scottmac                                 Mon, 21 Feb 2011 08:09:02 +0000

Revision: http://svn.php.net/viewvc?view=revision&revision=308529

Log:
Fix Bug #54058, invalid utf-8 doesn't set json_encode() in all cases

Bug: http://bugs.php.net/54058 (Open) json_last_error() doesn't work properly 
with arrays/objects 
      
Changed paths:
    U   php/php-src/branches/PHP_5_3/ext/json/json.c
    A   php/php-src/branches/PHP_5_3/ext/json/tests/bug54058.phpt
    U   php/php-src/trunk/ext/json/json.c
    A   php/php-src/trunk/ext/json/tests/bug54058.phpt

Modified: php/php-src/branches/PHP_5_3/ext/json/json.c
===================================================================
--- php/php-src/branches/PHP_5_3/ext/json/json.c        2011-02-21 07:36:26 UTC 
(rev 308528)
+++ php/php-src/branches/PHP_5_3/ext/json/json.c        2011-02-21 08:09:02 UTC 
(rev 308529)
@@ -431,7 +431,6 @@

 PHP_JSON_API void php_json_encode(smart_str *buf, zval *val, int options 
TSRMLS_DC) /* {{{ */
 {
-       JSON_G(error_code) = PHP_JSON_ERROR_NONE;
        switch (Z_TYPE_P(val))
        {
                case IS_NULL:
@@ -567,6 +566,8 @@
                return;
        }

+       JSON_G(error_code) = PHP_JSON_ERROR_NONE;
+
        php_json_encode(&buf, parameter, options TSRMLS_CC);

        ZVAL_STRINGL(return_value, buf.c, buf.len, 1);

Added: php/php-src/branches/PHP_5_3/ext/json/tests/bug54058.phpt
===================================================================
--- php/php-src/branches/PHP_5_3/ext/json/tests/bug54058.phpt                   
        (rev 0)
+++ php/php-src/branches/PHP_5_3/ext/json/tests/bug54058.phpt   2011-02-21 
08:09:02 UTC (rev 308529)
@@ -0,0 +1,35 @@
+--TEST--
+Bug #54058 (json_last_error() invalid UTF-8 produces wrong error)
+--SKIPIF--
+<?php if (!extension_loaded("json")) print "skip"; ?>
+--FILE--
+<?php
+
+$bad_utf8 = quoted_printable_decode('=B0');
+
+json_encode($bad_utf8);
+var_dump(json_last_error());
+
+$a = new stdclass;
+$a->foo = quoted_printable_decode('=B0');
+json_encode($a);
+var_dump(json_last_error());
+
+$b = new stdclass;
+$b->foo = $bad_utf8;
+$b->bar = 1;
+json_encode($b);
+var_dump(json_last_error());
+
+$c = array(
+    'foo' => $bad_utf8,
+    'bar' => 1
+);
+json_encode($c);
+var_dump(json_last_error());
+?>
+--EXPECTF--
+int(5)
+int(5)
+int(5)
+int(5)

Modified: php/php-src/trunk/ext/json/json.c
===================================================================
--- php/php-src/trunk/ext/json/json.c   2011-02-21 07:36:26 UTC (rev 308528)
+++ php/php-src/trunk/ext/json/json.c   2011-02-21 08:09:02 UTC (rev 308529)
@@ -538,7 +538,6 @@

 PHP_JSON_API void php_json_encode(smart_str *buf, zval *val, int options 
TSRMLS_DC) /* {{{ */
 {
-       JSON_G(error_code) = PHP_JSON_ERROR_NONE;
        switch (Z_TYPE_P(val))
        {
                case IS_NULL:
@@ -680,6 +679,8 @@
                return;
        }

+       JSON_G(error_code) = PHP_JSON_ERROR_NONE;
+
        php_json_encode(&buf, parameter, options TSRMLS_CC);

        ZVAL_STRINGL(return_value, buf.c, buf.len, 1);

Added: php/php-src/trunk/ext/json/tests/bug54058.phpt
===================================================================
--- php/php-src/trunk/ext/json/tests/bug54058.phpt                              
(rev 0)
+++ php/php-src/trunk/ext/json/tests/bug54058.phpt      2011-02-21 08:09:02 UTC 
(rev 308529)
@@ -0,0 +1,35 @@
+--TEST--
+Bug #54058 (json_last_error() invalid UTF-8 produces wrong error)
+--SKIPIF--
+<?php if (!extension_loaded("json")) print "skip"; ?>
+--FILE--
+<?php
+
+$bad_utf8 = quoted_printable_decode('=B0');
+
+json_encode($bad_utf8);
+var_dump(json_last_error());
+
+$a = new stdclass;
+$a->foo = quoted_printable_decode('=B0');
+json_encode($a);
+var_dump(json_last_error());
+
+$b = new stdclass;
+$b->foo = $bad_utf8;
+$b->bar = 1;
+json_encode($b);
+var_dump(json_last_error());
+
+$c = array(
+    'foo' => $bad_utf8,
+    'bar' => 1
+);
+json_encode($c);
+var_dump(json_last_error());
+?>
+--EXPECTF--
+int(5)
+int(5)
+int(5)
+int(5)

-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to