VirtusLab / using_directives

12 stars 8 forks source link

java.lang.ArrayIndexOutOfBoundsException when parsing `/*` #60

Open bishabosha opened 3 months ago

bishabosha commented 3 months ago

The following source file will cause the parser to hang before emitting java.lang.ArrayIndexOutOfBoundsException

//> using /*

class Foo

errors with

Error: java.lang.ArrayIndexOutOfBoundsException: Index -2147483648 out of bounds for length 14

Workaround: surround with quotes.

Context

I tried out prototyping a directive where one of the values was a raw glob pattern, e.g.

//> using sourceGenerator foo.scala **/*.proto
SethTisue commented 1 day ago

reported again today on Discord (one of @bjornregnell's students hit it)

bjornregnell commented 1 day ago

Yes this was a bit frustrating for the beginner student - I guess it is easy to get bitten by this when you try to find what is wrong in your code by de-commenting stuff here and there with /* and then forgetting to close it with */ which is a kind of plausible error to make for a human... Not easy to know what to do when the build suddenly start to crash -> despair...

bjornregnell commented 1 day ago

NOTE: it is enough to have a code file with a single /* so it does not require a using-directive to crash... So this is pretty generally applicable; perhaps increase priority of this bug's labelling?

bjornregnell commented 1 day ago

Also we were first confused about where it crashed and long suspected the scala compiler which was a bit worrying until we understood that it was the runner.

bjornregnell commented 1 day ago

Minimal reproduction:

$ scala-cli version
Scala CLI version: 1.5.1
Scala version (default): 3.5.1
$ mkdir empty && cd empty
$ echo "/*" | cat >a.scala
$ scala-cli compile a.scala 
Error: java.lang.ArrayIndexOutOfBoundsException: Index -2147483648 out of bounds for length 3
For more details, please see '/home/bjornr/tmp/empty/.scala-build/stacktraces/1729085559-18129846909220856422.log'
$ head .scala-build/stacktraces/1729085559-18129846909220856422.log 
java.lang.ArrayIndexOutOfBoundsException: Index -2147483648 out of bounds for length 3
  com.virtuslab.using_directives.custom.CustomCharArrayReader.nextChar(CustomCharArrayReader.java:29)
  com.virtuslab.using_directives.custom.Scanner.lambda$skipComment$5(Scanner.java:298)
  com.virtuslab.using_directives.custom.Scanner.skipComment(Scanner.java:308)
  com.virtuslab.using_directives.custom.Scanner.doFetchToken(Scanner.java:187)
  com.virtuslab.using_directives.custom.Scanner.fetchToken(Scanner.java:144)
  com.virtuslab.using_directives.custom.Scanner.nextToken(Scanner.java:98)
  com.virtuslab.using_directives.custom.Scanner.<init>(Scanner.java:27)
  com.virtuslab.using_directives.custom.Parser.<init>(Parser.java:20)
  com.virtuslab.using_directives.UsingDirectivesProcessor.extract(UsingDirectivesProcessor.java:34)