Skip to main content
aboutsummaryrefslogtreecommitdiffstats
path: root/codan
diff options
context:
space:
mode:
authorMarc-Andre Laperle2012-01-14 21:25:19 +0000
committerMarc-Andre Laperle2012-01-14 21:50:28 +0000
commitd98768295b4ad9f212dfd55c84b53ae9a2cab4ca (patch)
tree80ae09ca683b1b6917ac795c03d8e8166b33b7a6 /codan
parent59c4d10629cf7e8567ffb9759022212654c58615 (diff)
downloadorg.eclipse.cdt-d98768295b4ad9f212dfd55c84b53ae9a2cab4ca.tar.gz
org.eclipse.cdt-d98768295b4ad9f212dfd55c84b53ae9a2cab4ca.tar.xz
org.eclipse.cdt-d98768295b4ad9f212dfd55c84b53ae9a2cab4ca.zip
Bug 368446 - NonVirtualDestructor checker: stack overflow when class
inherits itself
Diffstat (limited to 'codan')
-rw-r--r--codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/NonVirtualDestructor.java15
-rw-r--r--codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/NonVirtualDestructorCheckerTest.java16
2 files changed, 27 insertions, 4 deletions
diff --git a/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/NonVirtualDestructor.java b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/NonVirtualDestructor.java
index b1d1afc0c69..84c697677a7 100644
--- a/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/NonVirtualDestructor.java
+++ b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/NonVirtualDestructor.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2009, 2011 Alena Laskavaia
+ * Copyright (c) 2009, 2012 Alena Laskavaia
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -12,6 +12,8 @@
*******************************************************************************/
package org.eclipse.cdt.codan.internal.checkers;
+import java.util.HashSet;
+
import org.eclipse.cdt.codan.core.cxx.model.AbstractIndexAstChecker;
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
@@ -33,6 +35,9 @@ import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBinding;
*/
public class NonVirtualDestructor extends AbstractIndexAstChecker {
public static final String PROBLEM_ID = "org.eclipse.cdt.codan.internal.checkers.NonVirtualDestructorProblem"; //$NON-NLS-1$
+
+ // Prevent stack overflow in case: class A: public A {};
+ private static HashSet<ICPPClassType> checkedClassTypes = new HashSet<ICPPClassType>();
@Override
public void processAst(IASTTranslationUnit ast) {
@@ -50,6 +55,7 @@ public class NonVirtualDestructor extends AbstractIndexAstChecker {
}
private static boolean hasVirtualDestructor(ICPPClassType classType) {
+ checkedClassTypes.add(classType);
ICPPMethod destructor = getDestructor(classType);
if (destructor != null && destructor.isVirtual()) {
return true;
@@ -58,7 +64,8 @@ public class NonVirtualDestructor extends AbstractIndexAstChecker {
for (ICPPBase base : bases) {
IBinding baseClass = base.getBaseClass();
if (baseClass instanceof ICPPClassType) {
- if (hasVirtualDestructor((ICPPClassType) baseClass)) {
+ ICPPClassType cppClassType = (ICPPClassType) baseClass;
+ if (!checkedClassTypes.contains(cppClassType) && hasVirtualDestructor(cppClassType)) {
return true;
}
}
@@ -81,7 +88,9 @@ public class NonVirtualDestructor extends AbstractIndexAstChecker {
return PROCESS_SKIP;
}
ICPPClassType classType = (ICPPClassType) binding;
- if (hasVirtualDestructor(classType)) {
+ boolean hasVirtualDestructor = hasVirtualDestructor(classType);
+ checkedClassTypes.clear();
+ if (hasVirtualDestructor) {
return PROCESS_SKIP;
}
ICPPMethod virtualMethod = null;
diff --git a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/NonVirtualDestructorCheckerTest.java b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/NonVirtualDestructorCheckerTest.java
index 2e5c88d8993..edecd93c067 100644
--- a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/NonVirtualDestructorCheckerTest.java
+++ b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/NonVirtualDestructorCheckerTest.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2011 Patrick Hofer and others.
+ * Copyright (c) 2011, 2012 Patrick Hofer and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -181,4 +181,18 @@ public class NonVirtualDestructorCheckerTest extends CheckerTestCase {
loadCodeAndRun(getAboveComment());
checkErrorLines(4);
}
+
+ // class C : public C {};
+ public void testBug368446_stackOverflow() throws Exception {
+ loadCodeAndRun(getAboveComment());
+ checkNoErrors();
+ }
+
+ // class B;
+ // class A : public B {};
+ // class B : public A {};
+ public void testBug368446_stackOverflow_indirect() throws Exception {
+ loadCodeAndRun(getAboveComment());
+ checkNoErrors();
+ }
}

Back to the top