From e8b95f1a21010f833c7a0c591e6059def8ab79ac Mon Sep 17 00:00:00 2001 From: Vitaly Polonetsky Date: Thu, 24 Sep 2020 18:52:59 -0700 Subject: [PATCH 1/4] Fixes #2157 System.exit() and its equivalents should be allowed in static main() methods, added check for Runtime.getRuntime().halt() --- pmd-java/src/main/resources/category/java/errorprone.xml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pmd-java/src/main/resources/category/java/errorprone.xml b/pmd-java/src/main/resources/category/java/errorprone.xml index 94875fb765..2febe30c74 100644 --- a/pmd-java/src/main/resources/category/java/errorprone.xml +++ b/pmd-java/src/main/resources/category/java/errorprone.xml @@ -1304,7 +1304,7 @@ public class GCCall { externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_errorprone.html#donotcallsystemexit"> Web applications should not call System.exit(), since only the web container or the -application server should stop the JVM. This rule also checks for the equivalent call Runtime.getRuntime().exit(). +application server should stop the JVM. This rule also checks for the equivalent calls Runtime.getRuntime().exit() and Runtime.getRuntime().halt(). 3 @@ -1315,8 +1315,8 @@ application server should stop the JVM. This rule also checks for the equivalent //Name[ starts-with(@Image,'System.exit') or - (starts-with(@Image,'Runtime.getRuntime') and ../../PrimarySuffix[ends-with(@Image,'exit')]) -] + (starts-with(@Image,'Runtime.getRuntime') and ../../PrimarySuffix[ends-with(@Image,'exit') or ends-with(@Image,'halt')]) +][not(ancestor::MethodDeclaration[1][@Name="main" and @Static = true()])] ]]> From 095f96a96674ca8f8897b3ef0fb88c1d64c98df6 Mon Sep 17 00:00:00 2001 From: Vitaly Polonetsky Date: Fri, 2 Oct 2020 00:09:31 -0700 Subject: [PATCH 2/4] Added tests to verify the fix for #2157 --- .../errorprone/xml/DoNotCallSystemExit.xml | 42 ++++++++++++++++++- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/DoNotCallSystemExit.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/DoNotCallSystemExit.xml index 931f5f1830..82fbeba58b 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/DoNotCallSystemExit.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/DoNotCallSystemExit.xml @@ -6,9 +6,15 @@ basic violations - 1 + 2 basic violations with Runtime - 1 + 2 + + + system exit in main + 0 + + + + + system exit in anonymous class inside main + 1 + + + From 1150e61ddd648b20a5a1ca81b7a2d36fdcfca73e Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Sat, 10 Oct 2020 12:50:12 +0200 Subject: [PATCH 3/4] [java] Rename DoNotCallSystemExit -> DoNotTerminateVM --- .../resources/category/java/errorprone.xml | 79 ++++++++++--------- ...temExitTest.java => DoNotTerminateVM.java} | 4 +- ...allSystemExit.xml => DoNotTerminateVM.xml} | 0 3 files changed, 45 insertions(+), 38 deletions(-) rename pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/errorprone/{DoNotCallSystemExitTest.java => DoNotTerminateVM.java} (78%) rename pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/{DoNotCallSystemExit.xml => DoNotTerminateVM.xml} (100%) diff --git a/pmd-java/src/main/resources/category/java/errorprone.xml b/pmd-java/src/main/resources/category/java/errorprone.xml index 2febe30c74..d0ac48edb3 100644 --- a/pmd-java/src/main/resources/category/java/errorprone.xml +++ b/pmd-java/src/main/resources/category/java/errorprone.xml @@ -1296,42 +1296,7 @@ public class GCCall { - - -Web applications should not call System.exit(), since only the web container or the -application server should stop the JVM. This rule also checks for the equivalent calls Runtime.getRuntime().exit() and Runtime.getRuntime().halt(). - - 3 - - - - - - - - - - - - + + + +Web applications should not call `System.exit()`, since only the web container or the +application server should stop the JVM. Otherwise a web application would terminate all other applications +running on the same application server. + +This rule also checks for the equivalent calls `Runtime.getRuntime().exit()` and `Runtime.getRuntime().halt()`. + +This rule was called *DoNotCallSystemExit* until PMD 6.29.0. + + 3 + + + + + + + + + + + + + Date: Sat, 10 Oct 2020 12:56:36 +0200 Subject: [PATCH 4/4] [doc] Update release notes, refs #2803, fixes #2157 --- docs/pages/release_notes.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index 0b4353d908..e0ef292bdb 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -23,6 +23,13 @@ See [#2758](https://github.com/pmd/pmd/pull/2758) for details. AbstractTokenizer and the custom tokenizers of Fortran, Perl and Ruby are deprecated now. +#### Renamed Rules + +* The Java rule {% rule "java/errorprone/DoNotCallSystemExit" %} has been renamed to + {% rule "java/errorprone/DoNotTerminateVM" %}, since it checks for all the following calls: + `System.exit(int)`, `Runtime.exit(int)`, `Runtime.halt(int)`. All these calls terminate + the Java VM, which is bad, if the VM runs an application server which many independent applications. + ### Fixed Issues * cpd @@ -41,6 +48,8 @@ AbstractTokenizer and the custom tokenizers of Fortran, Perl and Ruby are deprec * [#2759](https://github.com/pmd/pmd/issues/2759): \[java] False positive in UnusedAssignment * java-design * [#2708](https://github.com/pmd/pmd/issues/2708): \[java] False positive FinalFieldCouldBeStatic when using lombok Builder.Default +* java-errorprone + * [#2157](https://github.com/pmd/pmd/issues/2157): \[java] Improve DoNotCallSystemExit: permit call in main(), flag System.halt ### API Changes @@ -75,6 +84,7 @@ AbstractTokenizer and the custom tokenizers of Fortran, Perl and Ruby are deprec * [#2773](https://github.com/pmd/pmd/pull/2773): \[java] issue-2738: Adding null check to avoid npe when switch case is default - [Nimit Patel](https://github.com/nimit-patel) * [#2789](https://github.com/pmd/pmd/pull/2789): Add badge for reproducible build - [Dan Rollo](https://github.com/bhamail) * [#2791](https://github.com/pmd/pmd/pull/2791): \[apex] Analyze inner classes for sharing violations - [Jeff Bartolotta](https://github.com/jbartolotta-sfdc) +* [#2803](https://github.com/pmd/pmd/pull/2803): \[java] Improve DoNotCallSystemExit (Fixes #2157) - [Vitaly Polonetsky](https://github.com/mvitaly) {% endtocmaker %}