[clang-tools-extra] 5eaa531 - [clang-tidy][NFC] Update documentation for bugprone-undefined-memory-manipulation
Piotr Zegar via cfe-commits
cfe-commits at lists.llvm.org
Wed Sep 20 14:01:53 PDT 2023
Author: Piotr Zegar
Date: 2023-09-20T21:01:34Z
New Revision: 5eaa5312e7943e23155da4f0fbf07b55a200fc60
URL: https://github.com/llvm/llvm-project/commit/5eaa5312e7943e23155da4f0fbf07b55a200fc60
DIFF: https://github.com/llvm/llvm-project/commit/5eaa5312e7943e23155da4f0fbf07b55a200fc60.diff
LOG: [clang-tidy][NFC] Update documentation for bugprone-undefined-memory-manipulation
Add example and more information to check documentation.
Added:
Modified:
clang-tools-extra/clang-tidy/bugprone/UndefinedMemoryManipulationCheck.h
clang-tools-extra/docs/clang-tidy/checks/bugprone/undefined-memory-manipulation.rst
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/bugprone/UndefinedMemoryManipulationCheck.h b/clang-tools-extra/clang-tidy/bugprone/UndefinedMemoryManipulationCheck.h
index 1050d3913061056..5e2d7d8ce48ec58 100644
--- a/clang-tools-extra/clang-tidy/bugprone/UndefinedMemoryManipulationCheck.h
+++ b/clang-tools-extra/clang-tidy/bugprone/UndefinedMemoryManipulationCheck.h
@@ -14,7 +14,7 @@
namespace clang::tidy::bugprone {
/// Finds calls of memory manipulation functions ``memset()``, ``memcpy()`` and
-/// ``memmove()`` on not TriviallyCopyable objects resulting in undefined
+/// ``memmove()`` on non-TriviallyCopyable objects resulting in undefined
/// behavior.
///
/// For the user-facing documentation see:
diff --git a/clang-tools-extra/docs/clang-tidy/checks/bugprone/undefined-memory-manipulation.rst b/clang-tools-extra/docs/clang-tidy/checks/bugprone/undefined-memory-manipulation.rst
index 2735184507bbfed..bad1f6d0a86159d 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/bugprone/undefined-memory-manipulation.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/undefined-memory-manipulation.rst
@@ -4,4 +4,31 @@ bugprone-undefined-memory-manipulation
======================================
Finds calls of memory manipulation functions ``memset()``, ``memcpy()`` and
-``memmove()`` on not TriviallyCopyable objects resulting in undefined behavior.
+``memmove()`` on non-TriviallyCopyable objects resulting in undefined behavior.
+
+Using memory manipulation functions on non-TriviallyCopyable objects can lead
+to a range of subtle and challenging issues in C++ code. The most immediate
+concern is the potential for undefined behavior, where the state of the object
+may become corrupted or invalid. This can manifest as crashes, data corruption,
+or unexpected behavior at runtime, making it challenging to identify and
+diagnose the root cause. Additionally, misuse of memory manipulation functions
+can bypass essential object-specific operations, such as constructors and
+destructors, leading to resource leaks or improper initialization.
+
+For example, when using ``memcpy`` to copy ``std::string``, pointer data is
+being copied, and it can result in a double free issue.
+
+.. code-block:: c++
+
+ #include <cstring>
+ #include <string>
+
+ int main() {
+ std::string source = "Hello";
+ std::string destination;
+
+ std::memcpy(&destination, &source, sizeof(std::string));
+
+ // Undefined behavior may occur here, during std::string destructor call.
+ return 0;
+ }
More information about the cfe-commits
mailing list