summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/docs/clang-tidy/checks/misc-string-literal-with-embedded-nul.rst
diff options
context:
space:
mode:
authorEtienne Bergeron <etienneb@google.com>2016-04-07 16:16:36 +0000
committerEtienne Bergeron <etienneb@google.com>2016-04-07 16:16:36 +0000
commita5fd19ba1e6a50c49ea8be34caa6b398f7f08546 (patch)
treea10daf7ada942faf021f0dcfa378412b1f0c59b8 /clang-tools-extra/docs/clang-tidy/checks/misc-string-literal-with-embedded-nul.rst
parent3802c4af596d1e708a999ddb3e965e9b60820ad6 (diff)
downloadbcm5719-llvm-a5fd19ba1e6a50c49ea8be34caa6b398f7f08546.tar.gz
bcm5719-llvm-a5fd19ba1e6a50c49ea8be34caa6b398f7f08546.zip
[clang-tidy] add new checker for string literal with NUL character.
Summary: This patch adds the support for detecting suspicious string literals and their //incorrect// usage. The following example shows a incorrect character escaping leading to an embedded NUL character. ``` std::string str = "\0x42"; // Should be "\x42". ``` The patch also add detection of truncated literal when a literal is passed to a string constructor. Reviewers: hokein, alexfh Subscribers: LegalizeAdulthood, bcraig, Eugene.Zelenko, bkramer, cfe-commits Differential Revision: http://reviews.llvm.org/D18783 llvm-svn: 265691
Diffstat (limited to 'clang-tools-extra/docs/clang-tidy/checks/misc-string-literal-with-embedded-nul.rst')
-rw-r--r--clang-tools-extra/docs/clang-tidy/checks/misc-string-literal-with-embedded-nul.rst38
1 files changed, 38 insertions, 0 deletions
diff --git a/clang-tools-extra/docs/clang-tidy/checks/misc-string-literal-with-embedded-nul.rst b/clang-tools-extra/docs/clang-tidy/checks/misc-string-literal-with-embedded-nul.rst
new file mode 100644
index 00000000000..3661218acef
--- /dev/null
+++ b/clang-tools-extra/docs/clang-tidy/checks/misc-string-literal-with-embedded-nul.rst
@@ -0,0 +1,38 @@
+.. title:: clang-tidy - misc-string-literal-with-embedded-nul
+
+misc-string-literal-with-embedded-nul
+=====================================
+
+Finds occurences of string literal with embedded NUL character and validates
+their usage.
+
+
+Invalid escaping
+^^^^^^^^^^^^^^^^
+
+Special characters can be escaped within a string literal by using their
+hexadecimal encoding like ``\x42``. A common mistake is to escape them
+like this ``\0x42`` where the ``\0`` stands for the NUL character.
+
+.. code:: c++
+
+ const char* Example[] = "Invalid character: \0x12 should be \x12";
+ const char* Bytes[] = "\x03\0x02\0x01\0x00\0xFF\0xFF\0xFF";
+
+
+Truncated literal
+^^^^^^^^^^^^^^^^^
+
+String-like classes can manipulate strings with embedded NUL as they are
+keeping track of the bytes and the length. This is not the case for a
+``char*`` (NUL-terminated) string.
+
+A common mistake is to pass a string-literal with embedded NUL to a string
+constructor expecting a NUL-terminated string. The bytes after the first NUL
+character are truncated.
+
+.. code:: c++
+
+ std::string str("abc\0def"); // "def" is truncated
+ str += "\0"; // This statement is doing nothing
+ if (str == "\0abc") return; // This expression is always true
OpenPOWER on IntegriCloud