JSON Java API 的一个 bug

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

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

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote>
  • You can use BBCode tags in the text.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. The supported tag styles are: <foo>, [foo].
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.

More information about formatting options

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