Skip to content

Commit 7bb2d46

Browse files
committed
[analyzer] CStringSyntaxChecks: Fix an off-by-one error in the strlcat() check.
oth strlcat and strlcpy cut off their safe bound for the argument value at sizeof(destination). There's no need to subtract 1 in only one of these cases. Differential Revision: https://reviews.llvm.org/D57981 rdar://problem/47873212 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@353583 91177308-0d34-0410-b5e6-96231b3b80d8 (cherry picked from commit 5bcf852d505e6e3d6d965f18c5fcd72ff5ef5a06) apple-llvm-split-commit: 72dde1b133bbb481209f091451762835c8d0e849 apple-llvm-split-dir: clang/
1 parent dea440c commit 7bb2d46

File tree

2 files changed

+4
-9
lines changed

2 files changed

+4
-9
lines changed

clang/lib/StaticAnalyzer/Checkers/CStringSyntaxChecker.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,6 @@ bool WalkAST::containsBadStrncatPattern(const CallExpr *CE) {
154154
bool WalkAST::containsBadStrlcpyStrlcatPattern(const CallExpr *CE) {
155155
if (CE->getNumArgs() != 3)
156156
return false;
157-
const FunctionDecl *FD = CE->getDirectCallee();
158-
bool Append = CheckerContext::isCLibraryFunction(FD, "strlcat");
159157
const Expr *DstArg = CE->getArg(0);
160158
const Expr *LenArg = CE->getArg(2);
161159

@@ -195,13 +193,8 @@ bool WalkAST::containsBadStrlcpyStrlcatPattern(const CallExpr *CE) {
195193
ASTContext &C = BR.getContext();
196194
uint64_t BufferLen = C.getTypeSize(Buffer) / 8;
197195
auto RemainingBufferLen = BufferLen - DstOff;
198-
if (Append) {
199-
if (RemainingBufferLen <= ILRawVal)
200-
return true;
201-
} else {
202-
if (RemainingBufferLen < ILRawVal)
203-
return true;
204-
}
196+
if (RemainingBufferLen < ILRawVal)
197+
return true;
205198
}
206199
}
207200
}

clang/test/Analysis/cstring-syntax.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ void testStrlcpy(const char *src) {
3333
strlcpy(dest, src, ulen);
3434
strlcpy(dest + 5, src, 5);
3535
strlcpy(dest + 5, src, 10); // expected-warning {{The third argument allows to potentially copy more bytes than it should. Replace with the value sizeof(<destination buffer>) or lower}}
36+
strlcpy(dest, "aaaaaaaaaaaaaaa", 10); // no-warning
3637
}
3738

3839
void testStrlcat(const char *src) {
@@ -51,4 +52,5 @@ void testStrlcat(const char *src) {
5152
strlcat(dest, src, ulen);
5253
strlcpy(dest, src, 5);
5354
strlcat(dest + 5, src, badlen); // expected-warning {{The third argument allows to potentially copy more bytes than it should. Replace with the value sizeof(<destination buffer>) or lower}}
55+
strlcat(dest, "aaaaaaaaaaaaaaa", 10); // no-warning
5456
}

0 commit comments

Comments
 (0)