1 | // RUN: %check_clang_tidy %s bugprone-string-literal-with-embedded-nul %t |
2 | |
3 | namespace std { |
4 | template <typename T> |
5 | class allocator {}; |
6 | template <typename T> |
7 | class char_traits {}; |
8 | template <typename C, typename T, typename A> |
9 | struct 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 | |
18 | typedef basic_string<char, std::char_traits<char>, std::allocator<char>> string; |
19 | typedef basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t>> wstring; |
20 | } |
21 | |
22 | bool operator==(const std::string&, const char*); |
23 | bool operator==(const char*, const std::string&); |
24 | |
25 | |
26 | const char Valid[] = "This is valid \x12." ; |
27 | const 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 | |
30 | const char textA[] = "\0x01\0x02\0x03\0x04" ; |
31 | // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: suspicious embedded NUL character |
32 | const wchar_t textW[] = L"\0x01\0x02\0x03\0x04" ; |
33 | // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: suspicious embedded NUL character |
34 | |
35 | const char A[] = "\0" ; |
36 | const char B[] = "\0x" ; |
37 | const char C[] = "\0x1" ; |
38 | const char D[] = "\0x11" ; |
39 | // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: suspicious embedded NUL character |
40 | |
41 | const wchar_t E[] = L"\0" ; |
42 | const wchar_t F[] = L"\0x" ; |
43 | const wchar_t G[] = L"\0x1" ; |
44 | const wchar_t H[] = L"\0x11" ; |
45 | // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: suspicious embedded NUL character |
46 | |
47 | const char I[] = "\000\000\000\000" ; |
48 | const char J[] = "\0\0\0\0\0\0" ; |
49 | const char K[] = "" ; |
50 | |
51 | const char L[] = "\0x12" "\0x12" "\0x12" "\0x12" ; |
52 | // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: suspicious embedded NUL character |
53 | |
54 | void 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 | |
76 | void 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 | |