Skip to content

Add compliant and noncompliant examples of java/[email protected] #81

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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 [email protected] defects=1}
public void preserveThreadInterruptionStatusRuleNoncompliant(int numTimes) throws RuntimeException {
try {
for (int i=0; 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 [email protected] defects=0}
public void preserveThreadInterruptionStatusRuleCompliant(int numTimes) throws RuntimeException {
try {
for (int i=0; i < numTimes; i++ ) {
Thread.sleep(1000L);
}
} catch (InterruptedException e) {
// Compliant: InterruptedException wrapped and rethrown using RuntimeException and resetting interrupt status.
Thread.currentThread().interrupt();
throw new RuntimeException(e);
}
}
// {/fact}
}