Author: matthewbg Date: Thu Aug 4 19:22:34 2011 New Revision: 136950 URL: http://llvm.org/viewvc/llvm-project?rev=136950&view=rev Log: Extend memset/memcpy/memmove checking to include memcmp
Modified: cfe/trunk/include/clang/Sema/Sema.h cfe/trunk/lib/Sema/SemaChecking.cpp cfe/trunk/test/SemaCXX/warn-memset-bad-sizeof.cpp Modified: cfe/trunk/include/clang/Sema/Sema.h URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/Sema.h?rev=136950&r1=136949&r2=136950&view=diff ============================================================================== --- cfe/trunk/include/clang/Sema/Sema.h (original) +++ cfe/trunk/include/clang/Sema/Sema.h Thu Aug 4 19:22:34 2011 @@ -5992,16 +5992,16 @@ bool isPrintf); /// \brief Enumeration used to describe which of the memory setting or copying - /// functions is being checked by \c CheckMemsetcpymoveArguments(). + /// functions is being checked by \c CheckMemaccessArguments(). enum CheckedMemoryFunction { CMF_Memset, CMF_Memcpy, - CMF_Memmove + CMF_Memmove, + CMF_Memcmp }; - void CheckMemsetcpymoveArguments(const CallExpr *Call, - CheckedMemoryFunction CMF, - IdentifierInfo *FnName); + void CheckMemaccessArguments(const CallExpr *Call, CheckedMemoryFunction CMF, + IdentifierInfo *FnName); void CheckReturnStackAddr(Expr *RetValExp, QualType lhsType, SourceLocation ReturnLoc); Modified: cfe/trunk/lib/Sema/SemaChecking.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=136950&r1=136949&r2=136950&view=diff ============================================================================== --- cfe/trunk/lib/Sema/SemaChecking.cpp (original) +++ cfe/trunk/lib/Sema/SemaChecking.cpp Thu Aug 4 19:22:34 2011 @@ -319,7 +319,7 @@ TheCall->getCallee()->getLocStart()); } - // Memset/memcpy/memmove handling + // Memset/memcpy/memmove/memcmp handling int CMF = -1; switch (FDecl->getBuiltinID()) { case Builtin::BI__builtin_memset: @@ -340,6 +340,10 @@ CMF = CMF_Memmove; break; + case Builtin::BI__builtin_memcmp: + CMF = CMF_Memcmp; + break; + default: if (FDecl->getLinkage() == ExternalLinkage && (!getLangOptions().CPlusPlus || FDecl->isExternC())) { @@ -349,12 +353,14 @@ CMF = CMF_Memcpy; else if (FnInfo->isStr("memmove")) CMF = CMF_Memmove; + else if (FnInfo->isStr("memcmp")) + CMF = CMF_Memcmp; } break; } if (CMF != -1) - CheckMemsetcpymoveArguments(TheCall, CheckedMemoryFunction(CMF), FnInfo); + CheckMemaccessArguments(TheCall, CheckedMemoryFunction(CMF), FnInfo); return false; } @@ -1881,12 +1887,13 @@ /// \brief Check for dangerous or invalid arguments to memset(). /// /// This issues warnings on known problematic, dangerous or unspecified -/// arguments to the standard 'memset', 'memcpy', and 'memmove' function calls. +/// arguments to the standard 'memset', 'memcpy', 'memmove', and 'memcmp' +/// function calls. /// /// \param Call The call expression to diagnose. -void Sema::CheckMemsetcpymoveArguments(const CallExpr *Call, - CheckedMemoryFunction CMF, - IdentifierInfo *FnName) { +void Sema::CheckMemaccessArguments(const CallExpr *Call, + CheckedMemoryFunction CMF, + IdentifierInfo *FnName) { // It is possible to have a non-standard definition of memset. Validate // we have enough arguments, and if not, abort further checking. if (Call->getNumArgs() < 3) Modified: cfe/trunk/test/SemaCXX/warn-memset-bad-sizeof.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-memset-bad-sizeof.cpp?rev=136950&r1=136949&r2=136950&view=diff ============================================================================== --- cfe/trunk/test/SemaCXX/warn-memset-bad-sizeof.cpp (original) +++ cfe/trunk/test/SemaCXX/warn-memset-bad-sizeof.cpp Thu Aug 4 19:22:34 2011 @@ -3,6 +3,7 @@ extern "C" void *memset(void *, int, unsigned); extern "C" void *memmove(void *s1, const void *s2, unsigned n); extern "C" void *memcpy(void *s1, const void *s2, unsigned n); +extern "C" void *memcmp(void *s1, const void *s2, unsigned n); struct S {int a, b, c, d;}; typedef S* PS; @@ -51,6 +52,11 @@ memcpy(0, &s, sizeof(&s)); // \ // expected-warning {{argument to 'sizeof' in 'memcpy' call is the same expression as the source}} + memmove(ps, 0, sizeof(ps)); // \ + // expected-warning {{argument to 'sizeof' in 'memmove' call is the same expression as the destination}} + memcmp(ps, 0, sizeof(ps)); // \ + // expected-warning {{argument to 'sizeof' in 'memcmp' call is the same expression as the destination}} + /* Shouldn't warn */ memset((void*)&s, 0, sizeof(&s)); memset(&s, 0, sizeof(s)); @@ -99,3 +105,10 @@ memcpy(&foo, &arr, sizeof(Foo)); memcpy(&arr, &foo, sizeof(Foo)); } + +namespace ns { +void memset(void* s, char c, int n); +void f(int* i) { + memset(i, 0, sizeof(i)); +} +} _______________________________________________ cfe-commits mailing list cfe-commits@cs.uiuc.edu http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits