1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-23 17:05:26 +02:00

Bug 509833 - Fix unsafe method call in ClassTypeHelper.findOverriders()

Change-Id: I3368d7dd22ce8e207f6363509b6830099a8f0236
This commit is contained in:
Nathan Ridge 2017-01-02 20:25:47 -05:00
parent 6132605f03
commit aa0d656ab7
4 changed files with 14 additions and 9 deletions

View file

@ -839,7 +839,8 @@ public class ClassTypeHelper {
/**
* Returns all methods found in the index, that override the given {@code method}.
*/
public static ICPPMethod[] findOverriders(IIndex index, ICPPMethod method) throws CoreException {
public static ICPPMethod[] findOverriders(IIndex index, ICPPMethod method, IASTNode point)
throws CoreException {
if (!isVirtual(method))
return ICPPMethod.EMPTY_CPPMETHOD_ARRAY;
@ -848,18 +849,18 @@ public class ClassTypeHelper {
return ICPPMethod.EMPTY_CPPMETHOD_ARRAY;
ICPPClassType[] subclasses= getSubClasses(index, mcl);
return findOverriders(subclasses, method);
return findOverriders(subclasses, method, null);
}
/**
* Returns all methods belonging to the given set of classes that override the given {@code method}.
*/
public static ICPPMethod[] findOverriders(ICPPClassType[] subclasses, ICPPMethod method) {
public static ICPPMethod[] findOverriders(ICPPClassType[] subclasses, ICPPMethod method, IASTNode point) {
final char[] mname= method.getNameCharArray();
final ICPPFunctionType mft= method.getType();
final ArrayList<ICPPMethod> result= new ArrayList<>();
for (ICPPClassType subClass : subclasses) {
ICPPMethod[] methods= subClass.getDeclaredMethods();
ICPPMethod[] methods= ClassTypeHelper.getDeclaredMethods(subClass, point);
for (ICPPMethod candidate : methods) {
if (CharArrayUtils.equals(mname, candidate.getNameCharArray()) &&
functionTypesAllowOverride(mft, candidate.getType())) {

View file

@ -177,7 +177,7 @@ public class CHQueries {
* if there are none.
*/
static ICElement[] findOverriders(IIndex index, ICPPMethod binding) throws CoreException {
IBinding[] virtualOverriders= ClassTypeHelper.findOverriders(index, binding);
IBinding[] virtualOverriders= ClassTypeHelper.findOverriders(index, binding, null);
if (virtualOverriders.length > 0) {
ArrayList<ICElementHandle> list= new ArrayList<ICElementHandle>();
list.addAll(Arrays.asList(IndexUI.findRepresentative(index, binding)));

View file

@ -100,6 +100,7 @@ public class CRenameMethodProcessor extends CRenameGlobalProcessor {
CRefactoringArgument argument= getArgument();
IBinding binding= argument.getBinding();
IASTNode node= ASTInternal.getPhysicalNodeOfScope(argument.getScope());
ArrayList<IBinding> bindings= new ArrayList<>();
if (binding != null) {
recordRename(binding);
@ -110,7 +111,7 @@ public class CRenameMethodProcessor extends CRenameGlobalProcessor {
try {
IBinding[] bs= ClassTypeHelper.findOverridden(m, argument.getTranslationUnit());
bindings.addAll(Arrays.asList(bs));
bs= ClassTypeHelper.findOverriders(getIndex(), m);
bs= ClassTypeHelper.findOverriders(getIndex(), m, node);
bindings.addAll(Arrays.asList(bs));
} catch (CoreException e) {
status.addError(e.getMessage());

View file

@ -24,6 +24,7 @@ import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IASTComment;
import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorElseStatement;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorEndifStatement;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorIfStatement;
@ -58,7 +59,7 @@ public class LinkedNamesFinder {
if (target == null) {
return EMPTY_LOCATIONS_ARRAY;
}
BindingFinder bindingFinder = new BindingFinder(root);
BindingFinder bindingFinder = new BindingFinder(root, name);
bindingFinder.find(target);
return bindingFinder.getLocations();
}
@ -66,9 +67,11 @@ public class LinkedNamesFinder {
private static class BindingFinder {
private final IASTTranslationUnit root;
private final List<IRegion> locations;
private final IASTNode point;
public BindingFinder(IASTTranslationUnit root) {
public BindingFinder(IASTTranslationUnit root, IASTNode point) {
this.root = root;
this.point = point;
locations = new ArrayList<IRegion>();
}
@ -122,7 +125,7 @@ public class LinkedNamesFinder {
SubclassFinder subclassFinder = new SubclassFinder(ownerClass);
root.accept(subclassFinder);
return ClassTypeHelper.findOverriders(subclassFinder.getSubclasses(), method);
return ClassTypeHelper.findOverriders(subclassFinder.getSubclasses(), method, point);
}
public IRegion[] getLocations() {