-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added resource leak transform and general codemod (#339)
Added a new resource leak transform and general codemod - Removed CodeQL dependency for ResourceLeakFixer and left it available as a general transform. - Added new resource leak codemod independent of CodeQL. This may conflict with the resource leak codemods if both are available, producing more changes than necessary. - Added ResourceLeakFixer transform to PreventFileWriterLeakWithFilesCodemod. Closes #304. You may notice the changes to the PreventFileWriterLeakWithFilesCodemod test files. This is due to a newly discovered issue. Refer to #338.
- Loading branch information
1 parent
8e979de
commit 7620608
Showing
12 changed files
with
290 additions
and
70 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
37 changes: 37 additions & 0 deletions
37
core-codemods/src/main/java/io/codemodder/codemods/ResourceLeakCodemod.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,37 @@ | ||
package io.codemodder.codemods; | ||
|
||
import com.github.javaparser.ast.CompilationUnit; | ||
import com.github.javaparser.ast.expr.Expression; | ||
import io.codemodder.*; | ||
import io.codemodder.javaparser.JavaParserChanger; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
/** A codemod that wraps AutoCloseable objects whenever possible. */ | ||
@Codemod( | ||
id = "pixee:java/resource-leak", | ||
reviewGuidance = ReviewGuidance.MERGE_WITHOUT_REVIEW, | ||
importance = Importance.MEDIUM, | ||
executionPriority = CodemodExecutionPriority.LOW) | ||
public final class ResourceLeakCodemod extends JavaParserChanger { | ||
|
||
private Optional<CodemodChange> onNodeFound(final Expression expr) { | ||
int originalLine = expr.getBegin().get().line; | ||
if (ResourceLeakFixer.checkAndFix(expr).isPresent()) { | ||
return Optional.of(CodemodChange.from(originalLine)); | ||
} else { | ||
return Optional.empty(); | ||
} | ||
} | ||
|
||
@Override | ||
public CodemodFileScanningResult visit( | ||
final CodemodInvocationContext context, final CompilationUnit cu) { | ||
List<CodemodChange> changes = | ||
cu.findAll(Expression.class).stream() | ||
.flatMap(expr -> onNodeFound(expr).stream()) | ||
.collect(Collectors.toList()); | ||
return CodemodFileScanningResult.withOnlyChanges(changes); | ||
} | ||
} |
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
11 changes: 11 additions & 0 deletions
11
...ds/src/main/resources/io/codemodder/codemods/ResourceLeakCodemod/description.md
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,11 @@ | ||
This change adds [try-with-resources](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html) to code to prevent resources from being leaked, which could lead to denial-of-service conditions like connection pool or file handle exhaustion. | ||
|
||
Our changes look something like this: | ||
|
||
```diff | ||
- BufferedReader br = new BufferedReader(new FileReader("C:\\test.txt")); | ||
- System.out.println(br.readLine()); | ||
+ try(FileReader input = new FileReader("C:\\test.txt"); BufferedReader br = new BufferedReader(input)){ | ||
+ System.out.println(br.readLine()); | ||
+ } | ||
``` |
9 changes: 9 additions & 0 deletions
9
core-codemods/src/main/resources/io/codemodder/codemods/ResourceLeakCodemod/report.json
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,9 @@ | ||
{ | ||
"summary" : "Prevent resource leaks", | ||
"change": "Added a try-with-resources statement to automatically close resources", | ||
"reviewGuidanceIJustification" : "This codemod causes resources to be cleaned up immediately after use instead of at garbage collection time, and we don't believe this change entails any risk.", | ||
"references" : [ | ||
"https://cwe.mitre.org/data/definitions/404.html", | ||
"https://cwe.mitre.org/data/definitions/772.html" | ||
] | ||
} |
10 changes: 10 additions & 0 deletions
10
core-codemods/src/test/java/io/codemodder/codemods/ResourceLeakCodemodTest.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,10 @@ | ||
package io.codemodder.codemods; | ||
|
||
import io.codemodder.testutils.CodemodTestMixin; | ||
import io.codemodder.testutils.Metadata; | ||
|
||
@Metadata( | ||
codemodType = ResourceLeakCodemod.class, | ||
testResourceDir = "resource-leak", | ||
dependencies = {}) | ||
final class ResourceLeakCodemodTest implements CodemodTestMixin {} |
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
Oops, something went wrong.