1 //===--- SuspiciousStringCompareCheck.cpp - clang-tidy---------------------===// 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 #include "SuspiciousStringCompareCheck.h" 10 #include "../utils/Matchers.h" 11 #include "../utils/OptionsUtils.h" 12 #include "clang/AST/ASTContext.h" 13 #include "clang/ASTMatchers/ASTMatchFinder.h" 14 #include "clang/Lex/Lexer.h" 15 16 using namespace clang::ast_matchers; 17 18 namespace clang { 19 namespace tidy { 20 namespace bugprone { 21 22 // Semicolon separated list of known string compare-like functions. The list 23 // must ends with a semicolon. 24 static const char KnownStringCompareFunctions[] = "__builtin_memcmp;" 25 "__builtin_strcasecmp;" 26 "__builtin_strcmp;" 27 "__builtin_strncasecmp;" 28 "__builtin_strncmp;" 29 "_mbscmp;" 30 "_mbscmp_l;" 31 "_mbsicmp;" 32 "_mbsicmp_l;" 33 "_mbsnbcmp;" 34 "_mbsnbcmp_l;" 35 "_mbsnbicmp;" 36 "_mbsnbicmp_l;" 37 "_mbsncmp;" 38 "_mbsncmp_l;" 39 "_mbsnicmp;" 40 "_mbsnicmp_l;" 41 "_memicmp;" 42 "_memicmp_l;" 43 "_stricmp;" 44 "_stricmp_l;" 45 "_strnicmp;" 46 "_strnicmp_l;" 47 "_wcsicmp;" 48 "_wcsicmp_l;" 49 "_wcsnicmp;" 50 "_wcsnicmp_l;" 51 "lstrcmp;" 52 "lstrcmpi;" 53 "memcmp;" 54 "memicmp;" 55 "strcasecmp;" 56 "strcmp;" 57 "strcmpi;" 58 "stricmp;" 59 "strncasecmp;" 60 "strncmp;" 61 "strnicmp;" 62 "wcscasecmp;" 63 "wcscmp;" 64 "wcsicmp;" 65 "wcsncmp;" 66 "wcsnicmp;" 67 "wmemcmp;"; 68 69 SuspiciousStringCompareCheck::SuspiciousStringCompareCheck( 70 StringRef Name, ClangTidyContext *Context) 71 : ClangTidyCheck(Name, Context), 72 WarnOnImplicitComparison(Options.get("WarnOnImplicitComparison", true)), 73 WarnOnLogicalNotComparison( 74 Options.get("WarnOnLogicalNotComparison", false)), 75 StringCompareLikeFunctions( 76 Options.get("StringCompareLikeFunctions", "")) {} 77 78 void SuspiciousStringCompareCheck::storeOptions( 79 ClangTidyOptions::OptionMap &Opts) { 80 Options.store(Opts, "WarnOnImplicitComparison", WarnOnImplicitComparison); 81 Options.store(Opts, "WarnOnLogicalNotComparison", WarnOnLogicalNotComparison); 82 Options.store(Opts, "StringCompareLikeFunctions", StringCompareLikeFunctions); 83 } 84 85 void SuspiciousStringCompareCheck::registerMatchers(MatchFinder *Finder) { 86 // Match relational operators. 87 const auto ComparisonUnaryOperator = unaryOperator(hasOperatorName("!")); 88 const auto ComparisonBinaryOperator = binaryOperator(isComparisonOperator()); 89 const auto ComparisonOperator = 90 expr(anyOf(ComparisonUnaryOperator, ComparisonBinaryOperator)); 91 92 // Add the list of known string compare-like functions and add user-defined 93 // functions. 94 std::vector<std::string> FunctionNames = utils::options::parseStringList( 95 (llvm::Twine(KnownStringCompareFunctions) + StringCompareLikeFunctions) 96 .str()); 97 98 // Match a call to a string compare functions. 99 const auto FunctionCompareDecl = 100 functionDecl(hasAnyName(std::vector<StringRef>(FunctionNames.begin(), 101 FunctionNames.end()))) 102 .bind("decl"); 103 const auto DirectStringCompareCallExpr = 104 callExpr(hasDeclaration(FunctionCompareDecl)).bind("call"); 105 const auto MacroStringCompareCallExpr = conditionalOperator(anyOf( 106 hasTrueExpression(ignoringParenImpCasts(DirectStringCompareCallExpr)), 107 hasFalseExpression(ignoringParenImpCasts(DirectStringCompareCallExpr)))); 108 // The implicit cast is not present in C. 109 const auto StringCompareCallExpr = ignoringParenImpCasts( 110 anyOf(DirectStringCompareCallExpr, MacroStringCompareCallExpr)); 111 112 if (WarnOnImplicitComparison) { 113 // Detect suspicious calls to string compare: 114 // 'if (strcmp())' -> 'if (strcmp() != 0)' 115 Finder->addMatcher( 116 stmt(anyOf(ifStmt(hasCondition(StringCompareCallExpr)), 117 whileStmt(hasCondition(StringCompareCallExpr)), 118 doStmt(hasCondition(StringCompareCallExpr)), 119 forStmt(hasCondition(StringCompareCallExpr)), 120 binaryOperator(hasAnyOperatorName("&&", "||"), 121 hasEitherOperand(StringCompareCallExpr)))) 122 .bind("missing-comparison"), 123 this); 124 } 125 126 if (WarnOnLogicalNotComparison) { 127 // Detect suspicious calls to string compared with '!' operator: 128 // 'if (!strcmp())' -> 'if (strcmp() == 0)' 129 Finder->addMatcher(unaryOperator(hasOperatorName("!"), 130 hasUnaryOperand(ignoringParenImpCasts( 131 StringCompareCallExpr))) 132 .bind("logical-not-comparison"), 133 this); 134 } 135 136 // Detect suspicious cast to an inconsistant type (i.e. not integer type). 137 Finder->addMatcher( 138 implicitCastExpr(unless(hasType(isInteger())), 139 hasSourceExpression(StringCompareCallExpr)) 140 .bind("invalid-conversion"), 141 this); 142 143 // Detect suspicious operator with string compare function as operand. 144 Finder->addMatcher( 145 binaryOperator(unless(anyOf(isComparisonOperator(), hasOperatorName("&&"), 146 hasOperatorName("||"), hasOperatorName("="))), 147 hasEitherOperand(StringCompareCallExpr)) 148 .bind("suspicious-operator"), 149 this); 150 151 // Detect comparison to invalid constant: 'strcmp() == -1'. 152 const auto InvalidLiteral = ignoringParenImpCasts( 153 anyOf(integerLiteral(unless(equals(0))), 154 unaryOperator( 155 hasOperatorName("-"), 156 has(ignoringParenImpCasts(integerLiteral(unless(equals(0)))))), 157 characterLiteral(), cxxBoolLiteral())); 158 159 Finder->addMatcher(binaryOperator(isComparisonOperator(), 160 hasEitherOperand(StringCompareCallExpr), 161 hasEitherOperand(InvalidLiteral)) 162 .bind("invalid-comparison"), 163 this); 164 } 165 166 void SuspiciousStringCompareCheck::check( 167 const MatchFinder::MatchResult &Result) { 168 const auto *Decl = Result.Nodes.getNodeAs<FunctionDecl>("decl"); 169 const auto *Call = Result.Nodes.getNodeAs<CallExpr>("call"); 170 assert(Decl != nullptr && Call != nullptr); 171 172 if (Result.Nodes.getNodeAs<Stmt>("missing-comparison")) { 173 SourceLocation EndLoc = Lexer::getLocForEndOfToken( 174 Call->getRParenLoc(), 0, Result.Context->getSourceManager(), 175 getLangOpts()); 176 177 diag(Call->getBeginLoc(), 178 "function %0 is called without explicitly comparing result") 179 << Decl << FixItHint::CreateInsertion(EndLoc, " != 0"); 180 } 181 182 if (const auto *E = Result.Nodes.getNodeAs<Expr>("logical-not-comparison")) { 183 SourceLocation EndLoc = Lexer::getLocForEndOfToken( 184 Call->getRParenLoc(), 0, Result.Context->getSourceManager(), 185 getLangOpts()); 186 SourceLocation NotLoc = E->getBeginLoc(); 187 188 diag(Call->getBeginLoc(), 189 "function %0 is compared using logical not operator") 190 << Decl 191 << FixItHint::CreateRemoval( 192 CharSourceRange::getTokenRange(NotLoc, NotLoc)) 193 << FixItHint::CreateInsertion(EndLoc, " == 0"); 194 } 195 196 if (Result.Nodes.getNodeAs<Stmt>("invalid-comparison")) { 197 diag(Call->getBeginLoc(), 198 "function %0 is compared to a suspicious constant") 199 << Decl; 200 } 201 202 if (const auto *BinOp = 203 Result.Nodes.getNodeAs<BinaryOperator>("suspicious-operator")) { 204 diag(Call->getBeginLoc(), "results of function %0 used by operator '%1'") 205 << Decl << BinOp->getOpcodeStr(); 206 } 207 208 if (Result.Nodes.getNodeAs<Stmt>("invalid-conversion")) { 209 diag(Call->getBeginLoc(), "function %0 has suspicious implicit cast") 210 << Decl; 211 } 212 } 213 214 } // namespace bugprone 215 } // namespace tidy 216 } // namespace clang 217