26 #include "llvm/ADT/SmallString.h" 27 #include "llvm/Support/raw_ostream.h" 29 using namespace clang;
39 bool sameDecl(
const Expr *A1,
const Expr *A2) {
42 return D1->
getDecl() == D2->getDecl();
47 bool isSizeof(
const Expr *E,
const Expr *WithArg) {
48 if (
const auto *UE = dyn_cast<UnaryExprOrTypeTraitExpr>(E))
49 if (UE->getKind() ==
UETT_SizeOf && !UE->isArgumentType())
50 return sameDecl(UE->getArgumentExpr(), WithArg);
55 bool isStrlen(
const Expr *E,
const Expr *WithArg) {
56 if (
const auto *CE = dyn_cast<CallExpr>(E)) {
61 sameDecl(CE->getArg(0), WithArg));
67 bool isOne(
const Expr *E) {
68 if (
const auto *IL = dyn_cast<IntegerLiteral>(E))
69 return (IL->getValue().isIntN(1));
73 StringRef getPrintableName(
const Expr *E) {
81 bool containsBadStrncatPattern(
const CallExpr *CE);
93 bool containsBadStrlcpyPattern(
const CallExpr *CE);
97 : Checker(Checker), BR(BR), AC(AC) {}
100 void VisitChildren(
Stmt *S);
101 void VisitStmt(
Stmt *S) {
114 bool WalkAST::containsBadStrncatPattern(
const CallExpr *CE) {
122 if (
const auto *BE = dyn_cast<BinaryOperator>(LenArg->
IgnoreParenCasts())) {
124 if (BE->getOpcode() == BO_Sub) {
125 const Expr *L = BE->getLHS();
126 const Expr *R = BE->getRHS();
127 if (isSizeof(L, DstArg) && isStrlen(R, DstArg))
136 if (isSizeof(LenArg, DstArg))
140 if (isSizeof(LenArg, SrcArg))
145 bool WalkAST::containsBadStrlcpyPattern(
const CallExpr *CE) {
156 const auto *LenArgVal = dyn_cast<
VarDecl>(LenArgDecl->getDecl());
157 if (LenArgVal->getInit())
158 LenArg = LenArgVal->getInit();
165 uint64_t ILRawVal = IL->getValue().getZExtValue();
172 DstArgDecl = dyn_cast<
DeclRefExpr>(BE->getLHS()->IgnoreParenImpCasts());
173 if (BE->getOpcode() == BO_Add) {
174 if ((IL = dyn_cast<IntegerLiteral>(BE->getRHS()->IgnoreParenImpCasts()))) {
175 DstOff = IL->getValue().getZExtValue();
181 if (
const auto *Buffer = dyn_cast<ConstantArrayType>(DstArgDecl->getType())) {
184 if ((BufferLen - DstOff) < ILRawVal)
193 void WalkAST::VisitCallExpr(
CallExpr *CE) {
199 if (containsBadStrncatPattern(CE)) {
205 StringRef DstName = getPrintableName(DstArg);
208 llvm::raw_svector_ostream os(S);
209 os <<
"Potential buffer overflow. ";
210 if (!DstName.empty()) {
211 os <<
"Replace with 'sizeof(" << DstName <<
") " 212 "- strlen(" << DstName <<
") - 1'";
216 os <<
"se a safer 'strlcat' API";
219 "C String API", os.str(), Loc,
223 if (containsBadStrlcpyPattern(CE)) {
229 StringRef DstName = getPrintableName(DstArg);
232 llvm::raw_svector_ostream os(S);
233 os <<
"The third argument is larger than the size of the input buffer. ";
234 if (!DstName.empty())
235 os <<
"Replace with the value 'sizeof(" << DstName <<
")` or lower";
238 "C String API", os.str(), Loc,
247 void WalkAST::VisitChildren(
Stmt *S) {
254 class CStringSyntaxChecker:
public Checker<check::ASTCodeBody> {
Represents a function declaration or definition.
Expr * getArg(unsigned Arg)
getArg - Return the specified argument.
Defines enumerations for the type traits support.
virtual Stmt * getBody() const
getBody - If this Decl represents a declaration for a body of code, such as a function or method defi...
Stmt - This represents one statement.
unsigned getNumArgs() const
getNumArgs - Return the number of actual arguments to this call.
Decl - This represents one declaration (or definition), e.g.
Represents a variable declaration or definition.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
AnalysisDeclContext contains the context data for the function or method under analysis.
ASTContext & getContext()
Expr * IgnoreParenCasts() LLVM_READONLY
IgnoreParenCasts - Ignore parentheses and casts.
AnalysisDeclContext * getAnalysisDeclContext(const Decl *D)
Expr - This represents one expression.
static bool isCLibraryFunction(const FunctionDecl *FD, StringRef Name=StringRef())
Returns true if the callee is an externally-visible function in the top-level namespace, such as malloc.
CHECKER * registerChecker(AT... Args)
Used to register checkers.
BugReporter is a utility class for generating PathDiagnostics for analysis.
static PathDiagnosticLocation createBegin(const Decl *D, const SourceManager &SM)
Create a location for the beginning of the declaration.
StmtVisitor - This class implements a simple visitor for Stmt subclasses.
const Decl * getDecl() const
Dataflow Directional Tag Classes.
FunctionDecl * getDirectCallee()
If the callee is a FunctionDecl, return it. Otherwise return 0.
Expr * IgnoreParenImpCasts() LLVM_READONLY
IgnoreParenImpCasts - Ignore parentheses and implicit casts.
Expr * IgnoreParenLValueCasts() LLVM_READONLY
Ignore parentheses and lvalue casts.
uint64_t getTypeSize(QualType T) const
Return the size of the specified (complete) type T, in bits.
SourceManager & getSourceManager()
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
Defines the clang::TargetInfo interface.
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
A reference to a declared variable, function, enum, etc.
void EmitBasicReport(const Decl *DeclWithIssue, const CheckerBase *Checker, StringRef BugName, StringRef BugCategory, StringRef BugStr, PathDiagnosticLocation Loc, ArrayRef< SourceRange > Ranges=None)