Browse Source

Catch another simple to detect case of infinite antcall loops

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@275186 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 22 years ago
parent
commit
03d8b12829
5 changed files with 46 additions and 5 deletions
  1. +7
    -0
      src/etc/testcases/taskdefs/ant.xml
  2. +2
    -3
      src/main/org/apache/tools/ant/Project.java
  3. +16
    -0
      src/main/org/apache/tools/ant/Target.java
  4. +17
    -2
      src/main/org/apache/tools/ant/taskdefs/Ant.java
  5. +4
    -0
      src/testcases/org/apache/tools/ant/taskdefs/AntTest.java

+ 7
- 0
src/etc/testcases/taskdefs/ant.xml View File

@@ -162,4 +162,11 @@
<echo>test2 is ${test2}</echo>
<echo>test1.x is ${test1.x}</echo>
</target>

<target name="infinite-loop-via-depends">
<antcall target="dependent"/>
</target>

<target name="middleman" depends="infinite-loop-via-depends"/>
<target name="dependent" depends="middleman"/>
</project>

+ 2
- 3
src/main/org/apache/tools/ant/Project.java View File

@@ -1589,8 +1589,7 @@ public class Project {
* Must not be <code>null</code>.
* @param targets A map of names to targets (String to Target).
* Must not be <code>null</code>.
* @return a vector of strings with the names of the targets in
* sorted order.
* @return a vector of Target objects in sorted order.
* @exception BuildException if there is a cyclic dependency among the
* targets, or if a named target does not exist.
*/
@@ -2096,4 +2095,4 @@ public class Project {
// is private/protected.
}
}
}
}

+ 16
- 0
src/main/org/apache/tools/ant/Target.java View File

@@ -255,6 +255,22 @@ public class Target implements TaskContainer {
}
}

/**
* Does this target depend on the named target?
*
* @since Ant 1.6
*/
public boolean dependsOn(String other) {
if (getProject() != null) {
List l = getProject().topoSort(getName(),
getProject().getTargets());
int myIdx = l.indexOf(this);
int otherIdx = l.indexOf(getProject().getTargets().get(other));
return myIdx >= otherIdx;
}
return false;
}

/**
* Sets the "if" condition to test on execution. This is the
* name of a property to test for existence - if the property


+ 17
- 2
src/main/org/apache/tools/ant/taskdefs/Ant.java View File

@@ -68,6 +68,7 @@ import org.apache.tools.ant.DefaultLogger;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.ProjectComponent;
import org.apache.tools.ant.ProjectHelper;
import org.apache.tools.ant.Target;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.PropertySet;
import org.apache.tools.ant.util.FileUtils;
@@ -363,7 +364,10 @@ public class Ant extends Task {
if (newProject.getProperty("ant.file")
.equals(getProject().getProperty("ant.file"))
&& getOwningTarget() != null) {
if (getOwningTarget().getName().equals("")) {

String owningTargetName = getOwningTarget().getName();

if (owningTargetName.equals("")) {
if (getTaskName().equals("antcall")) {
throw new BuildException("antcall must not be used at"
+ " the top level.");
@@ -372,9 +376,20 @@ public class Ant extends Task {
+ " top level must not invoke"
+ " its own build file.");
}
} else if (getOwningTarget().getName().equals(target)) {
} else if (owningTargetName.equals(target)) {
throw new BuildException(getTaskName() + " task calling "
+ "its own parent target.");
} else {
Target other =
(Target) getProject().getTargets().get(target);
if (other != null && other.dependsOn(owningTargetName)) {
throw new BuildException(getTaskName()
+ " task calling a target"
+ " that depends on"
+ " its parent target \'"
+ owningTargetName
+ "\'.");
}
}
}



+ 4
- 0
src/testcases/org/apache/tools/ant/taskdefs/AntTest.java View File

@@ -302,6 +302,10 @@ public class AntTest extends BuildFileTest {
assertTrue(getLog().indexOf("test1.x is 1") > -1);
}

public void testInfiniteLoopViaDepends() {
expectBuildException("infinite-loop-via-depends", "recursive call");
}

private class BasedirChecker implements BuildListener {
private String[] expectedBasedirs;
private int calls = 0;


Loading…
Cancel
Save