[analyzer] [PR39792] false positive on strcpy targeting struct members

Patch by Pierre van Houtryve.

Differential Revision: https://reviews.llvm.org/D55226

llvm-svn: 351097
This commit is contained in:
George Karpenkov 2019-01-14 18:54:48 +00:00
parent 704913f562
commit e2a8eec457
2 changed files with 12 additions and 7 deletions

View File

@ -651,8 +651,8 @@ void WalkAST::checkCall_strcpy(const CallExpr *CE, const FunctionDecl *FD) {
const auto *Target = CE->getArg(0)->IgnoreImpCasts(),
*Source = CE->getArg(1)->IgnoreImpCasts();
if (const auto *DeclRef = dyn_cast<DeclRefExpr>(Target))
if (const auto *Array = dyn_cast<ConstantArrayType>(DeclRef->getType())) {
if (const auto *Array = dyn_cast<ConstantArrayType>(Target->getType())) {
uint64_t ArraySize = BR.getContext().getTypeSize(Array) / 8;
if (const auto *String = dyn_cast<StringLiteral>(Source)) {
if (ArraySize >= String->getLength() + 1)

View File

@ -177,6 +177,11 @@ void test_strcpy_safe() {
strcpy(x, "abcd");
}
void test_strcpy_safe_2() {
struct {char s1[100];} s;
strcpy(s.s1, "hello");
}
//===----------------------------------------------------------------------===
// strcat()
//===----------------------------------------------------------------------===