Skip to content

Commit

Permalink
Check for undefined label before use
Browse files Browse the repository at this point in the history
  • Loading branch information
markro49 committed Feb 10, 2021
1 parent ddec1f1 commit 3e8d2f1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,25 +69,27 @@ public boolean isSatisfiedBy(TreePath path) {
return (loc.getVarIndex() == varIndex);
}

Pair<String, Pair<Integer, Integer>> key =
Pair.of(fullMethodName, Pair.of(loc.getVarIndex(), loc.getScopeStart()));
String potentialVarName = LocalVariableScanner.getFromMethodNameIndexMap(key);
if (potentialVarName != null) {
if (varName.equals(potentialVarName)) {
// now use methodNameCounter to ensure that if this is the
// i'th variable of this name, its offset is the i'th offset
// of all variables with this name
List<Integer> allOffsetsWithThisName =
LocalVariableScanner.getFromMethodNameCounter(fullMethodName, potentialVarName);
// methodNameCounter.get(fullMethodName).get(potentialVarName);
Integer thisVariablesOffset = allOffsetsWithThisName.indexOf(loc.getScopeStart());

// now you need to make sure that this is the
// thisVariablesOffset'th variable tree in the entire source
int i = LocalVariableScanner.indexOfVarTree(path, parent, potentialVarName);

if (i == thisVariablesOffset) {
return true;
if (loc.scopeStartDefined()) {
Pair<String, Pair<Integer, Integer>> key =
Pair.of(fullMethodName, Pair.of(loc.getVarIndex(), loc.getScopeStart()));
String potentialVarName = LocalVariableScanner.getFromMethodNameIndexMap(key);
if (potentialVarName != null) {
if (varName.equals(potentialVarName)) {
// now use methodNameCounter to ensure that if this is the
// i'th variable of this name, its offset is the i'th offset
// of all variables with this name
List<Integer> allOffsetsWithThisName =
LocalVariableScanner.getFromMethodNameCounter(fullMethodName, potentialVarName);
// methodNameCounter.get(fullMethodName).get(potentialVarName);
Integer thisVariablesOffset = allOffsetsWithThisName.indexOf(loc.getScopeStart());

// now you need to make sure that this is the
// thisVariablesOffset'th variable tree in the entire source
int i = LocalVariableScanner.indexOfVarTree(path, parent, potentialVarName);

if (i == thisVariablesOffset) {
return true;
}
}
}
}
Expand Down
26 changes: 18 additions & 8 deletions scene-lib/src/scenelib/annotations/el/LocalLocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,34 +105,44 @@ public LocalLocation(int scopeStart, int scopeLength, int index) {
this.variableName = null;
}

/**
* Test if the bytecode offset to the start of the first scope/lifetime is defined.
*
* @return if the Label at start[0] is resolved
*/
public boolean scopeStartDefined() {
try {
start[0].getOffset();
} catch (IllegalStateException e) {
return false;
}
return true;
}

/**
* Returns the bytecode offset to the start of the first scope/lifetime.
*
* @return the bytecode offset to the start of the first scope/lifetime or -1 if the Label at
* start[0] is not resolved
* @return the bytecode offset to the start of the first scope/lifetime
*/
public int getScopeStart() {
try {
return start[0].getOffset();
} catch (IllegalStateException e) {
System.err.println("Labels not resolved: " + Arrays.toString(start));
return -1;
throw new Error("Labels not resolved: " + Arrays.toString(start));
}
}

// This is used only in IndexFileWriter.
/**
* Returns the length of all the scopes/lifetimes (in bytes).
*
* @return the length of all the scopes/lifetimes (in bytes) or -1 if the Label at start[0] or
* end[end.length-1] is not resolved
* @return the length of all the scopes/lifetimes (in bytes)
*/
public int getScopeLength() {
try {
return end[end.length - 1].getOffset() - getScopeStart();
} catch (IllegalStateException e) {
System.err.println("Labels not resolved: " + Arrays.toString(end));
return -1;
throw new Error("Labels not resolved: " + Arrays.toString(start));
}
}

Expand Down

0 comments on commit 3e8d2f1

Please sign in to comment.