From: Endre Fülöp Date: Sun, 22 May 2022 21:28:10 +0000 (+0200) Subject: [clang-tidy] Extend cert-oop57-cpp to check non-zero memset values X-Git-Tag: upstream/15.0.7~6244 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=e290fcef111c24ed833c9768809eb900a21c94e0;p=platform%2Fupstream%2Fllvm.git [clang-tidy] Extend cert-oop57-cpp to check non-zero memset values Clang Tidy check cert-oop57-cpp now checks for arbitrary-valued arguments in memset expressions containing non-trivially default-constructible instances. Previously it only checked literal 0 values. Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D126186 --- diff --git a/clang-tools-extra/clang-tidy/cert/NonTrivialTypesLibcMemoryCallsCheck.cpp b/clang-tools-extra/clang-tidy/cert/NonTrivialTypesLibcMemoryCallsCheck.cpp index 8e6acea..5c8da5b 100644 --- a/clang-tools-extra/clang-tidy/cert/NonTrivialTypesLibcMemoryCallsCheck.cpp +++ b/clang-tools-extra/clang-tidy/cert/NonTrivialTypesLibcMemoryCallsCheck.cpp @@ -80,7 +80,7 @@ void NonTrivialTypesLibcMemoryCallsCheck::registerMatchers( auto IsRecordSizeOf = expr(sizeOfExpr(hasArgumentOfType(equalsBoundNode("Record")))); auto ArgChecker = [&](Matcher RecordConstraint, - BindableMatcher SecondArg) { + BindableMatcher SecondArg = expr()) { return allOf(argumentCountIs(3), hasArgument(0, IsStructPointer(RecordConstraint, true)), hasArgument(1, SecondArg), hasArgument(2, IsRecordSizeOf)); @@ -89,8 +89,7 @@ void NonTrivialTypesLibcMemoryCallsCheck::registerMatchers( Finder->addMatcher( callExpr(callee(namedDecl(hasAnyName( utils::options::parseListPair(BuiltinMemSet, MemSetNames)))), - ArgChecker(unless(isTriviallyDefaultConstructible()), - expr(integerLiteral(equals(0))))) + ArgChecker(unless(isTriviallyDefaultConstructible()))) .bind("lazyConstruct"), this); Finder->addMatcher( diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 5196c53..c709f6d 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -156,6 +156,9 @@ Changes in existing checks ` when `sizeof(...)` is compared against a `__int128_t`. +- Made :doc:`cert-oop57-cpp ` more sensitive + by checking for an arbitrary expression in the second argument of ``memset``. + - Improved :doc:`cppcoreguidelines-prefer-member-initializer ` check. diff --git a/clang-tools-extra/test/clang-tidy/checkers/cert-oop57-cpp.cpp b/clang-tools-extra/test/clang-tidy/checkers/cert-oop57-cpp.cpp index a5a3873..880c5c3 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/cert-oop57-cpp.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/cert-oop57-cpp.cpp @@ -88,3 +88,17 @@ void baz(const NonTrivial &Other) { mymemcmp(&Data, &Other, sizeof(Data)); // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: consider using comparison operators instead of calling 'mymemcmp' } + +void nonNullSetValue() { + NonTrivial Data; + // Check non-null-valued second argument. + std::memset(&Data, 1, sizeof(Data)); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: calling 'memset' on a non-trivially default constructible class is undefined +} + +void nonLiteralSetValue(char C) { + NonTrivial Data; + // Check non-literal second argument. + std::memset(&Data, C, sizeof(Data)); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: calling 'memset' on a non-trivially default constructible class is undefined +}