Repository navigation
Added resource leak transform and general codemod #339
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
Changes from all commits
d0e2039
88687fe
b0f89b6
cc091ff
7fbc386
926c8d8
845a2ca
580e563
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will be an extremely expensive codemod to execute as it will force every file to be visited, and every I think we need a Semgrep pattern to detect reasonable starting points that can be confirmed more thoroughly by AST-inspecting code.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a guess, but I think it's not that much to test. Again, maybe we should add it to another ticket, and make sure we check that ticket off before we add to
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only reason it visits |
||
| List<CodemodChange> changes = | ||
| cu.findAll(Expression.class).stream() | ||
| .flatMap(expr -> onNodeFound(expr).stream()) | ||
| .collect(Collectors.toList()); | ||
| return CodemodFileScanningResult.withOnlyChanges(changes); | ||
| } | ||
| } | ||
| 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()); | ||
| + } | ||
| ``` |
| 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" | ||
| ] | ||
| } |
| 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 {} |
Uh oh!
There was an error while loading. Please reload this page.