aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKristof Umann <dkszelethus@gmail.com>2019-08-15 08:52:10 +0000
committerKristof Umann <dkszelethus@gmail.com>2019-08-15 08:52:10 +0000
commite6e133b7007635b7445c7d15806e9e418f38866c (patch)
tree670a85fae38dd6118bd0677e7b12f2a65f2e94a2
parent1bd898989ca9259ae0d2bd11eb204478987081c7 (diff)
downloadllvm-e6e133b7007635b7445c7d15806e9e418f38866c.zip
llvm-e6e133b7007635b7445c7d15806e9e418f38866c.tar.gz
llvm-e6e133b7007635b7445c7d15806e9e418f38866c.tar.bz2
[analyzer] Add docs for cplusplus.InnerPointer
Differential Revision: https://reviews.llvm.org/D60281 llvm-svn: 368979
-rw-r--r--clang/docs/analyzer/checkers.rst29
1 files changed, 27 insertions, 2 deletions
diff --git a/clang/docs/analyzer/checkers.rst b/clang/docs/analyzer/checkers.rst
index e5b60bb..f50fff9 100644
--- a/clang/docs/analyzer/checkers.rst
+++ b/clang/docs/analyzer/checkers.rst
@@ -242,10 +242,35 @@ C++ Checkers.
.. _cplusplus-InnerPointer:
-cplusplus.InnerPointer
-""""""""""""""""""""""
+cplusplus.InnerPointer (C++)
+""""""""""""""""""""""""""""
Check for inner pointers of C++ containers used after re/deallocation.
+Many container methods in the C++ standard library are known to invalidate
+"references" (including actual references, iterators and raw pointers) to
+elements of the container. Using such references after they are invalidated
+causes undefined behavior, which is a common source of memory errors in C++ that
+this checker is capable of finding.
+
+The checker is currently limited to ``std::string`` objects and doesn't
+recognize some of the more sophisticated approaches to passing unowned pointers
+around, such as ``std::string_view``.
+
+.. code-block:: cpp
+
+ void deref_after_assignment() {
+ std::string s = "llvm";
+ const char *c = s.data(); // note: pointer to inner buffer of 'std::string' obtained here
+ s = "clang"; // note: inner buffer of 'std::string' reallocated by call to 'operator='
+ consume(c); // warn: inner pointer of container used after re/deallocation
+ }
+
+ const char *return_temp(int x) {
+ return std::to_string(x).c_str(); // warn: inner pointer of container used after re/deallocation
+ // note: pointer to inner buffer of 'std::string' obtained here
+ // note: inner buffer of 'std::string' deallocated by call to destructor
+ }
+
.. _cplusplus-NewDelete:
cplusplus.NewDelete (C++)