JSON Java API 的一个 bug

用到 [JSON] 的官方 [Java API] 时,遇到了一个 bug,当解析“{"a":[a,b,”,会导致 OutOfMemoryException。简单来说,就是整个 JSON 字符串中,以一个不完整的 JSON Array 结尾并且最后一个有效字符是逗号时,会引发这个问题。补丁如下:

--- src/org/json/JSONArray.java 2008-02-02 06:59:17 +0000
+++ src/org/json/JSONArray.java 2008-02-02 10:42:30 +0000
@@ -122,8 +126,11 @@
             switch (x.nextClean()) {
             case ';':
             case ',':
-                if (x.nextClean() == ']') {
+                char c = x.nextClean();
+                if (c == ']') {
                     return;
+                } else if (c == 0) {
+                    throw x.syntaxError("Expected a ',' or ']'");
                 }
                 x.back();
                 break;

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You can use BBCode tags in the text.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.