]> granicus.if.org Git - php/commitdiff
MFH Allow a custom recursion depth to be specified for json_decode()
authorScott MacVicar <scottmac@php.net>
Thu, 14 May 2009 22:02:08 +0000 (22:02 +0000)
committerScott MacVicar <scottmac@php.net>
Thu, 14 May 2009 22:02:08 +0000 (22:02 +0000)
ext/json/JSON_parser.c
ext/json/JSON_parser.h
ext/json/json.c

index 6aae849f1627855f8e24e7f71cc98779d94db458..6fbdb548146b8fea0a35805f2a5d72d95c201b4a 100644 (file)
@@ -247,6 +247,11 @@ new_JSON_parser(int depth)
     jp->top = -1;
        jp->error_code = PHP_JSON_ERROR_NONE;
     jp->stack = (int*)ecalloc(depth, sizeof(int));
+    if (depth > JSON_PARSER_DEFAULT_DEPTH) {
+        jp->the_zstack = (zval **)safe_emalloc(depth, sizeof(zval), 0);
+    } else {
+        jp->the_zstack = &jp->the_static_zstack[0];
+    }
     push(jp, MODE_DONE);
     return jp;
 }
@@ -258,6 +263,9 @@ int
 free_JSON_parser(JSON_parser jp)
 {
     efree((void*)jp->stack);
+    if (jp->the_zstack != &jp->the_static_zstack[0]) {
+        efree(jp->the_zstack);
+    }
     efree((void*)jp);
     return false;
 }
index 0ed7e52a8f74bc5a41a33fb387e0e297357614e1..771ba967ac3ac0d55073f864744d8490bcaa5afd 100644 (file)
@@ -6,7 +6,7 @@
 #include "php.h"
 #include "ext/standard/php_smart_str.h"
 
-#define JSON_PARSER_MAX_DEPTH 512
+#define JSON_PARSER_DEFAULT_DEPTH 512
 
 typedef struct JSON_parser_struct {
     int state;
@@ -14,8 +14,8 @@ typedef struct JSON_parser_struct {
     int top;
        int error_code;
     int* stack;
-    zval *the_zstack[JSON_PARSER_MAX_DEPTH];
-
+    zval **the_zstack;
+    zval *the_static_zstack[JSON_PARSER_DEFAULT_DEPTH];
 } * JSON_parser;
 
 enum error_codes {
index 42721edac61e50d7de269de23b08e6f1b94bd0ac..a30d13ed36631b0ade29fabde9ba9eb6f1d2e462 100644 (file)
@@ -494,7 +494,7 @@ static PHP_FUNCTION(json_decode)
        char *str;
        int str_len, utf16_len;
        zend_bool assoc = 0; /* return JS objects as PHP objects by default */
-       long depth = JSON_PARSER_MAX_DEPTH;
+       long depth = JSON_PARSER_DEFAULT_DEPTH;
        zval *z;
        unsigned short *utf16;
        JSON_parser jp;
@@ -517,9 +517,9 @@ static PHP_FUNCTION(json_decode)
                RETURN_NULL();
        }
 
-       /* can be removed once we remove the max depth limit */
-       if (depth <= 0 || depth > JSON_PARSER_MAX_DEPTH) {
-               depth = JSON_PARSER_MAX_DEPTH;
+       if (depth <= 0) {
+               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Depth must greater than zero.");
+               RETURN_NULL();
        }
 
        ALLOC_INIT_ZVAL(z);