Skip to content

Commit 5eaa531

Browse files
committed
[clang-tidy][NFC] Update documentation for bugprone-undefined-memory-manipulation
Add example and more information to check documentation.
1 parent 93e12f1 commit 5eaa531

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

clang-tools-extra/clang-tidy/bugprone/UndefinedMemoryManipulationCheck.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
namespace clang::tidy::bugprone {
1515

1616
/// Finds calls of memory manipulation functions ``memset()``, ``memcpy()`` and
17-
/// ``memmove()`` on not TriviallyCopyable objects resulting in undefined
17+
/// ``memmove()`` on non-TriviallyCopyable objects resulting in undefined
1818
/// behavior.
1919
///
2020
/// For the user-facing documentation see:

clang-tools-extra/docs/clang-tidy/checks/bugprone/undefined-memory-manipulation.rst

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,31 @@ bugprone-undefined-memory-manipulation
44
======================================
55

66
Finds calls of memory manipulation functions ``memset()``, ``memcpy()`` and
7-
``memmove()`` on not TriviallyCopyable objects resulting in undefined behavior.
7+
``memmove()`` on non-TriviallyCopyable objects resulting in undefined behavior.
8+
9+
Using memory manipulation functions on non-TriviallyCopyable objects can lead
10+
to a range of subtle and challenging issues in C++ code. The most immediate
11+
concern is the potential for undefined behavior, where the state of the object
12+
may become corrupted or invalid. This can manifest as crashes, data corruption,
13+
or unexpected behavior at runtime, making it challenging to identify and
14+
diagnose the root cause. Additionally, misuse of memory manipulation functions
15+
can bypass essential object-specific operations, such as constructors and
16+
destructors, leading to resource leaks or improper initialization.
17+
18+
For example, when using ``memcpy`` to copy ``std::string``, pointer data is
19+
being copied, and it can result in a double free issue.
20+
21+
.. code-block:: c++
22+
23+
#include <cstring>
24+
#include <string>
25+
26+
int main() {
27+
std::string source = "Hello";
28+
std::string destination;
29+
30+
std::memcpy(&destination, &source, sizeof(std::string));
31+
32+
// Undefined behavior may occur here, during std::string destructor call.
33+
return 0;
34+
}

0 commit comments

Comments
 (0)