Make sure we don't try to get code out of bounds

This commit is contained in:
Jakub Valtar
2017-09-19 23:14:26 +02:00
parent 25ac5db0ab
commit 8c66a8c33d
2 changed files with 10 additions and 2 deletions

View File

@@ -1144,7 +1144,7 @@ public class PDEX {
static private JavaProblem convertIProblem(IProblem iproblem, PreprocessedSketch ps) {
SketchInterval in = ps.mapJavaToSketch(iproblem);
if (in == SketchInterval.BEFORE_START) return null;
String badCode = ps.pdeCode.substring(in.startPdeOffset, in.stopPdeOffset);
String badCode = ps.getPdeCode(in);
int line = ps.tabOffsetToTabLine(in.tabIndex, in.startTabOffset);
JavaProblem p = JavaProblem.fromIProblem(iproblem, in.tabIndex, line, badCode);
p.setPDEOffsets(in.startTabOffset, in.stopTabOffset);
@@ -1213,7 +1213,7 @@ public class PDEX {
case IProblem.UnterminatedString:
SketchInterval in = ps.mapJavaToSketch(iproblem);
if (in == SketchInterval.BEFORE_START) continue;
String badCode = ps.pdeCode.substring(in.startPdeOffset, in.stopPdeOffset);
String badCode = ps.getPdeCode(in);
matcher.reset(badCode);
while (matcher.find()) {
int offset = matcher.start();

View File

@@ -77,6 +77,14 @@ public class PreprocessedSketch {
}
public String getPdeCode(SketchInterval si) {
if (si == SketchInterval.BEFORE_START) return "";
int stop = Math.min(si.stopPdeOffset, pdeCode.length());
int start = Math.min(si.startPdeOffset, stop);
return pdeCode.substring(start, stop);
}
public SketchInterval mapJavaToSketch(ASTNode node) {
return mapJavaToSketch(node.getStartPosition(),
node.getStartPosition() + node.getLength());