diff --git a/clang-tools-extra/clang-tidy/bugprone/UndefinedMemoryManipulationCheck.h b/clang-tools-extra/clang-tidy/bugprone/UndefinedMemoryManipulationCheck.h index 1050d39130610..5e2d7d8ce48ec 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 2735184507bbf..bad1f6d0a8615 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 + #include + + 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; + }