21 #include "llvm/ADT/SmallString.h" 22 #include "llvm/Support/raw_ostream.h" 24 using namespace clang;
28 class UndefResultChecker
29 :
public Checker< check::PostStmt<BinaryOperator> > {
31 mutable std::unique_ptr<BugType> BT;
42 if (!isa<ArraySubscriptExpr>(Ex))
45 SVal Loc = state->getSVal(Ex, LCtx);
57 ProgramStateRef StInBound = state->assumeInBound(Idx, NumElements,
true);
58 ProgramStateRef StOutBound = state->assumeInBound(Idx, NumElements,
false);
59 return StOutBound && !StInBound;
71 if (state->getSVal(B, LCtx).isUndef()) {
88 new BuiltinBug(
this,
"Result of operation is garbage or undefined"));
91 llvm::raw_svector_ostream OS(sbuf);
92 const Expr *Ex =
nullptr;
95 if (state->getSVal(B->
getLHS(), LCtx).isUndef()) {
99 else if (state->getSVal(B->
getRHS(), LCtx).isUndef()) {
105 OS <<
"The " << (isLeft ?
"left" :
"right") <<
" operand of '" 107 <<
"' is a garbage value";
109 OS <<
" due to array index out of bounds";
112 if ((B->
getOpcode() == BinaryOperatorKind::BO_Shl ||
113 B->
getOpcode() == BinaryOperatorKind::BO_Shr) &&
115 OS <<
"The result of the " 116 << ((B->
getOpcode() == BinaryOperatorKind::BO_Shl) ?
"left" 118 <<
" shift is undefined because the right operand is negative";
119 }
else if ((B->
getOpcode() == BinaryOperatorKind::BO_Shl ||
120 B->
getOpcode() == BinaryOperatorKind::BO_Shr) &&
123 OS <<
"The result of the " 124 << ((B->
getOpcode() == BinaryOperatorKind::BO_Shl) ?
"left" 126 <<
" shift is undefined due to shifting by ";
129 const llvm::APSInt *I =
132 OS <<
"a value that is";
133 else if (I->isUnsigned())
134 OS <<
'\'' << I->getZExtValue() <<
"\', which is";
136 OS <<
'\'' << I->getSExtValue() <<
"\', which is";
138 OS <<
" greater or equal to the width of type '" 140 }
else if (B->
getOpcode() == BinaryOperatorKind::BO_Shl &&
142 OS <<
"The result of the left shift is undefined because the left " 143 "operand is negative";
145 OS <<
"The result of the '" 147 <<
"' expression is undefined";
150 auto report = llvm::make_unique<BugReport>(*BT, OS.str(), N);
virtual DefinedOrUnknownSVal getSizeInElements(ProgramStateRef state, const MemRegion *region, QualType EleTy)
An instance of this class is created to represent a function declaration or definition.
MemRegion - The root abstract class for all memory regions.
ExplodedNode * generateErrorNode(ProgramStateRef State=nullptr, const ProgramPointTag *Tag=nullptr)
Generate a transition to a node that will be used to report an error.
SVal getSVal(const Stmt *S) const
Get the value of arbitrary expressions at this point in the path.
const MemRegion * getSuperRegion() const
StringRef getCalleeName(const FunctionDecl *FunDecl) const
Get the name of the called function (path-sensitive).
bool isGreaterOrEqual(const Expr *E, unsigned long long Val)
Returns true if the value of E is greater than or equal to Val under unsigned comparison.
i32 captured_struct **param SharedsTy A type which contains references the shared variables *param Shareds Context with the list of shared variables from the p *TaskFunction *param Data Additional data for task generation like final * state
static bool isShiftOverflow(const BinaryOperator *B, CheckerContext &C)
StringRef getOpcodeStr() const
static bool isArrayIndexOutOfBounds(CheckerContext &C, const Expr *Ex)
A builtin binary operation expression such as "x + y" or "x <= y".
Expr * IgnoreParenCasts() LLVM_READONLY
IgnoreParenCasts - Ignore parentheses and casts.
Expr - This represents one expression.
void emitReport(std::unique_ptr< BugReport > R)
Emit the diagnostics report.
CHECKER * registerChecker()
Used to register checkers.
StoreManager & getStoreManager()
SVal - This represents a symbolic expression, which can be either an L-value or an R-value...
Dataflow Directional Tag Classes.
ASTContext & getASTContext()
static std::string getAsString(SplitQualType split, const PrintingPolicy &Policy)
const Decl * getDecl() const
unsigned getIntWidth(QualType T) const
const StackFrameContext * getStackFrame() const
T castAs() const
Convert to the specified SVal type, asserting that this SVal is of the desired type.
const ProgramStateRef & getState() const
bool isNegative(const Expr *E)
Returns true if the value of E is negative.
bool trackNullOrUndefValue(const ExplodedNode *N, const Stmt *S, BugReport &R, bool IsArg=false, bool EnableNullFPSuppression=true)
Attempts to add visitors to trace a null or undefined value back to its point of origin, whether it is a symbol constrained to null or an explicit assignment.
QualType getValueType() const override
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
SValBuilder & getSValBuilder()
ElementRegin is used to represent both array elements and casts.
virtual const llvm::APSInt * getKnownValue(ProgramStateRef state, SVal val)=0
Evaluates a given SVal.
const LocationContext * getLocationContext() const