clang 22.0.0git
BoolAssignmentChecker.cpp
Go to the documentation of this file.
1//== BoolAssignmentChecker.cpp - Boolean assignment checker -----*- C++ -*--==//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This defines BoolAssignmentChecker, a builtin check in ExprEngine that
10// performs checks for assignment of non-Boolean values to Boolean variables.
11//
12//===----------------------------------------------------------------------===//
13
20#include <optional>
21
22using namespace clang;
23using namespace ento;
24
25namespace {
26class BoolAssignmentChecker : public Checker<check::Bind> {
27 const BugType BT{this, "Assignment of a non-Boolean value"};
28 void emitReport(ProgramStateRef State, CheckerContext &C,
29 bool IsTainted = false) const;
30
31public:
32 void checkBind(SVal Loc, SVal Val, const Stmt *S, bool AtDeclInit,
33 CheckerContext &C) const;
34};
35} // end anonymous namespace
36
37void BoolAssignmentChecker::emitReport(ProgramStateRef State, CheckerContext &C,
38 bool IsTainted) const {
39 if (ExplodedNode *N = C.generateNonFatalErrorNode(State)) {
40 StringRef Msg = IsTainted ? "Might assign a tainted non-Boolean value"
41 : "Assignment of a non-Boolean value";
42 C.emitReport(std::make_unique<PathSensitiveBugReport>(BT, Msg, N));
43 }
44}
45
46static bool isBooleanType(QualType Ty) {
47 if (Ty->isBooleanType()) // C++ or C99
48 return true;
49
50 if (const TypedefType *TT = Ty->getAs<TypedefType>())
51 return TT->getDecl()->getName() == "BOOL" || // Objective-C
52 TT->getDecl()->getName() == "_Bool" || // stdbool.h < C99
53 TT->getDecl()->getName() == "Boolean"; // MacTypes.h
54
55 return false;
56}
57
58void BoolAssignmentChecker::checkBind(SVal Loc, SVal Val, const Stmt *S,
59 bool AtDeclInit,
60 CheckerContext &C) const {
61
62 // We are only interested in stores into Booleans.
63 const TypedValueRegion *TR =
64 dyn_cast_or_null<TypedValueRegion>(Loc.getAsRegion());
65
66 if (!TR)
67 return;
68
69 QualType RegTy = TR->getValueType();
70
71 if (!isBooleanType(RegTy))
72 return;
73
74 // Get the value of the right-hand side. We only care about values
75 // that are defined (UnknownVals and UndefinedVals are handled by other
76 // checkers).
77 std::optional<NonLoc> NV = Val.getAs<NonLoc>();
78 if (!NV)
79 return;
80
81 // Check if the assigned value meets our criteria for correctness. It must
82 // be a value that is either 0 or 1. One way to check this is to see if
83 // the value is possibly < 0 (for a negative value) or greater than 1.
84 ProgramStateRef State = C.getState();
85 BasicValueFactory &BVF = C.getSValBuilder().getBasicValueFactory();
86 ConstraintManager &CM = C.getConstraintManager();
87
88 llvm::APSInt Zero = BVF.getValue(0, RegTy);
89 llvm::APSInt One = BVF.getValue(1, RegTy);
90
91 ProgramStateRef StIn, StOut;
92 std::tie(StIn, StOut) = CM.assumeInclusiveRangeDual(State, *NV, Zero, One);
93
94 if (!StIn)
95 emitReport(StOut, C);
96 if (StIn && StOut && taint::isTainted(State, *NV))
97 emitReport(StOut, C, /*IsTainted=*/true);
98}
99
100void ento::registerBoolAssignmentChecker(CheckerManager &Mgr) {
101 Mgr.registerChecker<BoolAssignmentChecker>();
102}
103
104bool ento::shouldRegisterBoolAssignmentChecker(const CheckerManager &Mgr) {
105 return true;
106}
static bool isBooleanType(QualType Ty)
A (possibly-)qualified type.
Definition: TypeBase.h:937
Stmt - This represents one statement.
Definition: Stmt.h:85
bool isBooleanType() const
Definition: TypeBase.h:9066
const T * getAs() const
Member-template getAs<specific type>'.
Definition: TypeBase.h:9159
CHECKER * registerChecker(AT &&...Args)
Register a single-part checker (derived from Checker): construct its singleton instance,...
Simple checker classes that implement one frontend (i.e.
Definition: Checker.h:553
ProgramStatePair assumeInclusiveRangeDual(ProgramStateRef State, NonLoc Value, const llvm::APSInt &From, const llvm::APSInt &To)
Returns a pair of states (StInRange, StOutOfRange) where the given value is assumed to be in the rang...
SVal - This represents a symbolic expression, which can be either an L-value or an R-value.
Definition: SVals.h:56
std::optional< T > getAs() const
Convert to the specified SVal type, returning std::nullopt if this SVal is not of the desired type.
Definition: SVals.h:87
const MemRegion * getAsRegion() const
Definition: SVals.cpp:119
TypedValueRegion - An abstract class representing regions having a typed value.
Definition: MemRegion.h:563
virtual QualType getValueType() const =0
bool isTainted(ProgramStateRef State, const Stmt *S, const LocationContext *LCtx, TaintTagType Kind=TaintTagGeneric)
Check if the statement has a tainted value in the given state.
Definition: Taint.cpp:148
The JSON file list parser is used to communicate input to InstallAPI.