-
Notifications
You must be signed in to change notification settings - Fork 2k
Java: Ignore char array based closeables for CloseReader.ql and CloseWriter.ql #5868
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
Merged
aschackmull
merged 5 commits into
github:main
from
Marcono1234:marcono1234/ignore-not-closing-char-array-closeable
Jun 2, 2021
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2e098f0
Java: Ignore char array based closeables for CloseReader.ql and Close…
Marcono1234 8969da7
Java: Improve not closing resource query; add tests
Marcono1234 73c7e15
Java: Add back StringInputStream to CloseReader.ql
Marcono1234 e205e4b
Java: Add change note for close resource query changes
Marcono1234 ef410b9
Update java/change-notes/2021-05-14-close-resource-leaks-improvements.md
smowton 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
2 changes: 2 additions & 0 deletions
2
java/change-notes/2021-05-14-close-resource-leaks-improvements.md
This file contains hidden or 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,2 @@ | ||
| lgtm,codescanning | ||
| * The "Potential input resource leak" (`java/input-resource-leak`) and "Potential output resource leak" (`java/output-resource-leak`) queries no longer confuse `java.io` classes such as `Reader` with others that happen to share the same base name. Additionally the number of false positives has been reduced by recognizing `CharArrayReader` and `CharArrayWriter` as types that don't need to be closed. |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
6 changes: 4 additions & 2 deletions
6
java/ql/test/query-tests/CloseResource/CloseReader/CloseReader.expected
This file contains hidden or 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 |
|---|---|---|
| @@ -1,2 +1,4 @@ | ||
| | CloseReader.java:11:42:11:71 | new FileReader(...) | This FileReader is not always closed on method exit. | | ||
| | CloseReader.java:44:6:44:40 | new FileInputStream(...) | This FileInputStream is not always closed on method exit. | | ||
| | CloseReader.java:18:42:18:71 | new FileReader(...) | This FileReader is not always closed on method exit. | | ||
| | CloseReader.java:23:20:23:50 | new FileInputStream(...) | This FileInputStream is not always closed on method exit. | | ||
| | CloseReader.java:33:6:33:40 | new FileInputStream(...) | This FileInputStream is not always closed on method exit. | | ||
| | CloseReader.java:43:21:43:43 | new ZipFile(...) | This ZipFile is not always closed on method exit. | |
This file contains hidden or 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
2 changes: 1 addition & 1 deletion
2
java/ql/test/query-tests/CloseResource/CloseReader/CloseReader.qlref
This file contains hidden or 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 |
|---|---|---|
| @@ -1 +1 @@ | ||
| Likely Bugs/Resource Leaks/CloseReader.ql | ||
| Likely Bugs/Resource Leaks/CloseReader.ql |
3 changes: 3 additions & 0 deletions
3
java/ql/test/query-tests/CloseResource/CloseWriter/CloseWriter.expected
This file contains hidden or 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,3 @@ | ||
| | CloseWriter.java:17:42:17:71 | new FileWriter(...) | This FileWriter is not always closed on method exit. | | ||
| | CloseWriter.java:22:22:22:53 | new FileOutputStream(...) | This FileOutputStream is not always closed on method exit. | | ||
| | CloseWriter.java:32:6:32:41 | new FileOutputStream(...) | This FileOutputStream is not always closed on method exit. | |
131 changes: 131 additions & 0 deletions
131
java/ql/test/query-tests/CloseResource/CloseWriter/CloseWriter.java
This file contains hidden or 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,131 @@ | ||
| import java.io.BufferedWriter; | ||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.CharArrayWriter; | ||
| import java.io.Closeable; | ||
| import java.io.FileOutputStream; | ||
| import java.io.FileWriter; | ||
| import java.io.IOException; | ||
| import java.io.OutputStream; | ||
| import java.io.OutputStreamWriter; | ||
| import java.io.StringWriter; | ||
| import java.io.Writer; | ||
| import java.util.zip.ZipFile; | ||
|
|
||
| class CloseWriter { | ||
|
|
||
| void test1() throws IOException { | ||
| BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\test.txt")); | ||
| bw.write("test"); | ||
| } | ||
|
|
||
| void test2() throws IOException { | ||
| OutputStream out = new FileOutputStream("test.bin"); | ||
| out.write(1); | ||
| } | ||
|
|
||
| void test3() throws IOException { | ||
| OutputStreamWriter writer = null; | ||
| try { | ||
| // OutputStreamWriter may throw an exception, in which case the ... | ||
| writer = new OutputStreamWriter( | ||
| // ... FileOutputStream is not closed by the finally block | ||
| new FileOutputStream("C:\\test.txt"), "UTF-8"); | ||
| writer.write("test"); | ||
| } | ||
| finally { | ||
| if (writer != null) | ||
| writer.close(); | ||
| } | ||
| } | ||
|
|
||
| void testCorrect1() throws IOException { | ||
| BufferedWriter bw = null; | ||
| try { | ||
| bw = new BufferedWriter(new FileWriter("C:\\test.txt")); | ||
| bw.write("test"); | ||
| } | ||
| finally { | ||
| if(bw != null) | ||
| bw.close(); // 'bw' is closed | ||
| } | ||
| } | ||
|
|
||
| void testCorrect2() throws IOException { | ||
| BufferedWriter bw = null; | ||
| try { | ||
| bw = new BufferedWriter(new FileWriter("C:\\test.txt")); | ||
| bw.write("test"); | ||
| } | ||
| finally { | ||
| cleanup(bw); // 'bw' is closed within a helper method | ||
| } | ||
| } | ||
|
|
||
| void testCorrect3() throws IOException { | ||
| FileOutputStream fos = null; | ||
| OutputStreamWriter writer = null; | ||
| try { | ||
| fos = new FileOutputStream("C:\\test.txt"); | ||
| writer = new OutputStreamWriter(fos); | ||
| writer.write("test"); | ||
| } | ||
| finally { | ||
| if (fos != null) | ||
| fos.close(); // 'fos' is closed | ||
| if (writer != null) | ||
| writer.close(); // 'writer' is closed | ||
| } | ||
| } | ||
|
|
||
| void testCorrect4() throws IOException { | ||
| BufferedWriter bw = null; | ||
| try { | ||
| bw = new BufferedWriter(new FileWriter("C:\\test.txt")); | ||
| bw.write("test"); | ||
| } | ||
| finally { | ||
| cleanup(null, bw); // 'bw' is closed within a varargs helper method, invoked with multiple args | ||
| } | ||
| } | ||
|
|
||
| void cleanup(Closeable... closeables) throws IOException { | ||
| for (Closeable c : closeables) { | ||
| if (c != null) { | ||
| c.close(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| static class LogFile { | ||
| private BufferedWriter fileWr; | ||
| LogFile(String path) { | ||
| FileWriter fw = null; | ||
| try { | ||
| fw = new FileWriter(path); | ||
| } catch (IOException e) { | ||
| System.out.println("Error: File not readable: " + path); | ||
| System.exit(1); | ||
| } | ||
| init(fw); | ||
| } | ||
| private void init(OutputStreamWriter writer) { | ||
| fileWr = new BufferedWriter(writer); | ||
| } | ||
| public void writeStuff() throws IOException { | ||
| fileWr.write("test"); | ||
| fileWr.close(); | ||
| } | ||
| } | ||
|
|
||
| // Classes which should be ignored | ||
| void testIgnore() throws IOException { | ||
| Writer w1 = new CharArrayWriter(); | ||
| w1.write("test"); | ||
|
|
||
| Writer w2 = new StringWriter(); | ||
| w2.write("test"); | ||
|
|
||
| OutputStream o1 = new ByteArrayOutputStream(); | ||
| o1.write(1); | ||
| } | ||
| } |
1 change: 1 addition & 0 deletions
1
java/ql/test/query-tests/CloseResource/CloseWriter/CloseWriter.qlref
This file contains hidden or 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 @@ | ||
| Likely Bugs/Resource Leaks/CloseWriter.ql |
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.
Uh oh!
There was an error while loading. Please reload this page.