Path: blob/main/contrib/llvm-project/clang/lib/StaticAnalyzer/Checkers/FixedAddressChecker.cpp
35266 views
//=== FixedAddressChecker.cpp - Fixed address usage checker ----*- C++ -*--===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This files defines FixedAddressChecker, a builtin checker that checks for9// assignment of a fixed address to a pointer.10// This check corresponds to CWE-587.11//12//===----------------------------------------------------------------------===//1314#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"15#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"16#include "clang/StaticAnalyzer/Core/Checker.h"17#include "clang/StaticAnalyzer/Core/CheckerManager.h"18#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"1920using namespace clang;21using namespace ento;2223namespace {24class FixedAddressChecker25: public Checker< check::PreStmt<BinaryOperator> > {26const BugType BT{this, "Use fixed address"};2728public:29void checkPreStmt(const BinaryOperator *B, CheckerContext &C) const;30};31}3233void FixedAddressChecker::checkPreStmt(const BinaryOperator *B,34CheckerContext &C) const {35// Using a fixed address is not portable because that address will probably36// not be valid in all environments or platforms.3738if (B->getOpcode() != BO_Assign)39return;4041QualType T = B->getType();42if (!T->isPointerType())43return;4445SVal RV = C.getSVal(B->getRHS());4647if (!RV.isConstant() || RV.isZeroConstant())48return;4950if (ExplodedNode *N = C.generateNonFatalErrorNode()) {51// FIXME: improve grammar in the following strings:52constexpr llvm::StringLiteral Msg =53"Using a fixed address is not portable because that address will "54"probably not be valid in all environments or platforms.";55auto R = std::make_unique<PathSensitiveBugReport>(BT, Msg, N);56R->addRange(B->getRHS()->getSourceRange());57C.emitReport(std::move(R));58}59}6061void ento::registerFixedAddressChecker(CheckerManager &mgr) {62mgr.registerChecker<FixedAddressChecker>();63}6465bool ento::shouldRegisterFixedAddressChecker(const CheckerManager &mgr) {66return true;67}686970