tony2001                Thu Jul 20 08:56:24 2006 UTC

  Added files:                 
    /php-src/ext/json/tests     002.phpt 003.phpt 004.phpt 005.phpt 

  Modified files:              
    /php-src/ext/json   json.c 
    /php-src/ext/json/tests     001.phpt 
  Log:
  add recursion protection to json_encode() and new tests
  
  
http://cvs.php.net/viewvc.cgi/php-src/ext/json/json.c?r1=1.11&r2=1.12&diff_format=u
Index: php-src/ext/json/json.c
diff -u php-src/ext/json/json.c:1.11 php-src/ext/json/json.c:1.12
--- php-src/ext/json/json.c:1.11        Thu Jul 20 07:40:41 2006
+++ php-src/ext/json/json.c     Thu Jul 20 08:56:24 2006
@@ -16,7 +16,7 @@
   +----------------------------------------------------------------------+
 */
 
-/* $Id: json.c,v 1.11 2006/07/20 07:40:41 tony2001 Exp $ */
+/* $Id: json.c,v 1.12 2006/07/20 08:56:24 tony2001 Exp $ */
 
 #ifdef HAVE_CONFIG_H
 #include "config.h"
@@ -135,6 +135,11 @@
         r = 1;
     }
 
+    if (myht && myht->nApplyCount > 1) {
+        php_error_docref(NULL TSRMLS_CC, E_RECOVERABLE_ERROR, "recursion 
detected");
+        return;
+    }
+
     if (r == 0)
     {
         smart_str_appendc(buf, '[');
@@ -151,6 +156,7 @@
         ulong index;
         uint key_len;
         HashPosition pos;
+        HashTable *tmp_ht;
         int need_comma = 0;
 
         zend_hash_internal_pointer_reset_ex(myht, &pos);
@@ -160,6 +166,11 @@
                 break;
 
             if (zend_hash_get_current_data_ex(myht, (void **) &data, &pos) == 
SUCCESS) {
+                tmp_ht = HASH_OF(*data);
+                if (tmp_ht) {
+                    tmp_ht->nApplyCount++;
+                }
+
                 if (r == 0) {
                     if (need_comma) {
                         smart_str_appendc(buf, ',');
@@ -200,6 +211,10 @@
                         json_encode_r(buf, *data TSRMLS_CC);
                     }
                 }
+
+                if (tmp_ht) {
+                    tmp_ht->nApplyCount--;
+                }
             }
         }
     }
http://cvs.php.net/viewvc.cgi/php-src/ext/json/tests/001.phpt?r1=1.1&r2=1.2&diff_format=u
Index: php-src/ext/json/tests/001.phpt
diff -u php-src/ext/json/tests/001.phpt:1.1 php-src/ext/json/tests/001.phpt:1.2
--- php-src/ext/json/tests/001.phpt:1.1 Thu Jul 20 08:23:45 2006
+++ php-src/ext/json/tests/001.phpt     Thu Jul 20 08:56:24 2006
@@ -1,5 +1,7 @@
 --TEST--
 json_decode() tests
+--SKIPIF--
+<?php if (!extension_loaded("json")) print "skip"; ?>
 --FILE--
 <?php
 

http://cvs.php.net/viewvc.cgi/php-src/ext/json/tests/002.phpt?view=markup&rev=1.1
Index: php-src/ext/json/tests/002.phpt
+++ php-src/ext/json/tests/002.phpt
--TEST--
json_encode() tests
--SKIPIF--
<?php if (!extension_loaded("json")) print "skip"; ?>
--FILE--
<?php

var_dump(json_encode(""));
var_dump(json_encode(NULL));
var_dump(json_encode(TRUE));
var_dump(json_encode(array(""=>"")));
var_dump(json_encode(array(array(1))));

var_dump(json_encode(1));
var_dump(json_encode("руссиш"));


echo "Done\n";
?>
--EXPECTF--     
string(2) """"
string(4) "null"
string(4) "true"
string(2) "{}"
string(5) "[[1]]"
string(1) "1"
string(38) ""\u0440\u0443\u0441\u0441\u0438\u0448""
Done

http://cvs.php.net/viewvc.cgi/php-src/ext/json/tests/003.phpt?view=markup&rev=1.1
Index: php-src/ext/json/tests/003.phpt
+++ php-src/ext/json/tests/003.phpt
--TEST--
json_encode() & endless loop - 1
--SKIPIF--
<?php if (!extension_loaded("json")) print "skip"; ?>
--FILE--
<?php

$a = array();
$a[] = &$a;

var_dump($a);
var_dump(json_encode($a));

echo "Done\n";
?>
--EXPECTF--     
array(1) {
  [0]=>
  &array(1) {
    [0]=>
    &array(1) {
      [0]=>
      *RECURSION*
    }
  }
}

Catchable fatal error: json_encode(): recursion detected in %s on line %d

http://cvs.php.net/viewvc.cgi/php-src/ext/json/tests/004.phpt?view=markup&rev=1.1
Index: php-src/ext/json/tests/004.phpt
+++ php-src/ext/json/tests/004.phpt
--TEST--
json_encode() & endless loop - 2
--SKIPIF--
<?php if (!extension_loaded("json")) print "skip"; ?>
--FILE--
<?php

$a = new stdclass;
$a->prop = $a;

var_dump($a);
var_dump(json_encode($a));

echo "Done\n";
?>
--EXPECTF--     
object(stdClass)#%d (1) {
  ["prop"]=>
  object(stdClass)#%d (1) {
    ["prop"]=>
    *RECURSION*
  }
}

Catchable fatal error: json_encode(): recursion detected in %s on line %d

http://cvs.php.net/viewvc.cgi/php-src/ext/json/tests/005.phpt?view=markup&rev=1.1
Index: php-src/ext/json/tests/005.phpt
+++ php-src/ext/json/tests/005.phpt
--TEST--
json_encode() & endless loop - 3
--SKIPIF--
<?php if (!extension_loaded("json")) print "skip"; ?>
--FILE--
<?php

$a = array();
$a[] = $a;

var_dump($a);
var_dump(json_encode($a));

echo "Done\n";
?>
--EXPECTF--     
array(1) {
  [0]=>
  array(1) {
    [0]=>
    *RECURSION*
  }
}

Catchable fatal error: json_encode(): recursion detected in %s on line %d

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

Reply via email to