Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

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

Open
#21,187 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
cpp
Domain
security

Research direction

Start with the cpp/use-after-free query named in the issue and use the supplied C++ reproducer to trace the report on the chained assignment. Done means the analyzer no longer reports localPtr[0] as a use-after-free while continuing to detect genuine cases.

Written by the indexing model from the issue text.

Description

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

Dominant language
CodeQL
Stars
10.1k
Forks
2.1k
Avg merge
2d 16h
Merged PRs (30d)
143

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from github/codeql

All issues in github/codeql

Similar issues

More Security issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.