Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

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

Đang mở
#21,187 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
48/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
cpp
Lĩnh vực
security

Hướng nghiên cứu

Bắt đầu với truy vấn cpp/use-after-free được nêu trong issue và sử dụng trình tái hiện C++ được cung cấp để theo dõi báo cáo trên phép gán chuỗi. Hoàn thành nghĩa là analyzer không còn báo cáo localPtr[0] là use-after-free mà vẫn tiếp tục phát hiện các trường hợp thực sự.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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

Ngôn ngữ chính
CodeQL
Star
10.1k
Fork
2.1k
Merge trung bình
2 ngày 16 giờ
Pull request đã merge (30 ngày)
143

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của github/codeql

Tất cả issue của github/codeql

Issue tương tự

Thêm issue về Security

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.