Skip to content

Commit 1627006

Browse files
committed
Do not disable the security manager on Java 17 VMs and newer as it is deprecated for removal.
This fixes spotbugs#1579. If needed, disabling the security manager can still be requested on Java 17 and newer by setting the 'edu.umd.cs.findbugs.securityManagerDisabled' property to 'false'. Using this property also allows to disable this functionality on older VMs.
1 parent b1c2a5e commit 1627006

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ This is the changelog for SpotBugs. This follows [Keep a Changelog v1.0.0](http:
55
Currently the versioning policy of this project follows [Semantic Versioning v2.0.0](http://semver.org/spec/v2.0.0.html).
66

77
## Unreleased - 2022-??-??
8+
### Fixed
9+
- Avoid warning on use of security manager on Java 17 and newer. ([#1579](https://github.com/spotbugs/spotbugs/issues/1579))
810

911
## 4.7.1 - 2022-06-26
1012
### Fixed

spotbugs/src/main/java/edu/umd/cs/findbugs/PluginLoader.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import javax.annotation.Nullable;
5959
import javax.annotation.WillClose;
6060

61+
import edu.umd.cs.findbugs.util.SecurityManagerHandler;
6162
import org.dom4j.Document;
6263
import org.dom4j.DocumentException;
6364
import org.dom4j.Element;
@@ -1472,7 +1473,7 @@ static synchronized void loadInitialPlugins() {
14721473
// Thread.currentThread().getContextClassLoader().getResource("my.java.policy");
14731474
// Policy.getPolicy().refresh();
14741475
try {
1475-
System.setSecurityManager(null);
1476+
SecurityManagerHandler.disableSecurityManager();
14761477
} catch (Throwable e) {
14771478
assert true; // keep going
14781479
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package edu.umd.cs.findbugs.util;
2+
3+
/**
4+
* Since Java 17, the security manager is deprecated for removal and invoking related methods
5+
* causes a warning to be printed to the console. This intermediate disables the use of
6+
* security manager-related APIs on Java 17 or later, unless using the security manager is
7+
* explicitly configured by setting the <i>edu.umd.cs.findbugs.securityManagerDisabled</i>
8+
* property.
9+
*/
10+
public class SecurityManagerHandler {
11+
12+
/**
13+
* Determines if the security manager is used by SpotBugs.
14+
*/
15+
public static boolean SECURITY_MANAGER_DISABLED;
16+
17+
static {
18+
boolean securityManagerDisabled;
19+
try {
20+
String property = System.getProperty("edu.umd.cs.findbugs.securityManagerDisabled");
21+
if (property != null) {
22+
securityManagerDisabled = Boolean.parseBoolean(property);
23+
} else {
24+
String version = System.getProperty("java.version");
25+
if (version.startsWith("1.")) {
26+
version = version.substring(2, 3);
27+
} else {
28+
int index = version.indexOf(".");
29+
if (index != -1) {
30+
version = version.substring(0, index);
31+
}
32+
}
33+
securityManagerDisabled = Integer.parseInt(version) > 16;
34+
}
35+
} catch (Throwable ignored) {
36+
securityManagerDisabled = false;
37+
}
38+
SECURITY_MANAGER_DISABLED = securityManagerDisabled;
39+
}
40+
41+
/**
42+
* Disables the security manager by setting {@link System#setSecurityManager(SecurityManager)}
43+
* to {@code null}.
44+
*/
45+
public static void disableSecurityManager() {
46+
if (SECURITY_MANAGER_DISABLED) {
47+
return;
48+
}
49+
doDisableSecurityManager();
50+
}
51+
52+
/**
53+
* This method is a safeguard for running this library on a JVM that might no longer include
54+
* the security manager API after removal. As the JVM verifies methods lazily, and since this
55+
* method will never be invoked, validation of this method with a missing type can never fail.
56+
*/
57+
private static void doDisableSecurityManager() {
58+
System.setSecurityManager(null);
59+
}
60+
}

0 commit comments

Comments
 (0)