-
Notifications
You must be signed in to change notification settings - Fork 353
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
Owning fields should not be compared with string containment #6280
Merged
msridhar
merged 8 commits into
typetools:master
from
kelloggm:owning-field-string-comparison
Nov 3, 2023
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0bfdaf8
failing test
kelloggm 7becc3c
add issue URL
kelloggm caedc6c
Merge branch 'master' of https://github.com/typetools/checker-framewo…
kelloggm d997555
port Calvin's extraction of the relevant code into a method
kelloggm 9860ce4
fix bug
kelloggm d01ffc6
add a note
kelloggm 41fb766
Merge branch 'master' of https://github.com/typetools/checker-framewo…
kelloggm c12e758
test case
kelloggm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
32 changes: 32 additions & 0 deletions
32
checker/tests/resourceleak/OwningFieldStringComparison.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,32 @@ | ||
// Test case for https://github.com/typetools/checker-framework/issues/6276 | ||
|
||
import java.net.Socket; | ||
import org.checkerframework.checker.calledmethods.qual.*; | ||
import org.checkerframework.checker.mustcall.qual.*; | ||
|
||
@InheritableMustCall("a") | ||
public class OwningFieldStringComparison { | ||
|
||
// :: error: required.method.not.called | ||
@Owning Socket s; | ||
|
||
// important to the bug: the name of this field must contain | ||
// the name of the owning socket | ||
/* @NotOwning */ Socket s2; | ||
|
||
// Note this "destructor" closes the wrong socket | ||
@EnsuresCalledMethods(value = "this.s2", methods = "close") | ||
public void a() { | ||
try { | ||
this.s2.close(); | ||
} catch (Exception e) { | ||
|
||
} finally { | ||
try { | ||
this.s2.close(); | ||
} catch (Exception e) { | ||
|
||
} | ||
} | ||
} | ||
} |
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 @@ | ||
// Test case that shows that the fix for | ||
// https://github.com/typetools/checker-framework/issues/6276 | ||
// is not fooled by a must-call method that closes the owned field | ||
// of another instance of the class. | ||
|
||
import java.io.*; | ||
import org.checkerframework.checker.calledmethods.qual.*; | ||
import org.checkerframework.checker.mustcall.qual.*; | ||
|
||
@InheritableMustCall("close") | ||
public class SneakyDestructor { | ||
// :: error: required.method.not.called | ||
private final @Owning Closeable resource; | ||
|
||
public SneakyDestructor(Closeable r) { | ||
this.resource = r; | ||
} | ||
|
||
// ... | ||
|
||
@EnsuresCalledMethods(value = "#1.resource", methods = "close") | ||
public void close(SneakyDestructor other) throws IOException { | ||
other.resource.close(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we also need to check if the receiver is
this
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
StringToJavaExpression
should handle that for us: it uses the supplied location (field
, the declaration of the owning field, in this case) and the given string to find the appropriate program element, and associates that with the result.A potential point of confusion in my implementation is that
field
is used for two purposes:StringToJavaExpression
converts the input string into is compared against itStringToJavaExpression
should consider the stringe
to occur in the code: that is, the contextThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could I write something like
?
In that case, the field referred to by the expression really is
SneakyDestructor.resource
, but the destructor clearly doesn't close its own@Owning
field.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand what you mean now, thanks. I fleshed out your test case and added it to this PR, and a bit to my surprise the tests are passing.