groovy
  1. groovy
  2. GROOVY-5144

JsonSlurper does not handle backslashes at the end of a String

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.8.4
    • Fix Version/s: 1.8.6, 2.0-beta-3
    • Component/s: JSON
    • Labels:
      None
    • Environment:
      Ubuntu Linux 11.10 32-bit,

      OpenJDK Runtime Environment (IcedTea6 1.11pre) (6b23~pre11-0ubuntu1.11.10)
      OpenJDK Server VM (build 20.0-b11, mixed mode)
    • Number of attachments :
      1

      Description

      Whenever I try to deserialize a JSON Object with a String that ends with a correctly escaped backslash "
      ", it throws an exception:

      Caught: groovy.json.JsonException: Expected a value on line: 1, column: 25.
      But got an unterminated object.
      groovy.json.JsonException: Expected a value on line: 1, column: 25.
      But got an unterminated object.
      at JsonSlurperBug.run(JsonSlurperBug.groovy:9)

      I've attached a small repro.

        Activity

        Hide
        Ed Norris added a comment - - edited

        This is worse than it appears - if you have an element after the offending escaped backslash, the parseText statement hangs (indefinitely? I've waited 20 minutes)
        works:

        {"InstallLocation":"C:\\WINDOWS\\Microsoft.NET\\Framework\\v3.5","a":"b"}

        throws exception:

        {"InstallLocation":"C:\\WINDOWS\\Microsoft.NET\\Framework\\v3.5\\"}

        hangs:

        {"InstallLocation":"C:\\WINDOWS\\Microsoft.NET\\Framework\\v3.5\\","a":"b"}

        Windows 7 x64, groovy-all-1.8.4, Java 1.6.0.24, IntelliJ Idea 10.5.4

        Show
        Ed Norris added a comment - - edited This is worse than it appears - if you have an element after the offending escaped backslash, the parseText statement hangs (indefinitely? I've waited 20 minutes) works: {"InstallLocation":"C:\\WINDOWS\\Microsoft.NET\\Framework\\v3.5","a":"b"} throws exception: {"InstallLocation":"C:\\WINDOWS\\Microsoft.NET\\Framework\\v3.5\\"} hangs: {"InstallLocation":"C:\\WINDOWS\\Microsoft.NET\\Framework\\v3.5\\","a":"b"} Windows 7 x64, groovy-all-1.8.4, Java 1.6.0.24, IntelliJ Idea 10.5.4
        Hide
        John Wagenleitner added a comment -

        Problem appears to be this line in JsonLexer#nextToken that determines when the string is terminated by a quote. Any backslash (current -2) appearing before a quote (current char) causes it to keep reading rather than terminating and returning the token. When it is able to read to the next quote that does not have a preceeding backslash I think it then hangs when the pattern match is applied by possibleTokenType.matching(currentContent.toString()).

        I have opened a pull request (https://github.com/groovy/groovy-core/pull/14) on github for this issue.

        Show
        John Wagenleitner added a comment - Problem appears to be this line in JsonLexer#nextToken that determines when the string is terminated by a quote. Any backslash (current -2) appearing before a quote (current char) causes it to keep reading rather than terminating and returning the token. When it is able to read to the next quote that does not have a preceeding backslash I think it then hangs when the pattern match is applied by possibleTokenType.matching(currentContent.toString()) . I have opened a pull request ( https://github.com/groovy/groovy-core/pull/14 ) on github for this issue.
        Hide
        Guillaume Laforge added a comment -

        Thanks for the pull request!

        Show
        Guillaume Laforge added a comment - Thanks for the pull request!

          People

          • Assignee:
            Guillaume Laforge
            Reporter:
            Gabriel Szlechtman
          • Votes:
            2 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: