Hacktoberfest 2026: as issues que os mantenedores marcaram para outubro, abertas e boas para iniciantes. Ver issues do Hacktoberfest

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

Aberta
#21,187 1 comentário 0 reações 0 responsáveis Ver no GitHub

Ninguém assumiu esta issue ainda.

Avaliação

Dificuldade
4/5
Tempo estimado
3-5 dias
Facilidade para iniciantes
48/100
Tipo de issue
Bug
Clareza
Razoavelmente clara
Status de atividade
Estagnada
Stack de tecnologia
cpp
Domínio
security

Direção de pesquisa

Comece com a consulta cpp/use-after-free mencionada na issue e use o reprodutor C++ fornecido para rastrear o relatório na atribuição encadeada. A tarefa estará concluída quando o analisador não relatar mais localPtr[0] como use-after-free e continuar detectando casos genuínos.

Escrita pelo modelo de indexação a partir do texto da issue.

Descrição

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

Linguagem predominante
CodeQL
Estrelas
10.1k
Forks
2.1k
Merge médio
2d 16h
PRs com merge (30d)
143

Guia de contribuição

Abrir o guia de contribuição

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de github/codeql

Todas as issues de github/codeql

Issues semelhantes

Mais issues de Security

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.