[clang] [clang][analyzer] Check for label location bindings in `DereferenceChecker` (PR #91119)
Rajveer Singh Bharadwaj via cfe-commits
cfe-commits at lists.llvm.org
Tue May 7 06:49:30 PDT 2024
https://github.com/Rajveer100 updated https://github.com/llvm/llvm-project/pull/91119
>From 78a2afab67eef9a8a05ced89df0aadb56a2ec2b8 Mon Sep 17 00:00:00 2001
From: Rajveer <rajveer.developer at icloud.com>
Date: Sun, 5 May 2024 18:05:00 +0530
Subject: [PATCH] [clang][analyzer] Check for label location bindings in
`DereferenceChecker`
Resolves #89264
---
.../Checkers/DereferenceChecker.cpp | 15 ++++++++++++++-
clang/test/Analysis/gh-issue-89185.c | 7 +++----
2 files changed, 17 insertions(+), 5 deletions(-)
diff --git a/clang/lib/StaticAnalyzer/Checkers/DereferenceChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/DereferenceChecker.cpp
index 1cebfbbee77da..0355eede75eae 100644
--- a/clang/lib/StaticAnalyzer/Checkers/DereferenceChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/DereferenceChecker.cpp
@@ -31,11 +31,13 @@ class DereferenceChecker
: public Checker< check::Location,
check::Bind,
EventDispatcher<ImplicitNullDerefEvent> > {
- enum DerefKind { NullPointer, UndefinedPointerValue };
+ enum DerefKind { NullPointer, UndefinedPointerValue, AddressOfLabel };
BugType BT_Null{this, "Dereference of null pointer", categories::LogicError};
BugType BT_Undef{this, "Dereference of undefined pointer value",
categories::LogicError};
+ BugType BT_Label{this, "Dereference of the address of a label",
+ categories::LogicError};
void reportBug(DerefKind K, ProgramStateRef State, const Stmt *S,
CheckerContext &C) const;
@@ -167,6 +169,11 @@ void DereferenceChecker::reportBug(DerefKind K, ProgramStateRef State,
DerefStr1 = " results in an undefined pointer dereference";
DerefStr2 = " results in a dereference of an undefined pointer value";
break;
+ case DerefKind::AddressOfLabel:
+ BT = &BT_Label;
+ DerefStr1 = " results in an undefined pointer dereference";
+ DerefStr2 = " results in a dereference of an address of a label";
+ break;
};
// Generate an error node.
@@ -287,6 +294,12 @@ void DereferenceChecker::checkBind(SVal L, SVal V, const Stmt *S,
if (V.isUndef())
return;
+ // One should never write to label addresses.
+ if (auto Label = L.getAs<loc::GotoLabel>()) {
+ reportBug(DerefKind::AddressOfLabel, C.getState(), S, C);
+ return;
+ }
+
const MemRegion *MR = L.getAsRegion();
const TypedValueRegion *TVR = dyn_cast_or_null<TypedValueRegion>(MR);
if (!TVR)
diff --git a/clang/test/Analysis/gh-issue-89185.c b/clang/test/Analysis/gh-issue-89185.c
index 8a907f198a5fd..27456e7efe885 100644
--- a/clang/test/Analysis/gh-issue-89185.c
+++ b/clang/test/Analysis/gh-issue-89185.c
@@ -7,8 +7,7 @@ void clang_analyzer_dump_ptr(char*);
void binding_to_label_loc() {
char *b = &&MyLabel;
MyLabel:
- *b = 0; // no-crash
- clang_analyzer_dump_ptr(b); // expected-warning {{&&MyLabel}}
- clang_analyzer_dump(*b); // expected-warning {{Unknown}}
- // FIXME: We should never reach here, as storing to a label is invalid.
+ *b = 0; // expected-warning {{Dereference of the address of a label}}
+ clang_analyzer_dump_ptr(b);
+ clang_analyzer_dump(*b);
}
More information about the cfe-commits
mailing list