37 lines · plain
1.. title:: clang-tidy - bugprone-string-literal-with-embedded-nul2 3bugprone-string-literal-with-embedded-nul4=========================================5 6Finds occurrences of string literal with embedded NUL character and validates7their usage.8 9Invalid escaping10----------------11 12Special characters can be escaped within a string literal by using their13hexadecimal encoding like ``\x42``. A common mistake is to escape them14like this ``\0x42`` where the ``\0`` stands for the NUL character.15 16.. code-block:: c++17 18 const char* Example[] = "Invalid character: \0x12 should be \x12";19 const char* Bytes[] = "\x03\0x02\0x01\0x00\0xFF\0xFF\0xFF";20 21Truncated literal22-----------------23 24String-like classes can manipulate strings with embedded NUL as they are25keeping track of the bytes and the length. This is not the case for a26``char*`` (NUL-terminated) string.27 28A common mistake is to pass a string-literal with embedded NUL to a string29constructor expecting a NUL-terminated string. The bytes after the first NUL30character are truncated.31 32.. code-block:: c++33 34 std::string str("abc\0def"); // "def" is truncated35 str += "\0"; // This statement is doing nothing36 if (str == "\0abc") return; // This expression is always true37