From 6793fa5f19c983876b82a2a3e865b29824dec2e5 Mon Sep 17 00:00:00 2001 From: Riya Date: Thu, 14 Dec 2023 20:55:24 +0530 Subject: [PATCH 1/3] Add compliant and noncompliant examples of java/preserve-thread-interruption-status-rule@v1.0 --- .../PreserveThreadInterruptionStatusRule.java | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/java/detectors/preserve_thread_interruption_status_rule/PreserveThreadInterruptionStatusRule.java diff --git a/src/java/detectors/preserve_thread_interruption_status_rule/PreserveThreadInterruptionStatusRule.java b/src/java/detectors/preserve_thread_interruption_status_rule/PreserveThreadInterruptionStatusRule.java new file mode 100644 index 0000000..65aa637 --- /dev/null +++ b/src/java/detectors/preserve_thread_interruption_status_rule/PreserveThreadInterruptionStatusRule.java @@ -0,0 +1,39 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +package detectors.preserve_thread_interruption_status_rule; + +import java.lang.InterruptedException; +import java.lang.RuntimeException; +import java.lang.Thread; + +public class PreserveThreadInterruptionStatusRule { + + // {fact rule=preserve-thread-interruption-status-rule@v1.0 defects=1} + public void preserveThreadInterruptionStatusRuleNoncompliant(int numTimes) throws RuntimeException { + try { + for (int i; i < numTimes; i++ ) { + Thread.sleep(1000L); + } + // Noncompliant: InterruptedException wrapped and rethrown using RuntimeException but without resetting interrupt status. + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + // {/fact} + + // {fact rule=preserve-thread-interruption-status-rule@v1.0 defects=0} + public void preserveThreadInterruptionStatusRuleCompliant(int numTimes) throws RuntimeException { + try { + for (int i; i < numTimes; i++ ) { + Thread.sleep(1000L); + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new RuntimeException(e); + } + } + // {/fact} +} From 56f9d691c419efd1b6b987520ca812475115f960 Mon Sep 17 00:00:00 2001 From: Riya Date: Thu, 14 Dec 2023 20:57:05 +0530 Subject: [PATCH 2/3] Add compliant and noncompliant examples of java/preserve-thread-interruption-status-rule@v1.0 --- .../PreserveThreadInterruptionStatusRule.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/java/detectors/preserve_thread_interruption_status_rule/PreserveThreadInterruptionStatusRule.java b/src/java/detectors/preserve_thread_interruption_status_rule/PreserveThreadInterruptionStatusRule.java index 65aa637..7e466ff 100644 --- a/src/java/detectors/preserve_thread_interruption_status_rule/PreserveThreadInterruptionStatusRule.java +++ b/src/java/detectors/preserve_thread_interruption_status_rule/PreserveThreadInterruptionStatusRule.java @@ -31,6 +31,7 @@ public void preserveThreadInterruptionStatusRuleCompliant(int numTimes) throws R Thread.sleep(1000L); } } catch (InterruptedException e) { + // Compliant: InterruptedException wrapped and rethrown using RuntimeException and resetting interrupt status. Thread.currentThread().interrupt(); throw new RuntimeException(e); } From 1d710051ec04c848dd4ae62e73e69c1d17c938d6 Mon Sep 17 00:00:00 2001 From: Riya Date: Thu, 14 Dec 2023 20:59:07 +0530 Subject: [PATCH 3/3] Add compliant and noncompliant examples of java/preserve-thread-interruption-status-rule@v1.0 --- .../PreserveThreadInterruptionStatusRule.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/java/detectors/preserve_thread_interruption_status_rule/PreserveThreadInterruptionStatusRule.java b/src/java/detectors/preserve_thread_interruption_status_rule/PreserveThreadInterruptionStatusRule.java index 7e466ff..491e5cb 100644 --- a/src/java/detectors/preserve_thread_interruption_status_rule/PreserveThreadInterruptionStatusRule.java +++ b/src/java/detectors/preserve_thread_interruption_status_rule/PreserveThreadInterruptionStatusRule.java @@ -14,7 +14,7 @@ public class PreserveThreadInterruptionStatusRule { // {fact rule=preserve-thread-interruption-status-rule@v1.0 defects=1} public void preserveThreadInterruptionStatusRuleNoncompliant(int numTimes) throws RuntimeException { try { - for (int i; i < numTimes; i++ ) { + for (int i=0; i < numTimes; i++ ) { Thread.sleep(1000L); } // Noncompliant: InterruptedException wrapped and rethrown using RuntimeException but without resetting interrupt status. @@ -27,7 +27,7 @@ public void preserveThreadInterruptionStatusRuleNoncompliant(int numTimes) throw // {fact rule=preserve-thread-interruption-status-rule@v1.0 defects=0} public void preserveThreadInterruptionStatusRuleCompliant(int numTimes) throws RuntimeException { try { - for (int i; i < numTimes; i++ ) { + for (int i=0; i < numTimes; i++ ) { Thread.sleep(1000L); } } catch (InterruptedException e) {