forked from typetools/checker-framework
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix unsoundess in Resource Leak Checker related to owning fields and …
…EnsuresCalledMethods (typetools#4869)
- Loading branch information
Showing
8 changed files
with
198 additions
and
9 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// A test case for https://github.com/typetools/checker-framework/issues/4838. | ||
|
||
import java.io.Closeable; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import org.checkerframework.checker.calledmethods.qual.EnsuresCalledMethods; | ||
import org.checkerframework.checker.mustcall.qual.Owning; | ||
|
||
class ReplicaInputStreams implements Closeable { | ||
|
||
private final @Owning InputStream in1; | ||
private final @Owning InputStream in2; | ||
|
||
public ReplicaInputStreams(@Owning InputStream i1, @Owning InputStream i2) { | ||
this.in1 = i1; | ||
this.in2 = i2; | ||
} | ||
|
||
@Override | ||
@EnsuresCalledMethods( | ||
value = {"this.in1", "this.in2"}, | ||
methods = {"close"}) | ||
// :: error: destructor.exceptional.postcondition | ||
public void close() throws IOException { | ||
in1.close(); | ||
in2.close(); | ||
} | ||
} |
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,31 @@ | ||
// A test case for https://github.com/typetools/checker-framework/issues/4838. | ||
// This variant uses a try-finally in the destructor, so it is correct. | ||
|
||
import java.io.Closeable; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import org.checkerframework.checker.calledmethods.qual.EnsuresCalledMethods; | ||
import org.checkerframework.checker.mustcall.qual.Owning; | ||
|
||
class ReplicaInputStreams2 implements Closeable { | ||
|
||
private final @Owning InputStream in1; | ||
private final @Owning InputStream in2; | ||
|
||
public ReplicaInputStreams2(@Owning InputStream i1, @Owning InputStream i2) { | ||
this.in1 = i1; | ||
this.in2 = i2; | ||
} | ||
|
||
@Override | ||
@EnsuresCalledMethods( | ||
value = {"this.in1", "this.in2"}, | ||
methods = {"close"}) | ||
public void close() throws IOException { | ||
try { | ||
in1.close(); | ||
} finally { | ||
in2.close(); | ||
} | ||
} | ||
} |
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,35 @@ | ||
// A test case for https://github.com/typetools/checker-framework/issues/4838. | ||
// | ||
// This test that shows that no unsoundess occurs when a single close() method is responsible | ||
// for closing two resources. | ||
|
||
import java.io.IOException; | ||
import java.net.Socket; | ||
import org.checkerframework.checker.calledmethods.qual.*; | ||
import org.checkerframework.checker.mustcall.qual.*; | ||
|
||
@MustCall("dispose") class TwoResourcesECM { | ||
@Owning Socket s1, s2; | ||
|
||
// The contracts.postcondition error below is thrown because s1 is not final, | ||
// and therefore might theoretically be side-effected by the call to s2.close() | ||
// even on the non-exceptional path. See ReplicaInputStreams.java for a variant | ||
// of this test where such an error is not issued. Because this method can leak | ||
// along both regular and exceptional exits, both errors are issued. | ||
@EnsuresCalledMethods( | ||
value = {"this.s1", "this.s2"}, | ||
methods = {"close"}) | ||
// :: error: contracts.postcondition :: error: destructor.exceptional.postcondition | ||
public void dispose() throws IOException { | ||
s1.close(); | ||
s2.close(); | ||
} | ||
|
||
static void test1(TwoResourcesECM obj) { | ||
try { | ||
obj.dispose(); | ||
} catch (IOException ioe) { | ||
|
||
} | ||
} | ||
} |
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