canopy icon indicating copy to clipboard operation
canopy copied to clipboard

Don't throw a bounds exception when reporting Java errors at EOF.

Open kristapsdz-saic opened this issue 3 years ago • 1 comments
trafficstars

This fixes an off-by-one for the Java sources. If the condition is <=, the body will read after the end of the array and throw a bounds exception. This only occurs in the situation of printing an exception that has occurred at exactly the last character.

diff --git a/node_modules/canopy/templates/java/Parser.java b/node_modules/canopy/templates/java/Parser.java
index 8d38b7a..46563ed 100644
--- a/node_modules/canopy/templates/java/Parser.java
+++ b/node_modules/canopy/templates/java/Parser.java
@@ -27,7 +27,7 @@ public class {{name}} extends Grammar {
         String[] lines = input.split("\n");
         int lineNo = 0, position = 0;

-        while (position <= offset) {
+        while (position < offset) {
             position += lines[lineNo].length() + 1;
             lineNo += 1;
         }

kristapsdz-saic avatar Apr 21 '22 17:04 kristapsdz-saic

Could you give an example of a grammar and input text that triggers the bug you're fixing?

jcoglan avatar Apr 22 '22 23:04 jcoglan