Skip to content

Commit e2a8eec

Browse files
author
George Karpenkov
committedJan 14, 2019
[analyzer] [PR39792] false positive on strcpy targeting struct members
Patch by Pierre van Houtryve. Differential Revision: https://reviews.llvm.org/D55226 llvm-svn: 351097
1 parent 704913f commit e2a8eec

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed
 

‎clang/lib/StaticAnalyzer/Checkers/CheckSecuritySyntaxOnly.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -651,14 +651,14 @@ void WalkAST::checkCall_strcpy(const CallExpr *CE, const FunctionDecl *FD) {
651651

652652
const auto *Target = CE->getArg(0)->IgnoreImpCasts(),
653653
*Source = CE->getArg(1)->IgnoreImpCasts();
654-
if (const auto *DeclRef = dyn_cast<DeclRefExpr>(Target))
655-
if (const auto *Array = dyn_cast<ConstantArrayType>(DeclRef->getType())) {
656-
uint64_t ArraySize = BR.getContext().getTypeSize(Array) / 8;
657-
if (const auto *String = dyn_cast<StringLiteral>(Source)) {
658-
if (ArraySize >= String->getLength() + 1)
659-
return;
660-
}
654+
655+
if (const auto *Array = dyn_cast<ConstantArrayType>(Target->getType())) {
656+
uint64_t ArraySize = BR.getContext().getTypeSize(Array) / 8;
657+
if (const auto *String = dyn_cast<StringLiteral>(Source)) {
658+
if (ArraySize >= String->getLength() + 1)
659+
return;
661660
}
661+
}
662662

663663
// Issue a warning.
664664
PathDiagnosticLocation CELoc =

‎clang/test/Analysis/security-syntax-checks.m

+5
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,11 @@ void test_strcpy_safe() {
177177
strcpy(x, "abcd");
178178
}
179179

180+
void test_strcpy_safe_2() {
181+
struct {char s1[100];} s;
182+
strcpy(s.s1, "hello");
183+
}
184+
180185
//===----------------------------------------------------------------------===
181186
// strcat()
182187
//===----------------------------------------------------------------------===

0 commit comments

Comments
 (0)