[PATCH] D127036: [clang-tidy] Warn about arrays in `bugprone-undefined-memory-manipulation`

Fabian Wolff via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jun 3 16:54:51 PDT 2022


fwolff created this revision.
fwolff added reviewers: aaron.ballman, njames93, alexfh.
fwolff added a project: clang-tools-extra.
Herald added subscribers: carlosgalvezp, xazax.hun.
Herald added a project: All.
fwolff requested review of this revision.
Herald added a subscriber: cfe-commits.

Fixes issue #55579 <https://github.com/llvm/llvm-project/issues/55579>.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D127036

Files:
  clang-tools-extra/clang-tidy/bugprone/UndefinedMemoryManipulationCheck.cpp
  clang-tools-extra/test/clang-tidy/checkers/bugprone-undefined-memory-manipulation.cpp


Index: clang-tools-extra/test/clang-tidy/checkers/bugprone-undefined-memory-manipulation.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/bugprone-undefined-memory-manipulation.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/bugprone-undefined-memory-manipulation.cpp
@@ -126,6 +126,12 @@
   ::memmove(&p, &vb, sizeof(int));
   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: undefined behavior, source object type 'types::VirtualBase'
 
+  types::Copy ca[10];
+  memset(ca, 0, sizeof(ca));
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: undefined behavior, destination object type 'types::Copy[10]'
+  memset(&ca, 0, sizeof(ca));
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: undefined behavior, destination object type 'types::Copy[10]'
+
 #define MEMSET memset(&vf, 0, sizeof(int));
   MEMSET
   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: undefined behavior, destination object type 'types::VirtualFunc'
@@ -159,6 +165,13 @@
   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: undefined behavior, destination object type 'aliases::Copy2'
   memset(pc3, 0, sizeof(int));
   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: undefined behavior, destination object type 'Copy3'
+
+  using Copy3Arr = Copy3[5];
+  Copy3Arr c3a;
+  memset(c3a, 0, sizeof(c3a));
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: undefined behavior, destination object type 'Copy3Arr'
+  memset(&c3a, 0, sizeof(c3a));
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: undefined behavior, destination object type 'Copy3Arr'
 }
 
 void triviallyCopyable() {
Index: clang-tools-extra/clang-tidy/bugprone/UndefinedMemoryManipulationCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/bugprone/UndefinedMemoryManipulationCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/UndefinedMemoryManipulationCheck.cpp
@@ -24,9 +24,13 @@
 } // namespace
 
 void UndefinedMemoryManipulationCheck::registerMatchers(MatchFinder *Finder) {
-  const auto NotTriviallyCopyableObject =
-      hasType(ast_matchers::hasCanonicalType(
-          pointsTo(cxxRecordDecl(isNotTriviallyCopyable()))));
+  const auto ArrayOfNotTriviallyCopyable = arrayType(
+      hasElementType(hasDeclaration(cxxRecordDecl(isNotTriviallyCopyable()))));
+  const auto NotTriviallyCopyableObject = hasType(hasCanonicalType(
+      anyOf(pointsTo(qualType(
+                anyOf(hasDeclaration(cxxRecordDecl(isNotTriviallyCopyable())),
+                      ArrayOfNotTriviallyCopyable))),
+            ArrayOfNotTriviallyCopyable)));
 
   // Check whether destination object is not TriviallyCopyable.
   // Applicable to all three memory manipulation functions.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D127036.434206.patch
Type: text/x-patch
Size: 2713 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220603/7a754da1/attachment.bin>


More information about the cfe-commits mailing list