Skip to content

Clang Static Analyzer (MallocChecker) misses use-after-free via field address (e.g. &ptr->field) #152446

@LoboQ1ng

Description

@LoboQ1ng

Clang Static Analyzer's MallocChecker currently fails to detect use-after-free when the memory is accessed via the address of a field inside a freed structure.

For example, the following use-after-free goes undetected:

#include <stdlib.h>

struct Obj {
  int field;
};

void use(void *);

void test() {
  struct Obj *o = malloc(sizeof(struct Obj));
  free(o);
  use(&o->field); // ⚠️ No warning reported by CSA (MallocChecker)
}

In this example, the heap memory pointed to by o has been freed, yet &o->field is passed to a function. This is a classic use-after-free bug, but MallocChecker does not currently report it.
Root Cause
In MallocChecker.cpp, argument checking currently relies on:

SymbolRef Sym = ArgSVal.getAsSymbol();

This fails for field address expressions like &ptr->field, because such expressions produce a MemRegionVal, and getAsSymbol() does not extract the base symbol in those cases.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions