8178843: A bug in an inner loop in MethodGenerator's getLocals method

Reviewed-by: lancea
This commit is contained in:
Joe Wang 2019-07-11 15:58:54 +00:00
parent 61d10a507d
commit ea81ec7896

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
*/ */
/* /*
* Licensed to the Apache Software Foundation (ASF) under one or more * Licensed to the Apache Software Foundation (ASF) under one or more
@ -75,7 +75,7 @@ import java.util.Stack;
/** /**
* @author Jacek Ambroziak * @author Jacek Ambroziak
* @author Santiago Pericas-Geertsen * @author Santiago Pericas-Geertsen
* @LastModified: Nov 2017 * @LastModified: July 2019
*/ */
public class MethodGenerator extends MethodGen public class MethodGenerator extends MethodGen
implements com.sun.org.apache.xalan.internal.xsltc.compiler.Constants { implements com.sun.org.apache.xalan.internal.xsltc.compiler.Constants {
@ -207,7 +207,7 @@ public class MethodGenerator extends MethodGen
_nextNode = new INVOKEINTERFACE(index, 1); _nextNode = new INVOKEINTERFACE(index, 1);
_slotAllocator = new SlotAllocator(); _slotAllocator = new SlotAllocator();
_slotAllocator.initialize(getLocalVariableRegistry().getLocals(false)); _slotAllocator.initialize(getLocalVariableRegistry().getLocals());
_allocatorInit = true; _allocatorInit = true;
} }
@ -445,7 +445,7 @@ public class MethodGenerator extends MethodGen
} }
} }
} else { } else {
_nameToLVGMap.remove(lvg); _nameToLVGMap.remove(lvg.getName());
} }
} }
@ -480,55 +480,31 @@ public class MethodGenerator extends MethodGen
} }
/** /**
* <p>Gets all {@link LocalVariableGen} objects for this method.</p> * Gets all {@link LocalVariableGen} objects.
* <p>When the <code>includeRemoved</code> argument has the value * This method replaces {@link MethodGen#getLocalVariables()} which has
* <code>false</code>, this method replaces uses of
* {@link MethodGen#getLocalVariables()} which has
* a side-effect of setting the start and end range for any * a side-effect of setting the start and end range for any
* <code>LocalVariableGen</code> if either was <code>null</code>. That * {@code LocalVariableGen} if either was {@code null}. That
* side-effect causes problems for outlining of code in XSLTC. * side-effect causes problems for outlining of code in XSLTC.
* @param includeRemoved Specifies whether all local variables ever *
* declared should be returned (<code>true</code>) or only those not * @return an array of {@code LocalVariableGen} containing all the
* removed (<code>false</code>)
* @return an array of <code>LocalVariableGen</code> containing all the
* local variables * local variables
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
protected LocalVariableGen[] getLocals(boolean includeRemoved) { private LocalVariableGen[] getLocals() {
LocalVariableGen[] locals = null; LocalVariableGen[] locals = null;
List<LocalVariableGen> allVarsEverDeclared = new ArrayList<>(); List<LocalVariableGen> allVarsEverDeclared = new ArrayList<>();
if (includeRemoved) { for (Map.Entry<String, Object> nameVarsPair : _nameToLVGMap.entrySet()) {
int slotCount = allVarsEverDeclared.size(); Object vars = nameVarsPair.getValue();
if (vars != null) {
for (int i = 0; i < slotCount; i++) { if (vars instanceof ArrayList) {
Object slotEntries = _variables.get(i); List<LocalVariableGen> varsList =
if (slotEntries != null) { (List<LocalVariableGen>) vars;
if (slotEntries instanceof ArrayList) { for (int i = 0; i < varsList.size(); i++) {
List<LocalVariableGen> slotList = allVarsEverDeclared.add(varsList.get(i));
(List<LocalVariableGen>)slotEntries;
for (int j = 0; j < slotList.size(); j++) {
allVarsEverDeclared.add(slotList.get(i));
}
} else {
allVarsEverDeclared.add((LocalVariableGen)slotEntries);
}
}
}
} else {
for (Map.Entry<String, Object> nameVarsPair : _nameToLVGMap.entrySet()) {
Object vars = nameVarsPair.getValue();
if (vars != null) {
if (vars instanceof ArrayList) {
List<LocalVariableGen> varsList =
(List<LocalVariableGen>) vars;
for (int i = 0; i < varsList.size(); i++) {
allVarsEverDeclared.add(varsList.get(i));
}
} else {
allVarsEverDeclared.add((LocalVariableGen)vars);
} }
} else {
allVarsEverDeclared.add((LocalVariableGen)vars);
} }
} }
} }