Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-10676

ErrorReporter (used by FileSystemCompiler) can throw an IOOB exception on files with CR only

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 3.0.12, 4.0.4
    • None
    • None

    Description

      Systems using CR only aren't common but Groovy successfully parses such files. This is true for the old and new parser.

      For Groovy versions prior to 3, Antlr2 counted CR as a new line. Antlr4 doesn't. This makes a minor difference in the reported line/column numbers in error messages but otherwise isn't a huge issue.

      For places where we use ErrorReporter however, the CR counting behavior causes a subsequent error.

      Here is an example:

      import org.codehaus.groovy.tools.ErrorReporter
      try {
          // error should be at column > 40, first line should be short
          new GroovyShell().parse('/*\r * some comment\r */\r           class class {}\r')
      } catch(e) {
          Writer data = new StringWriter()
          new ErrorReporter(e, true).write(new PrintWriter(data))
      }
      

      Which throws this error:

      java.lang.StringIndexOutOfBoundsException: begin 10, end 2, length 2
      	at java.base/java.lang.String.checkBoundsBeginEnd(String.java:3319)
      	at java.base/java.lang.String.substring(String.java:1874)
      	at org.codehaus.groovy.control.SourceUnit.getSample(SourceUnit.java:288)
      	at org.codehaus.groovy.control.messages.SyntaxErrorMessage.write(SyntaxErrorMessage.java:54)
      	at org.codehaus.groovy.control.ErrorCollector.write(ErrorCollector.java:306)
      

      Attachments

        Issue Links

          Activity

            People

              paulk Paul King
              paulk Paul King
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: