Skip to content

Commit

Permalink
Emit -Wfortify-source using DiagRuntimeBehaviour
Browse files Browse the repository at this point in the history
This fixes a false positive on the following, where st is configured to have
different sizes based on some preprocessor logic:

  if (sizeof(buf) == sizeof(*st))
    memcpy(&buf, st, sizeof(*st));

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@357041 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
epilk committed Mar 26, 2019
1 parent bec53e9 commit 92053fe
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/Sema/SemaChecking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,9 +431,10 @@ void Sema::checkFortifiedBuiltinMemoryFunction(FunctionDecl *FD,
FunctionName = FunctionName.drop_front(std::strlen("__builtin_"));
}

Diag(TheCall->getBeginLoc(), DiagID)
<< FunctionName << ObjectSize.toString(/*Radix=*/10)
<< UsedSize.toString(/*Radix=*/10);
DiagRuntimeBehavior(TheCall->getBeginLoc(), TheCall,
PDiag(DiagID)
<< FunctionName << ObjectSize.toString(/*Radix=*/10)
<< UsedSize.toString(/*Radix=*/10));
}

static bool SemaBuiltinSEHScopeCheck(Sema &SemaRef, CallExpr *TheCall,
Expand Down
3 changes: 3 additions & 0 deletions test/Sema/warn-fortify-source.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ void call_memcpy() {
char dst[10];
char src[20];
memcpy(dst, src, 20); // expected-warning {{memcpy' will always overflow; destination buffer has size 10, but size argument is 20}}

if (sizeof(dst) == sizeof(src))
memcpy(dst, src, 20); // no warning, unreachable
}

void call_memcpy_type() {
Expand Down

0 comments on commit 92053fe

Please sign in to comment.