[clang] [Webkit Checkers] Introduce a Webkit checker for memory unsafe casts (PR #114606)

Ryosuke Niwa via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 21 18:38:23 PST 2024


================
@@ -0,0 +1,185 @@
+//=======- MemoryUnsafeCastChecker.cpp -------------------------*- C++ -*-==//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file defines MemoryUnsafeCast checker, which checks for casts from a
+// base type to a derived type.
+//===----------------------------------------------------------------------===//
+
+#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
+#include "clang/StaticAnalyzer/Core/Checker.h"
+#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
+
+using namespace clang;
+using namespace ento;
+using namespace ast_matchers;
+
+namespace {
+static constexpr const char *const BaseNode = "BaseNode";
+static constexpr const char *const DerivedNode = "DerivedNode";
+static constexpr const char *const FromCastNode = "FromCast";
+static constexpr const char *const ToCastNode = "ToCast";
+static constexpr const char *const WarnRecordDecl = "WarnRecordDecl";
+
+class MemoryUnsafeCastChecker : public Checker<check::ASTCodeBody> {
+  BugType BT{this, "Unsafe cast", "WebKit coding guidelines"};
+public:
+  void checkASTCodeBody(const Decl *D, AnalysisManager& Mgr,
+                        BugReporter &BR) const;
+};
+} // end namespace
+
+static void emitReport(AnalysisDeclContext *ADC, BugReporter &BR,
+                       const MemoryUnsafeCastChecker *Checker,
+                       std::string &Diagnostics, const CastExpr *CE) {
+  BR.EmitBasicReport(
+      ADC->getDecl(), Checker,
+      /*Name=*/"Unsafe type cast", categories::SecurityError, Diagnostics,
----------------
rniwa wrote:

This lists the warnings for this kind under "Security error" instead of "WebKit coding guidelines" in the results page.

https://github.com/llvm/llvm-project/pull/114606


More information about the cfe-commits mailing list