26 #include "llvm/ADT/SmallString.h" 27 #include "llvm/Support/raw_ostream.h" 29 using namespace clang;
34 const CheckerBase *Checker;
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);
102 bool containsBadStrlcpyStrlcatPattern(
const CallExpr *CE);
106 : Checker(Checker), BR(BR), AC(AC) {}
109 void VisitChildren(
Stmt *S);
110 void VisitStmt(
Stmt *S) {
123 bool WalkAST::containsBadStrncatPattern(
const CallExpr *CE) {
131 if (
const auto *BE = dyn_cast<BinaryOperator>(LenArg->
IgnoreParenCasts())) {
133 if (BE->getOpcode() == BO_Sub) {
134 const Expr *L = BE->getLHS();
135 const Expr *R = BE->getRHS();
136 if (isSizeof(L, DstArg) && isStrlen(R, DstArg))
145 if (isSizeof(LenArg, DstArg))
149 if (isSizeof(LenArg, SrcArg))
154 bool WalkAST::containsBadStrlcpyStrlcatPattern(
const CallExpr *CE) {
165 if (isSizeof(LenArg, DstArg))
169 const auto *LenArgVal = dyn_cast<
VarDecl>(LenArgDecl->getDecl());
170 if (LenArgVal->getInit())
171 LenArg = LenArgVal->getInit();
178 uint64_t ILRawVal = IL->getValue().getZExtValue();
185 DstArgDecl = dyn_cast<
DeclRefExpr>(BE->getLHS()->IgnoreParenImpCasts());
186 if (BE->getOpcode() == BO_Add) {
187 if ((IL = dyn_cast<IntegerLiteral>(BE->getRHS()->IgnoreParenImpCasts()))) {
188 DstOff = IL->getValue().getZExtValue();
194 if (
const auto *Buffer = dyn_cast<ConstantArrayType>(DstArgDecl->getType())) {
197 auto RemainingBufferLen = BufferLen - DstOff;
199 if (RemainingBufferLen <= ILRawVal)
202 if (RemainingBufferLen < ILRawVal)
212 void WalkAST::VisitCallExpr(
CallExpr *CE) {
218 if (containsBadStrncatPattern(CE)) {
221 PathDiagnosticLocation Loc =
224 StringRef DstName = getPrintableName(DstArg);
227 llvm::raw_svector_ostream os(S);
228 os <<
"Potential buffer overflow. ";
229 if (!DstName.empty()) {
230 os <<
"Replace with 'sizeof(" << DstName <<
") " 231 "- strlen(" << DstName <<
") - 1'";
235 os <<
"se a safer 'strlcat' API";
237 BR.EmitBasicReport(FD, Checker,
"Anti-pattern in the argument",
238 "C String API", os.str(), Loc,
243 if (containsBadStrlcpyStrlcatPattern(CE)) {
246 PathDiagnosticLocation Loc =
249 StringRef DstName = getPrintableName(DstArg);
252 llvm::raw_svector_ostream os(S);
253 os <<
"The third argument allows to potentially copy more bytes than it should. ";
254 os <<
"Replace with the value ";
255 if (!DstName.empty())
256 os <<
"sizeof(" << DstName <<
")";
258 os <<
"sizeof(<destination buffer>)";
261 BR.EmitBasicReport(FD, Checker,
"Anti-pattern in the argument",
262 "C String API", os.str(), Loc,
271 void WalkAST::VisitChildren(
Stmt *S) {
278 class CStringSyntaxChecker:
public Checker<check::ASTCodeBody> {
281 void checkASTCodeBody(
const Decl *D, AnalysisManager& Mgr,
282 BugReporter &BR)
const {
283 WalkAST walker(
this, BR, Mgr.getAnalysisDeclContext(D));
289 void ento::registerCStringSyntaxChecker(CheckerManager &mgr) {
290 mgr.registerChecker<CStringSyntaxChecker>();
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.
static void Append(char *Start, char *End, char *&Buffer, unsigned &BufferSize, unsigned &BufferCapacity)
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.
Expr * IgnoreParenCasts() LLVM_READONLY
IgnoreParenCasts - Ignore parentheses and casts.
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.
FunctionDecl * getDirectCallee()
If the callee is a FunctionDecl, return it. Otherwise return null.
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.
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.
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.