Skip to content

Commit 71b8e5f

Browse files
committed
Make sure we don't try to get code out of bounds
1 parent 1dc08f6 commit 71b8e5f

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

java/src/processing/mode/java/pdex/PDEX.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,7 +1144,7 @@ private void handleSketchProblems(PreprocessedSketch ps) {
11441144
static private JavaProblem convertIProblem(IProblem iproblem, PreprocessedSketch ps) {
11451145
SketchInterval in = ps.mapJavaToSketch(iproblem);
11461146
if (in == SketchInterval.BEFORE_START) return null;
1147-
String badCode = ps.pdeCode.substring(in.startPdeOffset, in.stopPdeOffset);
1147+
String badCode = ps.getPdeCode(in);
11481148
int line = ps.tabOffsetToTabLine(in.tabIndex, in.startTabOffset);
11491149
JavaProblem p = JavaProblem.fromIProblem(iproblem, in.tabIndex, line, badCode);
11501150
p.setPDEOffsets(in.startTabOffset, in.stopTabOffset);
@@ -1213,7 +1213,7 @@ static private List<JavaProblem> checkForCurlyQuotes(PreprocessedSketch ps) {
12131213
case IProblem.UnterminatedString:
12141214
SketchInterval in = ps.mapJavaToSketch(iproblem);
12151215
if (in == SketchInterval.BEFORE_START) continue;
1216-
String badCode = ps.pdeCode.substring(in.startPdeOffset, in.stopPdeOffset);
1216+
String badCode = ps.getPdeCode(in);
12171217
matcher.reset(badCode);
12181218
while (matcher.find()) {
12191219
int offset = matcher.start();

java/src/processing/mode/java/pdex/PreprocessedSketch.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,14 @@ public boolean inRange(SketchInterval interval) {
7777
}
7878

7979

80+
public String getPdeCode(SketchInterval si) {
81+
if (si == SketchInterval.BEFORE_START) return "";
82+
int stop = Math.min(si.stopPdeOffset, pdeCode.length());
83+
int start = Math.min(si.startPdeOffset, stop);
84+
return pdeCode.substring(start, stop);
85+
}
86+
87+
8088
public SketchInterval mapJavaToSketch(ASTNode node) {
8189
return mapJavaToSketch(node.getStartPosition(),
8290
node.getStartPosition() + node.getLength());

0 commit comments

Comments
 (0)