Skip to content

Commit bcb41f4

Browse files
committed
Guard all the places that ant sets the security manager
- This copies the code because org.eclipse.ant.internal.launching.remote.InternalAntRunner can't see (much of) anything else define by org.eclipse.ant. #1660
1 parent 34dc0b9 commit bcb41f4

File tree

2 files changed

+39
-19
lines changed
  • ant
    • org.eclipse.ant.launching/remote/org/eclipse/ant/internal/launching/remote
    • org.eclipse.ant.ui/Ant Tools Support/org/eclipse/ant/internal/ui/model

2 files changed

+39
-19
lines changed

ant/org.eclipse.ant.launching/remote/org/eclipse/ant/internal/launching/remote/InternalAntRunner.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import org.apache.tools.ant.Task;
5151
import org.apache.tools.ant.TaskAdapter;
5252
import org.apache.tools.ant.util.FileUtils;
53+
import org.apache.tools.ant.util.JavaEnvUtils;
5354
import org.eclipse.ant.internal.launching.remote.logger.RemoteAntBuildLogger;
5455

5556
/**
@@ -58,6 +59,17 @@
5859
*/
5960
public class InternalAntRunner {
6061

62+
private static final boolean IS_SECURITY_MANAGER_SUPPORTED = isSecurityManagerAllowed();
63+
64+
private static boolean isSecurityManagerAllowed() {
65+
String sm = System.getProperty("java.security.manager"); //$NON-NLS-1$
66+
if (sm == null) { // default is 'disallow' since 18 and was 'allow' before
67+
return !JavaEnvUtils.isAtLeastJavaVersion("18"); //$NON-NLS-1$
68+
}
69+
// Value is either 'disallow' or 'allow' or specifies the SecurityManager class to set
70+
return !"disallow".equals(sm); //$NON-NLS-1$
71+
}
72+
6173
/**
6274
* Message priority for project help messages.
6375
*/
@@ -443,7 +455,9 @@ private void run(List<String> argList) {
443455
printArguments(getCurrentProject());
444456
}
445457
try {
446-
System.setSecurityManager(new AntSecurityManager(originalSM, Thread.currentThread()));
458+
if (IS_SECURITY_MANAGER_SUPPORTED) {
459+
System.setSecurityManager(new AntSecurityManager(originalSM, Thread.currentThread()));
460+
}
447461
}
448462
catch (UnsupportedOperationException ex) {
449463
logMessage(null, RemoteAntMessages.getString("InternalAntRunner.SecurityManagerError"), Project.MSG_WARN); //$NON-NLS-1$

ant/org.eclipse.ant.ui/Ant Tools Support/org/eclipse/ant/internal/ui/model/AntModel.java

+24-18
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import org.apache.tools.ant.Task;
5050
import org.apache.tools.ant.TaskAdapter;
5151
import org.apache.tools.ant.UnknownElement;
52+
import org.apache.tools.ant.util.JavaEnvUtils;
5253
import org.eclipse.ant.core.AntCorePlugin;
5354
import org.eclipse.ant.core.AntCorePreferences;
5455
import org.eclipse.ant.core.AntSecurityException;
@@ -89,6 +90,17 @@
8990
@SuppressWarnings("removal") // SecurityManager
9091
public class AntModel implements IAntModel {
9192

93+
private static final boolean IS_SECURITY_MANAGER_SUPPORTED = isSecurityManagerAllowed();
94+
95+
private static boolean isSecurityManagerAllowed() {
96+
String sm = System.getProperty("java.security.manager"); //$NON-NLS-1$
97+
if (sm == null) { // default is 'disallow' since 18 and was 'allow' before
98+
return !JavaEnvUtils.isAtLeastJavaVersion("18"); //$NON-NLS-1$
99+
}
100+
// Value is either 'disallow' or 'allow' or specifies the SecurityManager class to set
101+
return !"disallow".equals(sm); //$NON-NLS-1$
102+
}
103+
92104
private static ClassLoader fgClassLoader;
93105
private static int fgInstanceCount = 0;
94106
private static Object loaderLock = new Object();
@@ -363,8 +375,10 @@ private void parseDocument(IDocument input) {
363375
SecurityManager origSM = System.getSecurityManager();
364376
processAntHome(true);
365377
try {
366-
// set a security manager to disallow system exit and system property setting
367-
System.setSecurityManager(new AntSecurityManager(origSM, Thread.currentThread(), false));
378+
if (IS_SECURITY_MANAGER_SUPPORTED) {
379+
// set a security manager to disallow system exit and system property setting
380+
System.setSecurityManager(new AntSecurityManager(origSM, Thread.currentThread(), false));
381+
}
368382
resolveBuildfile();
369383
endReporting();
370384
// clear the additional property-holder(s) to avoid potential memory leaks
@@ -379,7 +393,9 @@ private void parseDocument(IDocument input) {
379393
finally {
380394
Thread.currentThread().setContextClassLoader(originalClassLoader);
381395
getClassLoader(null);
382-
System.setSecurityManager(origSM);
396+
if (System.getSecurityManager() instanceof AntSecurityManager) {
397+
System.setSecurityManager(origSM);
398+
}
383399
project.fireBuildFinished(null); // cleanup (IntrospectionHelper)
384400
}
385401
}
@@ -550,9 +566,7 @@ private void setGlobalProperties(Project project) {
550566

551567
private void resolveBuildfile() {
552568
Collection<AntTaskNode> nodeCopy = new ArrayList<>(fTaskNodes);
553-
Iterator<AntTaskNode> iter = nodeCopy.iterator();
554-
while (iter.hasNext()) {
555-
AntTaskNode node = iter.next();
569+
for (AntTaskNode node : nodeCopy) {
556570
fNodeBeingResolved = node;
557571
fNodeBeingResolvedIndex = -1;
558572
if (node.configure(false)) {
@@ -1439,10 +1453,8 @@ private void reconcileTaskAndTypes() {
14391453
if (fCurrentNodeIdentifiers == null || fDefinerNodeIdentifierToDefinedTasks == null) {
14401454
return;
14411455
}
1442-
Iterator<String> iter = fDefinerNodeIdentifierToDefinedTasks.keySet().iterator();
14431456
ComponentHelper helper = ComponentHelper.getComponentHelper(fProjectNode.getProject());
1444-
while (iter.hasNext()) {
1445-
String key = iter.next();
1457+
for (String key : fDefinerNodeIdentifierToDefinedTasks.keySet()) {
14461458
if (fCurrentNodeIdentifiers.get(key) == null) {
14471459
removeDefinerTasks(key, helper.getAntTypeTable());
14481460
}
@@ -1579,9 +1591,7 @@ public AntElementNode getReferenceNode(String text) {
15791591
}
15801592

15811593
Set<Task> nodes = fTaskToNode.keySet();
1582-
Iterator<Task> iter = nodes.iterator();
1583-
while (iter.hasNext()) {
1584-
Task task = iter.next();
1594+
for (Task task : nodes) {
15851595
Task tmptask = task;
15861596
if (tmptask instanceof UnknownElement) {
15871597
UnknownElement element = (UnknownElement) tmptask;
@@ -1720,9 +1730,7 @@ protected void addDefinedTasks(List<String> newTasks, AntDefiningTaskNode node)
17201730
fCurrentNodeIdentifiers.remove(identifier);
17211731
}
17221732
fDefinerNodeIdentifierToDefinedTasks.put(identifier, newTasks);
1723-
Iterator<String> iter = newTasks.iterator();
1724-
while (iter.hasNext()) {
1725-
String name = iter.next();
1733+
for (String name : newTasks) {
17261734
fTaskNameToDefiningNode.put(name, node);
17271735
}
17281736
}
@@ -1800,9 +1808,7 @@ private String getPrefixMapping(String prefix) {
18001808
private String getUserPrefixMapping(String prefix) {
18011809
if (fNamespacePrefixMappings != null) {
18021810
Set<Entry<String, String>> entrySet = fNamespacePrefixMappings.entrySet();
1803-
Iterator<Entry<String, String>> entries = entrySet.iterator();
1804-
while (entries.hasNext()) {
1805-
Map.Entry<String, String> entry = entries.next();
1811+
for (Entry<String, String> entry : entrySet) {
18061812
if (entry.getValue().equals(prefix)) {
18071813
return entry.getKey();
18081814
}

0 commit comments

Comments
 (0)