summaryrefslogtreecommitdiffstats
path: root/clang/lib/StaticAnalyzer/Checkers/CStringSyntaxChecker.cpp
diff options
context:
space:
mode:
authorGeorge Karpenkov <ekarpenkov@apple.com>2018-08-23 00:02:35 +0000
committerGeorge Karpenkov <ekarpenkov@apple.com>2018-08-23 00:02:35 +0000
commitc433011e02d44ed31c20761ce897faea535bec31 (patch)
tree84177d1fbbe54b871b60fd8360cfacfdea63d0ab /clang/lib/StaticAnalyzer/Checkers/CStringSyntaxChecker.cpp
parentb45bf3bb8b5fb4a4fe6634a22f64b30428589644 (diff)
downloadbcm5719-llvm-c433011e02d44ed31c20761ce897faea535bec31.tar.gz
bcm5719-llvm-c433011e02d44ed31c20761ce897faea535bec31.zip
Revert "[CStringSyntaxChecker] Check strlcat sizeof check"
This reverts commit 3073790e87378fea9a68fb052185fec9596ef135. The check is not correct, strlact(dest, "mystr", sizeof(dest)) is fine. llvm-svn: 340501
Diffstat (limited to 'clang/lib/StaticAnalyzer/Checkers/CStringSyntaxChecker.cpp')
-rw-r--r--clang/lib/StaticAnalyzer/Checkers/CStringSyntaxChecker.cpp54
1 files changed, 4 insertions, 50 deletions
diff --git a/clang/lib/StaticAnalyzer/Checkers/CStringSyntaxChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/CStringSyntaxChecker.cpp
index 2763408face..8b4aa857e77 100644
--- a/clang/lib/StaticAnalyzer/Checkers/CStringSyntaxChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/CStringSyntaxChecker.cpp
@@ -90,16 +90,7 @@ class WalkAST: public StmtVisitor<WalkAST> {
/// strlcpy(dst, "abcd", 4);
/// strlcpy(dst + 3, "abcd", 2);
/// strlcpy(dst, "abcd", cpy);
- /// Identify erroneous patterns in the last argument to strlcat - the number
- /// of bytes to copy.
- /// The bad pattern checked is when the last argument is basically
- /// pointing to the destination buffer size or argument larger or
- /// equal to.
- /// char dst[2];
- /// strlcat(dst, src2, sizeof(dst));
- /// strlcat(dst, src2, 2);
- /// strlcat(dst, src2, 10);
- bool containsBadStrlcpyStrlcatPattern(const CallExpr *CE);
+ bool containsBadStrlcpyPattern(const CallExpr *CE);
public:
WalkAST(const CheckerBase *Checker, BugReporter &BR, AnalysisDeclContext *AC)
@@ -151,21 +142,15 @@ bool WalkAST::containsBadStrncatPattern(const CallExpr *CE) {
return false;
}
-bool WalkAST::containsBadStrlcpyStrlcatPattern(const CallExpr *CE) {
+bool WalkAST::containsBadStrlcpyPattern(const CallExpr *CE) {
if (CE->getNumArgs() != 3)
return false;
- const FunctionDecl *FD = CE->getDirectCallee();
- bool Append = CheckerContext::isCLibraryFunction(FD, "strlcat");
const Expr *DstArg = CE->getArg(0);
const Expr *LenArg = CE->getArg(2);
const auto *DstArgDecl = dyn_cast<DeclRefExpr>(DstArg->IgnoreParenImpCasts());
const auto *LenArgDecl = dyn_cast<DeclRefExpr>(LenArg->IgnoreParenLValueCasts());
uint64_t DstOff = 0;
- // - sizeof(dst)
- // strlcat appends at most size - strlen(dst) - 1
- if (Append && isSizeof(LenArg, DstArg))
- return true;
// - size_t dstlen = sizeof(dst)
if (LenArgDecl) {
const auto *LenArgVal = dyn_cast<VarDecl>(LenArgDecl->getDecl());
@@ -196,10 +181,7 @@ bool WalkAST::containsBadStrlcpyStrlcatPattern(const CallExpr *CE) {
if (const auto *Buffer = dyn_cast<ConstantArrayType>(DstArgDecl->getType())) {
ASTContext &C = BR.getContext();
uint64_t BufferLen = C.getTypeSize(Buffer) / 8;
- auto RemainingBufferLen = BufferLen - DstOff;
- if (Append)
- RemainingBufferLen -= 1;
- if (RemainingBufferLen < ILRawVal)
+ if ((BufferLen - DstOff) < ILRawVal)
return true;
}
}
@@ -238,7 +220,7 @@ void WalkAST::VisitCallExpr(CallExpr *CE) {
LenArg->getSourceRange());
}
} else if (CheckerContext::isCLibraryFunction(FD, "strlcpy")) {
- if (containsBadStrlcpyStrlcatPattern(CE)) {
+ if (containsBadStrlcpyPattern(CE)) {
const Expr *DstArg = CE->getArg(0);
const Expr *LenArg = CE->getArg(2);
PathDiagnosticLocation Loc =
@@ -256,34 +238,6 @@ void WalkAST::VisitCallExpr(CallExpr *CE) {
"C String API", os.str(), Loc,
LenArg->getSourceRange());
}
- } else if (CheckerContext::isCLibraryFunction(FD, "strlcat")) {
- if (containsBadStrlcpyStrlcatPattern(CE)) {
- const Expr *DstArg = CE->getArg(0);
- const Expr *LenArg = CE->getArg(2);
- PathDiagnosticLocation Loc =
- PathDiagnosticLocation::createBegin(LenArg, BR.getSourceManager(), AC);
-
- StringRef DstName = getPrintableName(DstArg);
- StringRef LenName = getPrintableName(LenArg);
-
- SmallString<256> S;
- llvm::raw_svector_ostream os(S);
- os << "The third argument allows to potentially copy more bytes than it should. ";
- os << "Replace with the value ";
- if (!LenName.empty())
- os << "'" << LenName << "'";
- else
- os << " <size> ";
- if (!DstName.empty())
- os << " - strlen(" << DstName << ")";
- else
- os << " - strlen(<destination buffer>)";
- os << " - 1 or lower";
-
- BR.EmitBasicReport(FD, Checker, "Anti-pattern in the argument",
- "C String API", os.str(), Loc,
- LenArg->getSourceRange());
- }
}
// Recurse and check children.
OpenPOWER on IntegriCloud