================
@@ -0,0 +1,95 @@
+#include "LifetimeModeling.h"
+#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
+#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
+#include "clang/StaticAnalyzer/Core/BugReporter/BugReporterVisitors.h"
+#include "clang/StaticAnalyzer/Core/Checker.h"
+#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
+
+using namespace clang;
+using namespace ento;
+
+namespace {
+class ReportDanglingPtrDeref : public Checker<check::Location> {
+public:
+  void checkLocation(SVal Loc, bool IsLoad, const Stmt *S,
+                     CheckerContext &C) const;
+  void reportUseAfterScope(const MemRegion *Region, ExplodedNode *N,
+                           CheckerContext &C) const;
+  const BugType BugMsg{this, "ReportDanglingPtrDeref", "LifetimeBound"};
+};
+
+class ReportDanglingPtrDerefBRVisitor : public BugReporterVisitor {
+  const MemRegion *SourceRegion;
+
+public:
+  ReportDanglingPtrDerefBRVisitor(const MemRegion *Source)
+      : SourceRegion(Source) {}
+
+  void Profile(llvm::FoldingSetNodeID &ID) const override {
+    ID.AddPointer(SourceRegion);
+  }
+
+  PathDiagnosticPieceRef VisitNode(const ExplodedNode *N,
+                                   BugReporterContext &BRC,
+                                   PathSensitiveBugReport &BR) override;
+};
+
+} // namespace
+
+void ReportDanglingPtrDeref::checkLocation(SVal Loc, bool IsLoad, const Stmt 
*S,
+                                           CheckerContext &C) const {
+  ProgramStateRef State = C.getState();
+
+  if (const MemRegion *LocRegion = Loc.getAsRegion()) {
+    if (lifetime_modeling::isDeallocated(State, LocRegion)) {
----------------
benedekaibas wrote:

> Something I am wondering about, would we end up reporting all dereferences of 
> the same dangling pointer? I wonder if we should silence the warnings after 
> the first report (e.g., remove once a deallocated pointer is reported we 
> could maybe put it in an already reported set and do not report again). But 
> this could be a follow-up PR.

I was thinking about something similar. I propose that I do a follow up PR that 
includes the other changes from @steakhal's patch and also add this feature in 
that PR? I did not implement everything from 
https://github.com/llvm/llvm-project/pull/206460#issuecomment-4935983232 - I 
have only included the changes in that patch that are aligning with the current 
scope of the `ReportDanglingPtrDeref` checker.

https://github.com/llvm/llvm-project/pull/209278
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to