clang-tools 23.0.0git
NotNullTerminatedResultCheck.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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
10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "clang/Frontend/CompilerInstance.h"
13#include "clang/Lex/Lexer.h"
14#include "clang/Lex/PPCallbacks.h"
15#include "clang/Lex/Preprocessor.h"
16#include <optional>
17
18using namespace clang::ast_matchers;
19
20namespace clang::tidy::bugprone {
21
22constexpr StringRef FunctionExprName = "FunctionExpr";
23constexpr StringRef CastExprName = "CastExpr";
24constexpr StringRef UnknownDestName = "UnknownDest";
25constexpr StringRef DestArrayTyName = "DestArrayTy";
26constexpr StringRef DestVarDeclName = "DestVarDecl";
27constexpr StringRef DestMallocExprName = "DestMalloc";
28constexpr StringRef DestExprName = "DestExpr";
29constexpr StringRef SrcVarDeclName = "SrcVarDecl";
30constexpr StringRef SrcExprName = "SrcExpr";
31constexpr StringRef LengthExprName = "LengthExpr";
32constexpr StringRef WrongLengthExprName = "WrongLength";
33constexpr StringRef UnknownLengthName = "UnknownLength";
34
35namespace {
36enum class LengthHandleKind { Increase, Decrease };
37} // namespace
38
39static Preprocessor *PP;
40
41// Returns the expression of destination's capacity which is part of a
42// 'VariableArrayType', 'ConstantArrayTypeLoc' or an argument of a 'malloc()'
43// family function call.
44static const Expr *getDestCapacityExpr(const MatchFinder::MatchResult &Result) {
45 if (const auto *DestMalloc = Result.Nodes.getNodeAs<Expr>(DestMallocExprName))
46 return DestMalloc;
47
48 if (const auto *DestVAT =
49 Result.Nodes.getNodeAs<VariableArrayType>(DestArrayTyName))
50 return DestVAT->getSizeExpr();
51
52 if (const auto *DestVD = Result.Nodes.getNodeAs<VarDecl>(DestVarDeclName))
53 if (const TypeLoc DestTL = DestVD->getTypeSourceInfo()->getTypeLoc())
54 if (const auto DestCTL = DestTL.getAs<ConstantArrayTypeLoc>())
55 return DestCTL.getSizeExpr();
56
57 return nullptr;
58}
59
60// Returns the length of \p E as an 'IntegerLiteral' or a 'StringLiteral'
61// without the null-terminator.
62static unsigned getLength(const Expr *E,
63 const MatchFinder::MatchResult &Result) {
64 if (!E)
65 return 0;
66
67 E = E->IgnoreImpCasts();
68
69 if (const auto *LengthDRE = dyn_cast<DeclRefExpr>(E))
70 if (const auto *LengthVD = dyn_cast<VarDecl>(LengthDRE->getDecl()))
71 if (!isa<ParmVarDecl>(LengthVD))
72 if (const Expr *LengthInit = LengthVD->getInit();
73 LengthInit && !LengthInit->isValueDependent()) {
74 Expr::EvalResult Length;
75 if (LengthInit->EvaluateAsInt(Length, *Result.Context))
76 return Length.Val.getInt().getZExtValue();
77 }
78
79 if (const auto *LengthIL = dyn_cast<IntegerLiteral>(E))
80 return LengthIL->getValue().getZExtValue();
81
82 if (const auto *StrDRE = dyn_cast<DeclRefExpr>(E))
83 if (const auto *StrVD = dyn_cast<VarDecl>(StrDRE->getDecl()))
84 if (const Expr *StrInit = StrVD->getInit())
85 if (const auto *StrSL =
86 dyn_cast<StringLiteral>(StrInit->IgnoreImpCasts()))
87 return StrSL->getLength();
88
89 if (const auto *SrcSL = dyn_cast<StringLiteral>(E))
90 return SrcSL->getLength();
91
92 return 0;
93}
94
95// Returns the capacity of the destination array.
96// For example in 'char dest[13]; memcpy(dest, ...)' it returns 13.
97static int getDestCapacity(const MatchFinder::MatchResult &Result) {
98 if (const auto *DestCapacityExpr = getDestCapacityExpr(Result))
99 return getLength(DestCapacityExpr, Result);
100
101 return 0;
102}
103
104// Returns the 'strlen()' if it is the given length.
105static const CallExpr *getStrlenExpr(const MatchFinder::MatchResult &Result) {
106 if (const auto *StrlenExpr =
107 Result.Nodes.getNodeAs<CallExpr>(WrongLengthExprName))
108 if (const Decl *D = StrlenExpr->getCalleeDecl())
109 if (const FunctionDecl *FD = D->getAsFunction())
110 if (const IdentifierInfo *II = FD->getIdentifier())
111 if (II->isStr("strlen") || II->isStr("wcslen"))
112 return StrlenExpr;
113
114 return nullptr;
115}
116
117// Returns the length which is given in the memory/string handler function.
118// For example in 'memcpy(dest, "foobar", 3)' it returns 3.
119static int getGivenLength(const MatchFinder::MatchResult &Result) {
120 if (Result.Nodes.getNodeAs<Expr>(UnknownLengthName))
121 return 0;
122
123 if (const int Length =
124 getLength(Result.Nodes.getNodeAs<Expr>(WrongLengthExprName), Result))
125 return Length;
126
127 if (const int Length =
128 getLength(Result.Nodes.getNodeAs<Expr>(LengthExprName), Result))
129 return Length;
130
131 // Special case, for example 'strlen("foo")'.
132 if (const CallExpr *StrlenCE = getStrlenExpr(Result))
133 if (const Expr *Arg = StrlenCE->getArg(0)->IgnoreImpCasts())
134 if (const int ArgLength = getLength(Arg, Result))
135 return ArgLength;
136
137 return 0;
138}
139
140// Returns a string representation of \p E.
141static StringRef exprToStr(const Expr *E,
142 const MatchFinder::MatchResult &Result) {
143 if (!E)
144 return "";
145
146 return Lexer::getSourceText(
147 CharSourceRange::getTokenRange(E->getSourceRange()),
148 *Result.SourceManager, Result.Context->getLangOpts(), nullptr);
149}
150
151// Returns the proper token based end location of \p E.
152static SourceLocation exprLocEnd(const Expr *E,
153 const MatchFinder::MatchResult &Result) {
154 return Lexer::getLocForEndOfToken(E->getEndLoc(), 0, *Result.SourceManager,
155 Result.Context->getLangOpts());
156}
157
158//===----------------------------------------------------------------------===//
159// Rewrite decision helper functions.
160//===----------------------------------------------------------------------===//
161
162// Increment by integer '1' can result in overflow if it is the maximal value.
163// After that it would be extended to 'size_t' and its value would be wrong,
164// therefore we have to inject '+ 1UL' instead.
165static bool isInjectUL(const MatchFinder::MatchResult &Result) {
166 return getGivenLength(Result) == std::numeric_limits<int>::max();
167}
168
169// If the capacity of the destination array is unknown it is denoted as unknown.
170static bool isKnownDest(const MatchFinder::MatchResult &Result) {
171 return !Result.Nodes.getNodeAs<Expr>(UnknownDestName);
172}
173
174// True if the capacity of the destination array is based on the given length,
175// therefore we assume that it cannot overflow (e.g. 'malloc(given_length + 1)'
176static bool isDestBasedOnGivenLength(const MatchFinder::MatchResult &Result) {
177 const StringRef DestCapacityExprStr =
178 exprToStr(getDestCapacityExpr(Result), Result).trim();
179 const StringRef LengthExprStr =
180 exprToStr(Result.Nodes.getNodeAs<Expr>(LengthExprName), Result).trim();
181
182 return !DestCapacityExprStr.empty() && !LengthExprStr.empty() &&
183 DestCapacityExprStr.contains(LengthExprStr);
184}
185
186// Writing and reading from the same memory cannot remove the null-terminator.
187static bool isDestAndSrcEquals(const MatchFinder::MatchResult &Result) {
188 if (const auto *DestDRE = Result.Nodes.getNodeAs<DeclRefExpr>(DestExprName))
189 if (const auto *SrcDRE = Result.Nodes.getNodeAs<DeclRefExpr>(SrcExprName))
190 return DestDRE->getDecl()->getCanonicalDecl() ==
191 SrcDRE->getDecl()->getCanonicalDecl();
192
193 return false;
194}
195
196// For example 'std::string str = "foo"; memcpy(dst, str.data(), str.length())'.
197static bool isStringDataAndLength(const MatchFinder::MatchResult &Result) {
198 const auto *DestExpr =
199 Result.Nodes.getNodeAs<CXXMemberCallExpr>(DestExprName);
200 const auto *SrcExpr = Result.Nodes.getNodeAs<CXXMemberCallExpr>(SrcExprName);
201 const auto *LengthExpr =
202 Result.Nodes.getNodeAs<CXXMemberCallExpr>(WrongLengthExprName);
203
204 StringRef DestStr = "", SrcStr = "", LengthStr = "";
205 if (DestExpr)
206 if (const CXXMethodDecl *DestMD = DestExpr->getMethodDecl())
207 DestStr = DestMD->getName();
208
209 if (SrcExpr)
210 if (const CXXMethodDecl *SrcMD = SrcExpr->getMethodDecl())
211 SrcStr = SrcMD->getName();
212
213 if (LengthExpr)
214 if (const CXXMethodDecl *LengthMD = LengthExpr->getMethodDecl())
215 LengthStr = LengthMD->getName();
216
217 return (LengthStr == "length" || LengthStr == "size") &&
218 (SrcStr == "data" || DestStr == "data");
219}
220
221static bool
222isGivenLengthEqualToSrcLength(const MatchFinder::MatchResult &Result) {
223 if (Result.Nodes.getNodeAs<Expr>(UnknownLengthName))
224 return false;
225
226 if (isStringDataAndLength(Result))
227 return true;
228
229 const int GivenLength = getGivenLength(Result);
230 const int SrcLength =
231 getLength(Result.Nodes.getNodeAs<Expr>(SrcExprName), Result);
232
233 if (GivenLength != 0 && SrcLength != 0 && GivenLength == SrcLength)
234 return true;
235
236 if (const auto *LengthExpr = Result.Nodes.getNodeAs<Expr>(LengthExprName))
237 if (isa<BinaryOperator>(LengthExpr->IgnoreParenImpCasts()))
238 return false;
239
240 // Check the strlen()'s argument's 'VarDecl' is equal to the source 'VarDecl'.
241 if (const CallExpr *StrlenCE = getStrlenExpr(Result))
242 if (const auto *ArgDRE =
243 dyn_cast<DeclRefExpr>(StrlenCE->getArg(0)->IgnoreImpCasts()))
244 if (const auto *SrcVD = Result.Nodes.getNodeAs<VarDecl>(SrcVarDeclName))
245 return dyn_cast<VarDecl>(ArgDRE->getDecl()) == SrcVD;
246
247 return false;
248}
249
250static bool isCorrectGivenLength(const MatchFinder::MatchResult &Result) {
251 if (Result.Nodes.getNodeAs<Expr>(UnknownLengthName))
252 return false;
253
254 return !isGivenLengthEqualToSrcLength(Result);
255}
256
257// If we rewrite the function call we need to create extra space to hold the
258// null terminator. The new necessary capacity overflows without that '+ 1'
259// size and we need to correct the given capacity.
260static bool isDestCapacityOverflows(const MatchFinder::MatchResult &Result) {
261 if (!isKnownDest(Result))
262 return true;
263
264 const Expr *DestCapacityExpr = getDestCapacityExpr(Result);
265 const int DestCapacity = getLength(DestCapacityExpr, Result);
266 const int GivenLength = getGivenLength(Result);
267
268 if (GivenLength != 0 && DestCapacity != 0)
269 return isGivenLengthEqualToSrcLength(Result) && DestCapacity == GivenLength;
270
271 // Assume that the destination array's capacity cannot overflow if the
272 // expression of the memory allocation contains '+ 1'.
273 const StringRef DestCapacityExprStr = exprToStr(DestCapacityExpr, Result);
274 if (DestCapacityExprStr.contains("+1") || DestCapacityExprStr.contains("+ 1"))
275 return false;
276
277 return true;
278}
279
280static bool
281isFixedGivenLengthAndUnknownSrc(const MatchFinder::MatchResult &Result) {
282 if (Result.Nodes.getNodeAs<IntegerLiteral>(WrongLengthExprName))
283 return !getLength(Result.Nodes.getNodeAs<Expr>(SrcExprName), Result);
284
285 return false;
286}
287
288//===----------------------------------------------------------------------===//
289// Code injection functions.
290//===----------------------------------------------------------------------===//
291
292// Increase or decrease \p LengthExpr by one.
293static void lengthExprHandle(const Expr *LengthExpr,
294 LengthHandleKind LengthHandle,
295 const MatchFinder::MatchResult &Result,
296 DiagnosticBuilder &Diag) {
297 LengthExpr = LengthExpr->IgnoreParenImpCasts();
298
299 // See whether we work with a macro.
300 const StringRef LengthExprStr = exprToStr(LengthExpr, Result);
301 const bool IsMacroDefinition = llvm::any_of(PP->macros(), [=](const auto &M) {
302 return M.first->getName() == LengthExprStr;
303 });
304
305 // Try to obtain an 'IntegerLiteral' and adjust it.
306 if (!IsMacroDefinition) {
307 if (const auto *LengthIL = dyn_cast<IntegerLiteral>(LengthExpr)) {
308 const uint64_t NewLength =
309 LengthIL->getValue().getZExtValue() +
310 (LengthHandle == LengthHandleKind::Increase ? 1 : -1);
311
312 const auto NewLengthFix = FixItHint::CreateReplacement(
313 LengthIL->getSourceRange(),
314 (Twine(NewLength) + (isInjectUL(Result) ? "UL" : "")).str());
315 Diag << NewLengthFix;
316 return;
317 }
318 }
319
320 // Try to obtain and remove the '+ 1' string as a decrement fix.
321 const auto *BO = dyn_cast<BinaryOperator>(LengthExpr);
322 if (BO && BO->getOpcode() == BO_Add &&
323 LengthHandle == LengthHandleKind::Decrease) {
324 const Expr *LhsExpr = BO->getLHS()->IgnoreImpCasts();
325 const Expr *RhsExpr = BO->getRHS()->IgnoreImpCasts();
326
327 if (const auto *LhsIL = dyn_cast<IntegerLiteral>(LhsExpr)) {
328 if (LhsIL->getValue().getZExtValue() == 1) {
329 Diag << FixItHint::CreateRemoval(
330 {LhsIL->getBeginLoc(),
331 RhsExpr->getBeginLoc().getLocWithOffset(-1)});
332 return;
333 }
334 }
335
336 if (const auto *RhsIL = dyn_cast<IntegerLiteral>(RhsExpr)) {
337 if (RhsIL->getValue().getZExtValue() == 1) {
338 Diag << FixItHint::CreateRemoval(
339 {LhsExpr->getEndLoc().getLocWithOffset(1), RhsIL->getEndLoc()});
340 return;
341 }
342 }
343 }
344
345 // Try to inject the '+ 1'/'- 1' string.
346 const bool NeedInnerParen = BO && BO->getOpcode() != BO_Add;
347
348 if (NeedInnerParen)
349 Diag << FixItHint::CreateInsertion(LengthExpr->getBeginLoc(), "(");
350
351 SmallString<8> Injection;
352 if (NeedInnerParen)
353 Injection += ')';
354 Injection += LengthHandle == LengthHandleKind::Increase ? " + 1" : " - 1";
355 if (isInjectUL(Result))
356 Injection += "UL";
357
358 Diag << FixItHint::CreateInsertion(exprLocEnd(LengthExpr, Result), Injection);
359}
360
361static void lengthArgHandle(LengthHandleKind LengthHandle,
362 const MatchFinder::MatchResult &Result,
363 DiagnosticBuilder &Diag) {
364 const auto *LengthExpr = Result.Nodes.getNodeAs<Expr>(LengthExprName);
365 lengthExprHandle(LengthExpr, LengthHandle, Result, Diag);
366}
367
368static void lengthArgPosHandle(unsigned ArgPos, LengthHandleKind LengthHandle,
369 const MatchFinder::MatchResult &Result,
370 DiagnosticBuilder &Diag) {
371 const auto *FunctionExpr = Result.Nodes.getNodeAs<CallExpr>(FunctionExprName);
372 lengthExprHandle(FunctionExpr->getArg(ArgPos), LengthHandle, Result, Diag);
373}
374
375// The string handler functions are only operates with plain 'char'/'wchar_t'
376// without 'unsigned/signed', therefore we need to cast it.
377static bool isDestExprFix(const MatchFinder::MatchResult &Result,
378 DiagnosticBuilder &Diag) {
379 const auto *Dest = Result.Nodes.getNodeAs<Expr>(DestExprName);
380 if (!Dest)
381 return false;
382
383 const std::string TempTyStr = Dest->getType().getAsString();
384 const StringRef TyStr = TempTyStr;
385 if (TyStr.starts_with("char") || TyStr.starts_with("wchar_t"))
386 return false;
387
388 Diag << FixItHint::CreateInsertion(Dest->getBeginLoc(), "(char *)");
389 return true;
390}
391
392// If the destination array is the same length as the given length we have to
393// increase the capacity by one to create space for the null terminator.
394static bool isDestCapacityFix(const MatchFinder::MatchResult &Result,
395 DiagnosticBuilder &Diag) {
396 const bool IsOverflows = isDestCapacityOverflows(Result);
397 if (IsOverflows)
398 if (const Expr *CapacityExpr = getDestCapacityExpr(Result))
399 lengthExprHandle(CapacityExpr, LengthHandleKind::Increase, Result, Diag);
400
401 return IsOverflows;
402}
403
404static void removeArg(int ArgPos, const MatchFinder::MatchResult &Result,
405 DiagnosticBuilder &Diag) {
406 // This is the following structure: (src, '\0', strlen(src))
407 // ArgToRemove: ~~~~~~~~~~~
408 // LHSArg: ~~~~
409 // RemoveArgFix: ~~~~~~~~~~~~~
410 const auto *FunctionExpr = Result.Nodes.getNodeAs<CallExpr>(FunctionExprName);
411 const Expr *ArgToRemove = FunctionExpr->getArg(ArgPos);
412 const Expr *LHSArg = FunctionExpr->getArg(ArgPos - 1);
413 const auto RemoveArgFix = FixItHint::CreateRemoval(
414 SourceRange(exprLocEnd(LHSArg, Result),
415 exprLocEnd(ArgToRemove, Result).getLocWithOffset(-1)));
416 Diag << RemoveArgFix;
417}
418
419static void renameFunc(StringRef NewFuncName,
420 const MatchFinder::MatchResult &Result,
421 DiagnosticBuilder &Diag) {
422 const auto *FunctionExpr = Result.Nodes.getNodeAs<CallExpr>(FunctionExprName);
423 const int FuncNameLength =
424 FunctionExpr->getDirectCallee()->getIdentifier()->getLength();
425 const SourceRange FuncNameRange(
426 FunctionExpr->getBeginLoc(),
427 FunctionExpr->getBeginLoc().getLocWithOffset(FuncNameLength - 1));
428
429 const auto FuncNameFix =
430 FixItHint::CreateReplacement(FuncNameRange, NewFuncName);
431 Diag << FuncNameFix;
432}
433
434static void renameMemcpy(StringRef Name, bool IsCopy, bool IsSafe,
435 const MatchFinder::MatchResult &Result,
436 DiagnosticBuilder &Diag) {
437 SmallString<10> NewFuncName;
438 NewFuncName = (Name[0] != 'w') ? "str" : "wcs";
439 NewFuncName += IsCopy ? "cpy" : "ncpy";
440 NewFuncName += IsSafe ? "_s" : "";
441 renameFunc(NewFuncName, Result, Diag);
442}
443
444static void insertDestCapacityArg(bool IsOverflows, StringRef Name,
445 const MatchFinder::MatchResult &Result,
446 DiagnosticBuilder &Diag) {
447 const auto *FunctionExpr = Result.Nodes.getNodeAs<CallExpr>(FunctionExprName);
448 SmallString<64> NewSecondArg;
449
450 if (const int DestLength = getDestCapacity(Result)) {
451 NewSecondArg = Twine(IsOverflows ? DestLength + 1 : DestLength).str();
452 } else {
453 NewSecondArg =
454 (Twine(exprToStr(getDestCapacityExpr(Result), Result)) +
455 (IsOverflows ? (!isInjectUL(Result) ? " + 1" : " + 1UL") : ""))
456 .str();
457 }
458
459 NewSecondArg += ", ";
460 const auto InsertNewArgFix = FixItHint::CreateInsertion(
461 FunctionExpr->getArg(1)->getBeginLoc(), NewSecondArg);
462 Diag << InsertNewArgFix;
463}
464
465static void insertNullTerminatorExpr(StringRef Name,
466 const MatchFinder::MatchResult &Result,
467 DiagnosticBuilder &Diag) {
468 const auto *FunctionExpr = Result.Nodes.getNodeAs<CallExpr>(FunctionExprName);
469 const int FuncLocStartColumn = Result.SourceManager->getPresumedColumnNumber(
470 FunctionExpr->getBeginLoc());
471 const SourceRange SpaceRange(
472 FunctionExpr->getBeginLoc().getLocWithOffset(-FuncLocStartColumn + 1),
473 FunctionExpr->getBeginLoc());
474 const StringRef SpaceBeforeStmtStr = Lexer::getSourceText(
475 CharSourceRange::getCharRange(SpaceRange), *Result.SourceManager,
476 Result.Context->getLangOpts(), nullptr);
477
478 SmallString<128> NewAddNullTermExprStr;
479 NewAddNullTermExprStr =
480 (Twine('\n') + SpaceBeforeStmtStr +
481 exprToStr(Result.Nodes.getNodeAs<Expr>(DestExprName), Result) + "[" +
482 exprToStr(Result.Nodes.getNodeAs<Expr>(LengthExprName), Result) +
483 "] = " + ((Name[0] != 'w') ? R"('\0';)" : R"(L'\0';)"))
484 .str();
485
486 const auto AddNullTerminatorExprFix = FixItHint::CreateInsertion(
487 exprLocEnd(FunctionExpr, Result).getLocWithOffset(1),
488 NewAddNullTermExprStr);
489 Diag << AddNullTerminatorExprFix;
490}
491
492//===----------------------------------------------------------------------===//
493// Checker logic with the matchers.
494//===----------------------------------------------------------------------===//
495
497 StringRef Name, ClangTidyContext *Context)
498 : ClangTidyCheck(Name, Context),
499 WantToUseSafeFunctions(Options.get("WantToUseSafeFunctions", true)) {}
500
503 Options.store(Opts, "WantToUseSafeFunctions", WantToUseSafeFunctions);
504}
505
507 const SourceManager &SM, Preprocessor *Pp, Preprocessor *ModuleExpanderPP) {
508 PP = Pp;
509}
510
511namespace {
512AST_MATCHER_P(Expr, hasDefinition, ast_matchers::internal::Matcher<Expr>,
513 InnerMatcher) {
514 const Expr *SimpleNode = &Node;
515 SimpleNode = SimpleNode->IgnoreParenImpCasts();
516
517 if (InnerMatcher.matches(*SimpleNode, Finder, Builder))
518 return true;
519
520 auto DREHasInit = ignoringImpCasts(
521 declRefExpr(to(varDecl(hasInitializer(ignoringImpCasts(InnerMatcher))))));
522
523 if (DREHasInit.matches(*SimpleNode, Finder, Builder))
524 return true;
525
526 const char *const VarDeclName = "variable-declaration";
527 auto DREHasDefinition = ignoringImpCasts(declRefExpr(
528 to(varDecl().bind(VarDeclName)),
529 hasAncestor(compoundStmt(hasDescendant(binaryOperator(
530 hasLHS(declRefExpr(to(varDecl(equalsBoundNode(VarDeclName))))),
531 hasRHS(ignoringImpCasts(InnerMatcher))))))));
532
533 if (DREHasDefinition.matches(*SimpleNode, Finder, Builder))
534 return true;
535
536 return false;
537}
538} // namespace
539
541 auto IncOp =
542 binaryOperator(hasOperatorName("+"),
543 hasEitherOperand(ignoringParenImpCasts(integerLiteral())));
544
545 auto DecOp =
546 binaryOperator(hasOperatorName("-"),
547 hasEitherOperand(ignoringParenImpCasts(integerLiteral())));
548
549 auto HasIncOp = anyOf(ignoringImpCasts(IncOp), hasDescendant(IncOp));
550 auto HasDecOp = anyOf(ignoringImpCasts(DecOp), hasDescendant(DecOp));
551
552 auto Container = ignoringImpCasts(cxxMemberCallExpr(hasDescendant(declRefExpr(
553 hasType(hasUnqualifiedDesugaredType(recordType(hasDeclaration(recordDecl(
554 hasAnyName("::std::vector", "::std::list", "::std::deque"))))))))));
555
556 auto StringTy = type(hasUnqualifiedDesugaredType(recordType(
557 hasDeclaration(cxxRecordDecl(hasName("::std::basic_string"))))));
558
559 auto AnyOfStringTy =
560 anyOf(hasType(StringTy), hasType(qualType(pointsTo(StringTy))));
561
562 auto CharTyArray = hasType(qualType(hasCanonicalType(
563 arrayType(hasElementType(isAnyCharacter())).bind(DestArrayTyName))));
564
565 auto CharTyPointer = hasType(
566 qualType(hasCanonicalType(pointerType(pointee(isAnyCharacter())))));
567
568 auto AnyOfCharTy = anyOf(CharTyArray, CharTyPointer);
569
570 //===--------------------------------------------------------------------===//
571 // The following six cases match problematic length expressions.
572 //===--------------------------------------------------------------------===//
573
574 // - Example: char src[] = "foo"; strlen(src);
575 auto Strlen =
576 callExpr(callee(functionDecl(hasAnyName("::strlen", "::wcslen"))))
577 .bind(WrongLengthExprName);
578
579 // - Example: std::string str = "foo"; str.size();
580 auto SizeOrLength =
581 cxxMemberCallExpr(on(expr(AnyOfStringTy).bind("Foo")),
582 has(memberExpr(member(hasAnyName("size", "length")))))
583 .bind(WrongLengthExprName);
584
585 // - Example: char src[] = "foo"; sizeof(src);
586 auto SizeOfCharExpr = unaryExprOrTypeTraitExpr(has(expr(AnyOfCharTy)));
587
588 auto WrongLength =
589 ignoringImpCasts(anyOf(Strlen, SizeOrLength, hasDescendant(Strlen),
590 hasDescendant(SizeOrLength)));
591
592 // - Example: length = strlen(src);
593 auto DREWithoutInc =
594 ignoringImpCasts(declRefExpr(to(varDecl(hasInitializer(WrongLength)))));
595
596 auto AnyOfCallOrDREWithoutInc = anyOf(DREWithoutInc, WrongLength);
597
598 // - Example: int getLength(const char *str) { return strlen(str); }
599 auto CallExprReturnWithoutInc = ignoringImpCasts(callExpr(callee(functionDecl(
600 hasBody(has(returnStmt(hasReturnValue(AnyOfCallOrDREWithoutInc))))))));
601
602 // - Example: int length = getLength(src);
603 auto DREHasReturnWithoutInc = ignoringImpCasts(
604 declRefExpr(to(varDecl(hasInitializer(CallExprReturnWithoutInc)))));
605
606 auto AnyOfWrongLengthInit =
607 anyOf(WrongLength, AnyOfCallOrDREWithoutInc, CallExprReturnWithoutInc,
608 DREHasReturnWithoutInc);
609
610 //===--------------------------------------------------------------------===//
611 // The following five cases match the 'destination' array length's
612 // expression which is used in 'memcpy()' and 'memmove()' matchers.
613 //===--------------------------------------------------------------------===//
614
615 // Note: Sometimes the size of char is explicitly written out.
616 auto SizeExpr = anyOf(SizeOfCharExpr, integerLiteral(equals(1)));
617
618 auto MallocLengthExpr = allOf(
619 callee(functionDecl(
620 hasAnyName("::alloca", "::calloc", "malloc", "realloc"))),
621 hasAnyArgument(allOf(unless(SizeExpr), expr().bind(DestMallocExprName))));
622
623 // - Example: (char *)malloc(length);
624 auto DestMalloc = anyOf(callExpr(MallocLengthExpr),
625 hasDescendant(callExpr(MallocLengthExpr)));
626
627 // - Example: new char[length];
628 auto DestCXXNewExpr = ignoringImpCasts(
629 cxxNewExpr(hasArraySize(expr().bind(DestMallocExprName))));
630
631 auto AnyOfDestInit = anyOf(DestMalloc, DestCXXNewExpr);
632
633 // - Example: char dest[13]; or char dest[length];
634 auto DestArrayTyDecl = declRefExpr(
635 to(anyOf(varDecl(CharTyArray).bind(DestVarDeclName),
636 varDecl(hasInitializer(AnyOfDestInit)).bind(DestVarDeclName))));
637
638 // - Example: foo[bar[baz]].qux; (or just ParmVarDecl)
639 auto DestUnknownDecl =
640 declRefExpr(to(varDecl(AnyOfCharTy).bind(DestVarDeclName)),
641 expr().bind(UnknownDestName))
642 .bind(DestExprName);
643
644 auto AnyOfDestDecl = ignoringImpCasts(
645 anyOf(allOf(hasDefinition(anyOf(AnyOfDestInit, DestArrayTyDecl,
646 hasDescendant(DestArrayTyDecl))),
647 expr().bind(DestExprName)),
648 anyOf(DestUnknownDecl, hasDescendant(DestUnknownDecl))));
649
650 auto NullTerminatorExpr = binaryOperator(
651 hasLHS(anyOf(hasDescendant(declRefExpr(to(varDecl(
652 equalsBoundNode(std::string(DestVarDeclName)))))),
653 hasDescendant(declRefExpr(
654 equalsBoundNode(std::string(DestExprName)))))),
655 hasRHS(ignoringImpCasts(
656 anyOf(characterLiteral(equals(0U)), integerLiteral(equals(0))))));
657
658 auto SrcDecl =
659 declRefExpr(to(decl().bind(SrcVarDeclName)),
660 anyOf(hasAncestor(cxxMemberCallExpr().bind(SrcExprName)),
661 expr().bind(SrcExprName)));
662
663 auto AnyOfSrcDecl =
664 ignoringImpCasts(anyOf(stringLiteral().bind(SrcExprName),
665 hasDescendant(stringLiteral().bind(SrcExprName)),
666 SrcDecl, hasDescendant(SrcDecl)));
667
668 //===--------------------------------------------------------------------===//
669 // Match the problematic function calls.
670 //===--------------------------------------------------------------------===//
671
672 struct CallContext {
673 CallContext(StringRef Name, std::optional<unsigned> DestinationPos,
674 std::optional<unsigned> SourcePos, unsigned LengthPos,
675 bool WithIncrease)
676 : Name(Name), DestinationPos(DestinationPos), SourcePos(SourcePos),
677 LengthPos(LengthPos), WithIncrease(WithIncrease) {}
678
679 StringRef Name;
680 std::optional<unsigned> DestinationPos;
681 std::optional<unsigned> SourcePos;
682 unsigned LengthPos;
683 bool WithIncrease;
684 };
685
686 auto MatchDestination = [=](CallContext CC) {
687 return hasArgument(*CC.DestinationPos,
688 allOf(AnyOfDestDecl,
689 unless(hasAncestor(compoundStmt(
690 hasDescendant(NullTerminatorExpr)))),
691 unless(Container)));
692 };
693
694 auto MatchSource = [=](CallContext CC) {
695 return hasArgument(*CC.SourcePos, AnyOfSrcDecl);
696 };
697
698 auto MatchGivenLength = [=](CallContext CC) {
699 return hasArgument(
700 CC.LengthPos,
701 allOf(
702 anyOf(ignoringImpCasts(integerLiteral().bind(WrongLengthExprName)),
703 allOf(unless(hasDefinition(SizeOfCharExpr)),
704 allOf(CC.WithIncrease
705 ? ignoringImpCasts(hasDefinition(HasIncOp))
706 : ignoringImpCasts(
707 allOf(unless(hasDefinition(HasIncOp)),
708 hasDefinition(optionally(
709 binaryOperator().bind(
711 AnyOfWrongLengthInit))),
712 expr().bind(LengthExprName)));
713 };
714
715 auto MatchCall = [=](CallContext CC) {
716 const std::string CharHandlerFuncName = "::" + CC.Name.str();
717
718 // Try to match with 'wchar_t' based function calls.
719 const std::string WcharHandlerFuncName =
720 "::" + (CC.Name.starts_with("mem") ? "w" + CC.Name.str()
721 : "wcs" + CC.Name.substr(3).str());
722
723 return allOf(callee(functionDecl(
724 hasAnyName(CharHandlerFuncName, WcharHandlerFuncName))),
725 MatchGivenLength(CC));
726 };
727
728 auto Match = [=](CallContext CC) {
729 if (CC.DestinationPos && CC.SourcePos)
730 return allOf(MatchCall(CC), MatchDestination(CC), MatchSource(CC));
731
732 if (CC.DestinationPos && !CC.SourcePos)
733 return allOf(MatchCall(CC), MatchDestination(CC),
734 hasArgument(*CC.DestinationPos, anything()));
735
736 if (!CC.DestinationPos && CC.SourcePos)
737 return allOf(MatchCall(CC), MatchSource(CC),
738 hasArgument(*CC.SourcePos, anything()));
739
740 llvm_unreachable("Unhandled match");
741 };
742
743 // void *memcpy(void *dest, const void *src, size_t count)
744 auto Memcpy = Match({"memcpy", 0, 1, 2, false});
745
746 // errno_t memcpy_s(void *dest, size_t ds, const void *src, size_t count)
747 auto MemcpyS = Match({"memcpy_s", 0, 2, 3, false});
748
749 // void *memchr(const void *src, int c, size_t count)
750 auto Memchr = Match({"memchr", std::nullopt, 0, 2, false});
751
752 // void *memmove(void *dest, const void *src, size_t count)
753 auto Memmove = Match({"memmove", 0, 1, 2, false});
754
755 // errno_t memmove_s(void *dest, size_t ds, const void *src, size_t count)
756 auto MemmoveS = Match({"memmove_s", 0, 2, 3, false});
757
758 // int strncmp(const char *str1, const char *str2, size_t count);
759 auto StrncmpRHS = Match({"strncmp", std::nullopt, 1, 2, true});
760 auto StrncmpLHS = Match({"strncmp", std::nullopt, 0, 2, true});
761
762 // size_t strxfrm(char *dest, const char *src, size_t count);
763 auto Strxfrm = Match({"strxfrm", 0, 1, 2, false});
764
765 // errno_t strerror_s(char *buffer, size_t bufferSize, int errnum);
766 auto StrerrorS = Match({"strerror_s", 0, std::nullopt, 1, false});
767
768 auto AnyOfMatchers = anyOf(Memcpy, MemcpyS, Memmove, MemmoveS, StrncmpRHS,
769 StrncmpLHS, Strxfrm, StrerrorS);
770
771 Finder->addMatcher(callExpr(AnyOfMatchers).bind(FunctionExprName), this);
772
773 // Need to remove the CastExpr from 'memchr()' as 'strchr()' returns 'char *'.
774 Finder->addMatcher(
775 callExpr(Memchr,
776 unless(hasAncestor(castExpr(unless(implicitCastExpr())))))
777 .bind(FunctionExprName),
778 this);
779 Finder->addMatcher(
780 castExpr(allOf(unless(implicitCastExpr()),
781 has(callExpr(Memchr).bind(FunctionExprName))))
782 .bind(CastExprName),
783 this);
784}
785
787 const MatchFinder::MatchResult &Result) {
788 const auto *FunctionExpr = Result.Nodes.getNodeAs<CallExpr>(FunctionExprName);
789 if (FunctionExpr->getBeginLoc().isMacroID())
790 return;
791
792 if (WantToUseSafeFunctions && PP->isMacroDefined("__STDC_LIB_EXT1__")) {
793 std::optional<bool> AreSafeFunctionsWanted;
794 for (const auto &M : PP->macros()) {
795 if (M.first->getName() != "__STDC_WANT_LIB_EXT1__")
796 continue;
797 const auto *MI = PP->getMacroInfo(M.first);
798 // PP->getMacroInfo() returns nullptr if macro has no definition.
799 if (!MI)
800 continue;
801 const auto &T = MI->tokens().back();
802 if (T.isLiteral() && T.getLiteralData()) {
803 const StringRef ValueStr(T.getLiteralData(), T.getLength());
804 llvm::APInt IntValue;
805 ValueStr.getAsInteger(10, IntValue);
806 AreSafeFunctionsWanted = IntValue.getZExtValue();
807 break;
808 }
809 }
810
811 if (AreSafeFunctionsWanted)
812 UseSafeFunctions = *AreSafeFunctionsWanted;
813 }
814
815 const StringRef Name = FunctionExpr->getDirectCallee()->getName();
816 if (Name.starts_with("mem") || Name.starts_with("wmem"))
817 memoryHandlerFunctionFix(Name, Result);
818 else if (Name == "strerror_s")
819 strerrorSFix(Result);
820 else if (Name.ends_with("ncmp"))
821 ncmpFix(Name, Result);
822 else if (Name.ends_with("xfrm"))
823 xfrmFix(Name, Result);
824}
825
826void NotNullTerminatedResultCheck::memoryHandlerFunctionFix(
827 StringRef Name, const MatchFinder::MatchResult &Result) {
828 if (isCorrectGivenLength(Result))
829 return;
830
831 if (Name.ends_with("chr")) {
832 memchrFix(Name, Result);
833 return;
834 }
835
836 if ((Name.contains("cpy") || Name.contains("move")) &&
838 return;
839
840 auto Diag =
841 diag(Result.Nodes.getNodeAs<CallExpr>(FunctionExprName)->getBeginLoc(),
842 "the result from calling '%0' is not null-terminated")
843 << Name;
844
845 if (Name.ends_with("cpy")) {
846 memcpyFix(Name, Result, Diag);
847 } else if (Name.ends_with("cpy_s")) {
848 memcpySFix(Name, Result, Diag);
849 } else if (Name.ends_with("move")) {
850 memmoveFix(Name, Result, Diag);
851 } else if (Name.ends_with("move_s")) {
852 isDestCapacityFix(Result, Diag);
853 lengthArgHandle(LengthHandleKind::Increase, Result, Diag);
854 }
855}
856
857void NotNullTerminatedResultCheck::memcpyFix(
858 StringRef Name, const MatchFinder::MatchResult &Result,
859 DiagnosticBuilder &Diag) {
860 const bool IsOverflows = isDestCapacityFix(Result, Diag);
861 const bool IsDestFixed = isDestExprFix(Result, Diag);
862
863 const bool IsCopy =
865
866 const bool IsSafe = UseSafeFunctions && IsOverflows && isKnownDest(Result) &&
868
869 const bool IsDestLengthNotRequired =
870 IsSafe && getLangOpts().CPlusPlus &&
871 Result.Nodes.getNodeAs<ArrayType>(DestArrayTyName) && !IsDestFixed;
872
873 renameMemcpy(Name, IsCopy, IsSafe, Result, Diag);
874
875 if (IsSafe && !IsDestLengthNotRequired)
876 insertDestCapacityArg(IsOverflows, Name, Result, Diag);
877
878 if (IsCopy)
879 removeArg(2, Result, Diag);
880
881 if (!IsCopy && !IsSafe)
882 insertNullTerminatorExpr(Name, Result, Diag);
883}
884
885void NotNullTerminatedResultCheck::memcpySFix(
886 StringRef Name, const MatchFinder::MatchResult &Result,
887 DiagnosticBuilder &Diag) {
888 const bool IsOverflows = isDestCapacityFix(Result, Diag);
889 const bool IsDestFixed = isDestExprFix(Result, Diag);
890
891 const bool RemoveDestLength =
892 getLangOpts().CPlusPlus &&
893 Result.Nodes.getNodeAs<ArrayType>(DestArrayTyName) && !IsDestFixed;
894 const bool IsCopy = isGivenLengthEqualToSrcLength(Result);
895 const bool IsSafe = IsOverflows;
896
897 renameMemcpy(Name, IsCopy, IsSafe, Result, Diag);
898
899 if (!IsSafe || (IsSafe && RemoveDestLength))
900 removeArg(1, Result, Diag);
901 else if (IsOverflows && isKnownDest(Result))
902 lengthArgPosHandle(1, LengthHandleKind::Increase, Result, Diag);
903
904 if (IsCopy)
905 removeArg(3, Result, Diag);
906
907 if (!IsCopy && !IsSafe)
908 insertNullTerminatorExpr(Name, Result, Diag);
909}
910
911void NotNullTerminatedResultCheck::memchrFix(
912 StringRef Name, const MatchFinder::MatchResult &Result) {
913 const auto *FunctionExpr = Result.Nodes.getNodeAs<CallExpr>(FunctionExprName);
914 if (const auto *GivenCL = dyn_cast<CharacterLiteral>(FunctionExpr->getArg(1)))
915 if (GivenCL->getValue() != 0)
916 return;
917
918 auto Diag = diag(FunctionExpr->getArg(2)->IgnoreParenCasts()->getBeginLoc(),
919 "the length is too short to include the null terminator");
920
921 if (const auto *CastExpr = Result.Nodes.getNodeAs<Expr>(CastExprName)) {
922 const auto CastRemoveFix = FixItHint::CreateRemoval(
923 SourceRange(CastExpr->getBeginLoc(),
924 FunctionExpr->getBeginLoc().getLocWithOffset(-1)));
925 Diag << CastRemoveFix;
926 }
927
928 const StringRef NewFuncName = (Name[0] != 'w') ? "strchr" : "wcschr";
929 renameFunc(NewFuncName, Result, Diag);
930 removeArg(2, Result, Diag);
931}
932
933void NotNullTerminatedResultCheck::memmoveFix(
934 StringRef Name, const MatchFinder::MatchResult &Result,
935 DiagnosticBuilder &Diag) const {
936 const bool IsOverflows = isDestCapacityFix(Result, Diag);
937
938 if (UseSafeFunctions && isKnownDest(Result)) {
939 renameFunc((Name[0] != 'w') ? "memmove_s" : "wmemmove_s", Result, Diag);
940 insertDestCapacityArg(IsOverflows, Name, Result, Diag);
941 }
942
943 lengthArgHandle(LengthHandleKind::Increase, Result, Diag);
944}
945
946void NotNullTerminatedResultCheck::strerrorSFix(
947 const MatchFinder::MatchResult &Result) {
948 auto Diag =
949 diag(Result.Nodes.getNodeAs<CallExpr>(FunctionExprName)->getBeginLoc(),
950 "the result from calling 'strerror_s' is not null-terminated and "
951 "missing the last character of the error message");
952
953 isDestCapacityFix(Result, Diag);
954 lengthArgHandle(LengthHandleKind::Increase, Result, Diag);
955}
956
957void NotNullTerminatedResultCheck::ncmpFix(
958 StringRef Name, const MatchFinder::MatchResult &Result) {
959 const auto *FunctionExpr = Result.Nodes.getNodeAs<CallExpr>(FunctionExprName);
960 const Expr *FirstArgExpr = FunctionExpr->getArg(0)->IgnoreImpCasts();
961 const Expr *SecondArgExpr = FunctionExpr->getArg(1)->IgnoreImpCasts();
962 bool IsLengthTooLong = false;
963
964 if (const CallExpr *StrlenExpr = getStrlenExpr(Result)) {
965 const Expr *LengthExprArg = StrlenExpr->getArg(0);
966 const StringRef FirstExprStr = exprToStr(FirstArgExpr, Result).trim();
967 const StringRef SecondExprStr = exprToStr(SecondArgExpr, Result).trim();
968 const StringRef LengthArgStr = exprToStr(LengthExprArg, Result).trim();
969 IsLengthTooLong =
970 LengthArgStr == FirstExprStr || LengthArgStr == SecondExprStr;
971 } else {
972 const int SrcLength =
973 getLength(Result.Nodes.getNodeAs<Expr>(SrcExprName), Result);
974 const int GivenLength = getGivenLength(Result);
975 if (SrcLength != 0 && GivenLength != 0)
976 IsLengthTooLong = GivenLength > SrcLength;
977 }
978
979 if (!IsLengthTooLong && !isStringDataAndLength(Result))
980 return;
981
982 auto Diag = diag(FunctionExpr->getArg(2)->IgnoreParenCasts()->getBeginLoc(),
983 "comparison length is too long and might lead to a "
984 "buffer overflow");
985
986 lengthArgHandle(LengthHandleKind::Decrease, Result, Diag);
987}
988
989void NotNullTerminatedResultCheck::xfrmFix(
990 StringRef Name, const MatchFinder::MatchResult &Result) {
991 if (!isDestCapacityOverflows(Result))
992 return;
993
994 auto Diag =
995 diag(Result.Nodes.getNodeAs<CallExpr>(FunctionExprName)->getBeginLoc(),
996 "the result from calling '%0' is not null-terminated")
997 << Name;
998
999 isDestCapacityFix(Result, Diag);
1000 lengthArgHandle(LengthHandleKind::Increase, Result, Diag);
1001}
1002
1003} // namespace clang::tidy::bugprone
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
void registerMatchers(ast_matchers::MatchFinder *Finder) override
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
NotNullTerminatedResultCheck(StringRef Name, ClangTidyContext *Context)
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
static bool isStringDataAndLength(const MatchFinder::MatchResult &Result)
static bool isCorrectGivenLength(const MatchFinder::MatchResult &Result)
static const CallExpr * getStrlenExpr(const MatchFinder::MatchResult &Result)
static void renameFunc(StringRef NewFuncName, const MatchFinder::MatchResult &Result, DiagnosticBuilder &Diag)
static bool isKnownDest(const MatchFinder::MatchResult &Result)
static bool isDestAndSrcEquals(const MatchFinder::MatchResult &Result)
static bool isDestExprFix(const MatchFinder::MatchResult &Result, DiagnosticBuilder &Diag)
static SourceLocation exprLocEnd(const Expr *E, const MatchFinder::MatchResult &Result)
static int getGivenLength(const MatchFinder::MatchResult &Result)
static bool isDestBasedOnGivenLength(const MatchFinder::MatchResult &Result)
static const Expr * getDestCapacityExpr(const MatchFinder::MatchResult &Result)
static bool isGivenLengthEqualToSrcLength(const MatchFinder::MatchResult &Result)
static int getDestCapacity(const MatchFinder::MatchResult &Result)
static bool isDestCapacityOverflows(const MatchFinder::MatchResult &Result)
static bool isInjectUL(const MatchFinder::MatchResult &Result)
static bool isFixedGivenLengthAndUnknownSrc(const MatchFinder::MatchResult &Result)
static void lengthExprHandle(const Expr *LengthExpr, LengthHandleKind LengthHandle, const MatchFinder::MatchResult &Result, DiagnosticBuilder &Diag)
static void lengthArgHandle(LengthHandleKind LengthHandle, const MatchFinder::MatchResult &Result, DiagnosticBuilder &Diag)
static void lengthArgPosHandle(unsigned ArgPos, LengthHandleKind LengthHandle, const MatchFinder::MatchResult &Result, DiagnosticBuilder &Diag)
static bool isDestCapacityFix(const MatchFinder::MatchResult &Result, DiagnosticBuilder &Diag)
static StringRef exprToStr(const Expr *E, const MatchFinder::MatchResult &Result)
static void insertNullTerminatorExpr(StringRef Name, const MatchFinder::MatchResult &Result, DiagnosticBuilder &Diag)
static void removeArg(int ArgPos, const MatchFinder::MatchResult &Result, DiagnosticBuilder &Diag)
static unsigned getLength(const Expr *E, const MatchFinder::MatchResult &Result)
static void insertDestCapacityArg(bool IsOverflows, StringRef Name, const MatchFinder::MatchResult &Result, DiagnosticBuilder &Diag)
static void renameMemcpy(StringRef Name, bool IsCopy, bool IsSafe, const MatchFinder::MatchResult &Result, DiagnosticBuilder &Diag)
llvm::StringMap< ClangTidyValue > OptionMap