Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[analyzer] Fix note for member reference #68691

Merged
merged 6 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 40 additions & 14 deletions clang/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,16 @@ const Expr *bugreporter::getDerefExpr(const Stmt *S) {
}
// Pattern match for a few useful cases: a[0], p->f, *p etc.
else if (const auto *ME = dyn_cast<MemberExpr>(E)) {
// This handles the case when the dereferencing of a member reference
// happens. This is needed, because the AST for dereferencing a
// member reference looks like the following:
// |-MemberExpr
// `-DeclRefExpr
// Without this special case the notes would refer to the whole object
// (struct, class or union variable) instead of just the relevant member.

if (ME->getMemberDecl()->getType()->isReferenceType())
break;
E = ME->getBase();
} else if (const auto *IvarRef = dyn_cast<ObjCIvarRefExpr>(E)) {
E = IvarRef->getBase();
Expand All @@ -157,26 +167,42 @@ const Expr *bugreporter::getDerefExpr(const Stmt *S) {
return E;
}

static const VarDecl *getVarDeclForExpression(const Expr *E) {
if (const auto *DR = dyn_cast<DeclRefExpr>(E))
return dyn_cast<VarDecl>(DR->getDecl());
return nullptr;
}

static const MemRegion *
getLocationRegionIfReference(const Expr *E, const ExplodedNode *N,
bool LookingForReference = true) {
if (const auto *DR = dyn_cast<DeclRefExpr>(E)) {
if (const auto *VD = dyn_cast<VarDecl>(DR->getDecl())) {
if (LookingForReference && !VD->getType()->isReferenceType())
return nullptr;
return N->getState()
->getLValue(VD, N->getLocationContext())
.getAsRegion();
if (const auto *ME = dyn_cast<MemberExpr>(E)) {
// This handles null references from FieldRegions, for example:
// struct Wrapper { int &ref; };
// Wrapper w = { *(int *)0 };
// w.ref = 1;
const Expr *Base = ME->getBase();
const VarDecl *VD = getVarDeclForExpression(Base);
if (!VD)
return nullptr;

const auto *FD = dyn_cast<FieldDecl>(ME->getMemberDecl());
if (!FD)
return nullptr;

if (FD->getType()->isReferenceType()) {
SVal StructSVal = N->getState()->getLValue(VD, N->getLocationContext());
return N->getState()->getLValue(FD, StructSVal).getAsRegion();
}
return nullptr;
}

// FIXME: This does not handle other kinds of null references,
// for example, references from FieldRegions:
// struct Wrapper { int &ref; };
// Wrapper w = { *(int *)0 };
// w.ref = 1;

return nullptr;
const VarDecl *VD = getVarDeclForExpression(E);
if (!VD)
return nullptr;
if (LookingForReference && !VD->getType()->isReferenceType())
return nullptr;
return N->getState()->getLValue(VD, N->getLocationContext()).getAsRegion();
}

/// Comparing internal representations of symbolic values (via
Expand Down
31 changes: 31 additions & 0 deletions clang/test/Analysis/diagnostics/deref-track-symbolic-region.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,34 @@ int testRefToNullPtr2() {
return *p2; //expected-warning {{Dereference of null pointer}}
// expected-note@-1{{Dereference of null pointer}}
}

void testMemberNullPointerDeref() {
struct Wrapper {char c; int *ptr; };
Wrapper w = {'a', nullptr}; // expected-note {{'w.ptr' initialized to a null pointer value}}
*w.ptr = 1; //expected-warning {{Dereference of null pointer}}
// expected-note@-1{{Dereference of null pointer}}
}

void testMemberNullReferenceDeref() {
struct Wrapper {char c; int &ref; };
Wrapper w = {.c = 'a', .ref = *(int *)0 }; // expected-note {{'w.ref' initialized to a null pointer value}}
// expected-warning@-1 {{binding dereferenced null pointer to reference has undefined behavior}}
w.ref = 1; //expected-warning {{Dereference of null pointer}}
// expected-note@-1{{Dereference of null pointer}}
}

void testReferenceToPointerWithNullptr() {
int *i = nullptr; // expected-note {{'i' initialized to a null pointer value}}
struct Wrapper {char c; int *&a;};
Wrapper w {'c', i}; // expected-note{{'w.a' initialized here}}
*(w.a) = 25; // expected-warning {{Dereference of null pointer}}
// expected-note@-1 {{Dereference of null pointer}}
}

void testNullReferenceToPointer() {
struct Wrapper {char c; int *&a;};
Wrapper w {'c', *(int **)0 }; // expected-note{{'w.a' initialized to a null pointer value}}
// expected-warning@-1 {{binding dereferenced null pointer to reference has undefined behavior}}
w.a = nullptr; // expected-warning {{Dereference of null pointer}}
// expected-note@-1 {{Dereference of null pointer}}
}