Hacktoberfest 2026:維護者為十月標記出來的 issue,仍然開放、適合新手。 瀏覽 Hacktoberfest issue

False Positive: cpp/use-after-free on chained assignment after delete

未關閉
#21,187 1 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

評估

難度
4/5
預估耗時
3-5 天
新手友好度
48/100
Issue 類型
缺陷
描述清晰度
基本清楚
活躍度
停滯
技術堆疊
cpp
領域
security

研究方向

從 issue 中提到的 cpp/use-after-free 查詢開始,並使用提供的 C++ 重現程式追蹤鏈式指派上的報告。完成標準是分析器不再將 localPtr[0] 報告為 use-after-free,同時繼續偵測真正的案例。

由索引模型根據 Issue 內容生成。

描述

false-positive

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

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

github/codeql 的其他 Issue

查看 github/codeql 的全部 Issue

相似的 Issue

更多 Security Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。