[clang-tidy] MemsetZeroLenghtChecker: Don't crash trying to evaluate dependent values.

llvm-svn: 213238
This commit is contained in:
Benjamin Kramer 2014-07-17 08:56:55 +00:00
parent 80c2289a03
commit 01f5686298
2 changed files with 10 additions and 4 deletions

View File

@ -59,11 +59,13 @@ void MemsetZeroLengthCheck::check(const MatchFinder::MatchResult &Result) {
// Try to evaluate the second argument so we can also find values that are not
// just literals.
llvm::APSInt Value1, Value2;
if (!Arg2->EvaluateAsInt(Value2, *Result.Context) || Value2 != 0)
if (Arg2->isValueDependent() ||
!Arg2->EvaluateAsInt(Value2, *Result.Context) || Value2 != 0)
return;
// If both arguments evaluate to zero emit a warning without fix suggestions.
if (Arg1->EvaluateAsInt(Value1, *Result.Context) &&
if (!Arg1->isValueDependent() &&
Arg1->EvaluateAsInt(Value1, *Result.Context) &&
(Value1 == 0 || Value1.isNegative())) {
diag(Call->getLocStart(), "memset of size zero");
return;

View File

@ -7,9 +7,13 @@ namespace std {
using ::memset;
}
template <int i>
template <int i, typename T>
void memtmpl() {
memset(0, sizeof(int), i);
memset(0, sizeof(T), sizeof(T));
memset(0, sizeof(T), 0);
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: memset of size zero, potentially swapped argument
// CHECK-FIXES: memset(0, 0, sizeof(T));
memset(0, sizeof(int), 0);
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: memset of size zero, potentially swapped argument
// CHECK-FIXES: memset(0, 0, sizeof(int));
@ -53,5 +57,5 @@ void foo(void *a, int xsize, int ysize) {
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: memset of size zero
// CHECK-FIXES: memset(a, -1, v);
memtmpl<0>();
memtmpl<0, int>();
}