False Positive: cpp/use-after-free on chained assignment after delete
还没有人认领这个 Issue。
评估
调研方向
从 issue 中提到的 cpp/use-after-free 查询开始,并使用提供的 C++ 复现程序跟踪链式赋值上的报告。完成标准是分析器不再将 localPtr[0] 报告为 use-after-free,同时继续检测真正的案例。
由索引模型根据 Issue 内容生成。
描述
Description of the false positive
I have encountered a false positive with the rule cpp/use-after-free.
CodeQL incorrectly flags a variable as being "used after free" when it is assigned via a chained assignment immediately following a delete[].
The analyzer seems to propagate the "freed" state of the dereferenced pointer to the local variable, failing to recognize that the new operator in the right-hand side of the assignment refreshes the pointer before the local variable reads it.
Code samples or links to source code
#include <new>
void reallocateBuffer(char** sharedPtr, int size) {
// 1. Memory is freed
delete[] *sharedPtr;
char* localPtr;
// 2. Chained assignment:
// C++ guarantees right-to-left associativity.
// 'new' happens first, updates '*sharedPtr', and THEN 'localPtr' takes that value.
localPtr = *sharedPtr = new char[size];
// 3. CodeQL flags 'localPtr' as Use-After-Free here
if (localPtr) {
localPtr[0] = 'A';
}
}
int main() {
char* data = new char[10];
reallocateBuffer(&data, 50);
delete[] data;
return 0;
}
Expected Behavior
CodeQL should recognize that localPtr is assigned the result of the new allocation (via *sharedPtr) and is therefore safe to use.
Actual Behavior
CodeQL reports cpp/use-after-free on the line localPtr[0] = 'A';, claiming localPtr points to memory that was freed by delete[] *sharedPtr.
Query / Rule ID
cpp/use-after-free
- 主要语言
- 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 ·