1// RUN: %check_clang_tidy %s bugprone-string-literal-with-embedded-nul %t
2
3namespace std {
4template <typename T>
5class allocator {};
6template <typename T>
7class char_traits {};
8template <typename C, typename T, typename A>
9struct basic_string {
10 typedef basic_string<C, T, A> _Type;
11 basic_string();
12 basic_string(const C *p, const A &a = A());
13
14 _Type& operator+=(const C* s);
15 _Type& operator=(const C* s);
16};
17
18typedef basic_string<char, std::char_traits<char>, std::allocator<char>> string;
19typedef basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t>> wstring;
20}
21
22bool operator==(const std::string&, const char*);
23bool operator==(const char*, const std::string&);
24
25
26const char Valid[] = "This is valid \x12.";
27const char Strange[] = "This is strange \0x12 and must be fixed";
28// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: suspicious embedded NUL character [bugprone-string-literal-with-embedded-nul]
29
30const char textA[] = "\0x01\0x02\0x03\0x04";
31// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: suspicious embedded NUL character
32const wchar_t textW[] = L"\0x01\0x02\0x03\0x04";
33// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: suspicious embedded NUL character
34
35const char A[] = "\0";
36const char B[] = "\0x";
37const char C[] = "\0x1";
38const char D[] = "\0x11";
39// CHECK-MESSAGES: :[[@LINE-1]]:18: warning: suspicious embedded NUL character
40
41const wchar_t E[] = L"\0";
42const wchar_t F[] = L"\0x";
43const wchar_t G[] = L"\0x1";
44const wchar_t H[] = L"\0x11";
45// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: suspicious embedded NUL character
46
47const char I[] = "\000\000\000\000";
48const char J[] = "\0\0\0\0\0\0";
49const char K[] = "";
50
51const char L[] = "\0x12" "\0x12" "\0x12" "\0x12";
52// CHECK-MESSAGES: :[[@LINE-1]]:18: warning: suspicious embedded NUL character
53
54void TestA() {
55 std::string str1 = "abc\0def";
56 // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: truncated string literal
57 std::string str2 = "\0";
58 // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: truncated string literal
59 std::string str3("\0");
60 // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: truncated string literal
61 std::string str4{"\x00\x01\x02\x03"};
62 // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: truncated string literal
63
64 std::string str;
65 str += "abc\0def";
66 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: truncated string literal
67 str = "abc\0def";
68 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: truncated string literal
69
70 if (str == "abc\0def") return;
71 // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: truncated string literal
72 if ("abc\0def" == str) return;
73 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: truncated string literal
74}
75
76void TestW() {
77 std::wstring str1 = L"abc\0def";
78 // CHECK-MESSAGES: :[[@LINE-1]]:23: warning: truncated string literal
79 std::wstring str2 = L"\0";
80 // CHECK-MESSAGES: :[[@LINE-1]]:23: warning: truncated string literal
81 std::wstring str3(L"\0");
82 // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: truncated string literal
83 std::wstring str4{L"\x00\x01\x02\x03"};
84 // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: truncated string literal
85}
86

source code of clang-tools-extra/test/clang-tidy/checkers/bugprone/string-literal-with-embedded-nul.cpp