False Positive: CloseSql.ql reports a ResultSet that is already scoped by try-with-resources on its parent Statement.
还没有人认领这个 Issue。
评估
调研方向
首先阅读 Likely Bugs/Resource Leaks/CloseSql.ql 和 NegCase7_Var3.java 示例。针对使用别名的 Statement 重现该报告,并检查父资源的所有权是如何跟踪的。当此案例中的 ResultSet 不再被报告,同时真正未关闭的 SQL 资源仍然受到覆盖时,即表示完成。
由索引模型根据 Issue 内容生成。
描述
Version
codeql 2.24.3
Checker
- Checker id:
Likely Bugs/Resource Leaks/CloseSql.ql - Checker description: This checker detects SQL resource objects (Connection, Statement, ResultSet) that are initialized locally and not guaranteed to be closed on method exit.
Description of the false positive
This case should stay out of the result set. The ResultSet is created from a Statement that is itself managed by try-with-resources, together with the owning Connection. Once the try block exits, both parent resources are closed, and the ResultSet lifetime is covered by that ownership chain.
The alias on the Statement does not change that. It is still the same tracked resource.
Affected test cases
NegCase7_Var3.java
stmtAlias is just another reference to originalStmt. The query appears to lose that alias relationship and treats rs as if it were detached from the try-with-resources scope.
// A locally initialized SQL ResultSet obtained via a method call on a Statement variable, where Statement is a transitive parent closeable resource, should not be flagged as unclosed.
package scensct.var.neg;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
public class NegCase7_Var3 {
public void test() throws Exception {
try (Connection conn = DriverManager.getConnection("jdbc:test");
Statement originalStmt = conn.createStatement()) {
// Alias the Statement
Statement stmtAlias = originalStmt;
// Always-true condition
if (conn != null) {
// Locally initialized ResultSet via method call on aliased Statement
ResultSet rs = stmtAlias.executeQuery("SELECT 1"); // [REPORTED LINE]
}
}
}
}
Cause analysis
Likely Bugs/Resource Leaks/CloseSql.ql is supposed to report SQL resources that are not guaranteed to be closed. That is not what this sample is doing.
The only reason this gets reported is that the Statement is referenced through stmtAlias instead of its original local name. If the query stops recognizing parent-resource ownership after a trivial alias, it will produce noise in ordinary JDBC code where objects are routinely passed through locals before use.
This is an ownership-tracking false positive, not a real leak.
- 主要语言
- CodeQL
- 星标
- 10.1k
- 派生
- 2.1k
- 平均合并
- 2 天 16 小时
- 30 天内合并 PR
- 143
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
github/codeql 的其他 Issue
-
agentic-workflows
难度 2/5 1-3 小时 新手友好度 70/100
-
false-positive javascript
难度 2/5 1-3 小时 新手友好度 84/100
-
难度 2/5 1-3 小时 新手友好度 82/100
-
难度 2/5 1-3 小时 新手友好度 78/100
-
false-positive
难度 2/5 1-3 小时 新手友好度 70/100
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 78/100
punkpeye/mcp-remote#369 ·
-
Mend: dependency security vulnerability untriaged
难度 1/5 1 小时以内 新手友好度 86/100
-
难度 2/5 1-3 小时 新手友好度 68/100
-
bug
难度 1/5 1 小时以内 新手友好度 90/100
cisagov/vulnrichment#337 ·
-
bug DUP Reservations
难度 2/5 1-3 小时 新手友好度 68/100
bcgov/reserve-rec-public#896 ·