-
Notifications
You must be signed in to change notification settings - Fork 348
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
-ApermitInitializationLeak
command-line argument (#5173)
- Loading branch information
Showing
9 changed files
with
222 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...st/java/org/checkerframework/checker/test/junit/ResourceLeakPermitInitializationLeak.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.checkerframework.checker.test.junit; | ||
|
||
import java.io.File; | ||
import java.util.List; | ||
import org.checkerframework.checker.resourceleak.ResourceLeakChecker; | ||
import org.checkerframework.framework.test.CheckerFrameworkPerDirectoryTest; | ||
import org.junit.runners.Parameterized.Parameters; | ||
|
||
/** Tests for the Resource Leak Checker. */ | ||
public class ResourceLeakPermitInitializationLeak extends CheckerFrameworkPerDirectoryTest { | ||
public ResourceLeakPermitInitializationLeak(List<File> testFiles) { | ||
super( | ||
testFiles, | ||
ResourceLeakChecker.class, | ||
"resourceleak-permitinitializationleak", | ||
"-Anomsgtext", | ||
"-ApermitInitializationLeak", | ||
"-AwarnUnneededSuppressions", | ||
"-encoding", | ||
"UTF-8"); | ||
} | ||
|
||
@Parameters | ||
public static String[] getTestDirs() { | ||
return new String[] {"resourceleak-permitinitializationleak"}; | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
checker/tests/resourceleak-permitinitializationleak/InstanceInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// A test that the checker is sound in the presence of instance initializer blocks. | ||
// In the resourceleak-permitinitializationleak/ directory, it's a test that the | ||
// checker is unsound with the -ApermitInitializationLeak command-line argument. | ||
|
||
import java.net.Socket; | ||
import org.checkerframework.checker.mustcall.qual.*; | ||
|
||
class InstanceInitializer { | ||
// :: error: required.method.not.called | ||
private @Owning Socket s; | ||
|
||
private final int DEFAULT_PORT = 5; | ||
private final String DEFAULT_ADDR = "localhost"; | ||
|
||
{ | ||
try { | ||
// This assignment is OK, because it's the first assignment. | ||
s = new Socket(DEFAULT_ADDR, DEFAULT_PORT); | ||
} catch (Exception e) { | ||
} | ||
} | ||
|
||
{ | ||
try { | ||
// This assignment is not OK, because it's a reassignment without satisfying the mustcall | ||
// obligations of the previous value of `s`. | ||
// With -ApermitInitializationLeak, the Resource Leak Checker unsoundly permits it. | ||
s = new Socket(DEFAULT_ADDR, DEFAULT_PORT); | ||
} catch (Exception e) { | ||
} | ||
} | ||
|
||
{ | ||
try { | ||
// :: error: required.method.not.called | ||
Socket s1 = new Socket(DEFAULT_ADDR, DEFAULT_PORT); | ||
} catch (Exception e) { | ||
} | ||
} | ||
|
||
{ | ||
Socket s1 = null; | ||
try { | ||
s1 = new Socket(DEFAULT_ADDR, DEFAULT_PORT); | ||
} catch (Exception e) { | ||
} | ||
s1.close(); | ||
} | ||
|
||
public InstanceInitializer() throws Exception { | ||
s = new Socket(DEFAULT_ADDR, DEFAULT_PORT); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
checker/tests/resourceleak-permitinitializationleak/SocketContainer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// A simple class that has a Socket as an owning field. | ||
// This test exists to check that we gracefully handle assignments 1) | ||
// in the constructor and 2) to null. | ||
|
||
import java.io.*; | ||
import java.net.*; | ||
import org.checkerframework.checker.calledmethods.qual.*; | ||
import org.checkerframework.checker.mustcall.qual.*; | ||
|
||
@MustCall("close") class SocketContainer { | ||
@Owning Socket sock; | ||
|
||
public SocketContainer(String host, int port) throws Exception { | ||
sock = new Socket(host, port); | ||
sock = new Socket(host, port); | ||
} | ||
|
||
@EnsuresCalledMethods(value = "this.sock", methods = "close") | ||
public void close() throws IOException { | ||
sock.close(); | ||
// It's okay to assign a field to null after its obligations have been fulfilled, | ||
// without inducing a reset. | ||
sock = null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters