|
| 1 | +// RUN: %clang_cc1 -fsyntax-only -std=c++20 -Wno-all -Wunsafe-buffer-usage -fsafe-buffer-usage-suggestions -fexperimental-bounds-safety-attributes -verify %s |
| 2 | + |
| 3 | +#include <ptrcheck.h> |
| 4 | + |
| 5 | +typedef unsigned size_t; |
| 6 | +namespace std { |
| 7 | + template <typename CharT> |
| 8 | + struct basic_string { |
| 9 | + const CharT *data() const noexcept; |
| 10 | + CharT *data() noexcept; |
| 11 | + const CharT *c_str() const noexcept; |
| 12 | + size_t size() const noexcept; |
| 13 | + size_t length() const noexcept; |
| 14 | + }; |
| 15 | + |
| 16 | + typedef basic_string<char> string; |
| 17 | + |
| 18 | + template <typename CharT> |
| 19 | + struct basic_string_view { |
| 20 | + basic_string_view(basic_string<CharT> str); |
| 21 | + const CharT *data() const noexcept; |
| 22 | + size_t size() const noexcept; |
| 23 | + size_t length() const noexcept; |
| 24 | + }; |
| 25 | + |
| 26 | + typedef basic_string_view<char> string_view; |
| 27 | +} // namespace std |
| 28 | + |
| 29 | +void nt_parm(const char * __null_terminated); |
| 30 | +const char * __null_terminated get_nt(const char *, size_t); |
| 31 | + |
| 32 | +void basics(const char * cstr, size_t cstr_len, std::string cxxstr) { |
| 33 | + const char * __null_terminated p = "hello"; // safe init |
| 34 | + |
| 35 | + nt_parm(p); |
| 36 | + nt_parm(get_nt(cstr, cstr_len)); |
| 37 | + |
| 38 | + const char * __null_terminated p2 = cxxstr.c_str(); // safe init |
| 39 | + const char * __null_terminated p3; |
| 40 | + |
| 41 | + p3 = cxxstr.c_str(); // safe assignment |
| 42 | + p3 = "hello"; // safe assignment |
| 43 | + p3 = p; // safe assignment |
| 44 | + p3 = get_nt(cstr, cstr_len); // safe assignment |
| 45 | + |
| 46 | + // expected-error@+1 {{initializing 'const char * __terminated_by(0)' (aka 'const char *') with an expression of incompatible type 'const char *' is an unsafe operation}} |
| 47 | + const char * __null_terminated p4 = cstr; // warn |
| 48 | + |
| 49 | + // expected-error@+1 {{passing 'const char *' to parameter of incompatible type 'const char * __terminated_by(0)' (aka 'const char *') is an unsafe operation}} |
| 50 | + nt_parm(cstr); // warn |
| 51 | + // expected-error@+1 {{assigning to 'const char * __terminated_by(0)' (aka 'const char *') from incompatible type 'const char *' is an unsafe operation}} |
| 52 | + p4 = cstr; // warn |
| 53 | + |
| 54 | + std::string_view view{cxxstr}; |
| 55 | + |
| 56 | + // expected-error@+1 {{assigning to 'const char * __terminated_by(0)' (aka 'const char *') from incompatible type 'const char *' is an unsafe operation}} |
| 57 | + p4 = view.data(); // warn |
| 58 | + // expected-error@+1 {{passing 'const char *' to parameter of incompatible type 'const char * __terminated_by(0)' (aka 'const char *') is an unsafe operation}} |
| 59 | + nt_parm(view.data()); // warn |
| 60 | + |
| 61 | + const char * __null_terminated p5 = 0; // nullptr is ok |
| 62 | + // expected-error@+1 {{initializing 'const char * __terminated_by(0)' (aka 'const char *') with an expression of incompatible type 'const char *' is an unsafe operation}} |
| 63 | + const char * __null_terminated p6 = (const char *)1; // other integer literal is unsafe |
| 64 | + |
| 65 | + // (non-0)-terminated pointer is NOT compatible with 'std::string::c_str': |
| 66 | + // expected-error@+1 {{initializing 'const char * __terminated_by(42)' (aka 'const char *') with an expression of incompatible type 'const char *' is an unsafe operation}} |
| 67 | + const char * __terminated_by(42) p7 = cxxstr.c_str(); |
| 68 | +} |
| 69 | + |
| 70 | +void test_explicit_cast(char * p, const char * q) { |
| 71 | + // expected-error@+1 {{casting 'char *' to incompatible type 'const char * __terminated_by(0)' (aka 'const char *') is an unsafe operation}} |
| 72 | + const char * __null_terminated nt = (const char * __null_terminated) p; |
| 73 | + const char * __null_terminated nt2 = reinterpret_cast<const char * __null_terminated> (p); // FP for now |
| 74 | + |
| 75 | + // expected-error@+1 {{casting 'char *' to incompatible type 'const char * __terminated_by(0)' (aka 'const char *') is an unsafe operation}} |
| 76 | + nt = (const char * __null_terminated) p; |
| 77 | + nt2 = reinterpret_cast<const char * __null_terminated> (p); // FP for now |
| 78 | + // expected-error@+1 {{casting 'char *' to incompatible type 'const char * __terminated_by(0)' (aka 'const char *') is an unsafe operation}} |
| 79 | + nt2 = static_cast<const char * __null_terminated> (p); // FP for now |
| 80 | + |
| 81 | + // expected-error@+1 {{casting 'const char *' to incompatible type 'const char * __terminated_by(0)' (aka 'const char *') is an unsafe operation}} |
| 82 | + const char * __null_terminated nt3 = (const char * __null_terminated) q; |
| 83 | + const char * __null_terminated nt4 = reinterpret_cast<const char * __null_terminated> (q); // FP for now |
| 84 | + |
| 85 | + // expected-error@+1 {{casting 'const char *' to incompatible type 'const char * __terminated_by(0)' (aka 'const char *') is an unsafe operation}} |
| 86 | + nt3 = (const char * __null_terminated) q; |
| 87 | + nt4 = reinterpret_cast<const char * __null_terminated> (q); // FP for now |
| 88 | + // expected-error@+1 {{casting 'const char *' to incompatible type 'const char * __terminated_by(0)' (aka 'const char *') is an unsafe operation}} |
| 89 | + nt4 = static_cast<const char * __null_terminated> (q); |
| 90 | + |
| 91 | + // OK cases for C-style casts |
| 92 | + char * __null_terminated nt_p; |
| 93 | + const char * __null_terminated nt_p2; |
| 94 | + int * __null_terminated nt_p3; |
| 95 | + const int * __null_terminated nt_p4; |
| 96 | + |
| 97 | + const char * __null_terminated nt5 = (const char * __null_terminated) nt_p; |
| 98 | + const char * __null_terminated nt6 = (const char * __null_terminated) nt_p2; |
| 99 | + const char * __null_terminated nt7 = (const char * __null_terminated) nt_p3; |
| 100 | + const char * __null_terminated nt8 = (const char * __null_terminated) nt_p4; |
| 101 | + |
| 102 | + nt5 = (const char * __null_terminated) nt_p; |
| 103 | + nt6 = (const char * __null_terminated) nt_p2; |
| 104 | + nt7 = (const char * __null_terminated) nt_p3; |
| 105 | + nt8 = (const char * __null_terminated) nt_p4; |
| 106 | + |
| 107 | + |
| 108 | + char * __null_terminated nt9 = (char * __null_terminated) nt_p; |
| 109 | + char * __null_terminated nt10 = (char * __null_terminated) nt_p2; |
| 110 | + char * __null_terminated nt11 = (char * __null_terminated) nt_p3; |
| 111 | + char * __null_terminated nt12 = (char * __null_terminated) nt_p4; |
| 112 | + |
| 113 | + nt9 = (char * __null_terminated) nt_p; |
| 114 | + nt10 = (char * __null_terminated) nt_p2; |
| 115 | + nt11 = (char * __null_terminated) nt_p3; |
| 116 | + nt12 = (char * __null_terminated) nt_p4; |
| 117 | +} |
| 118 | + |
| 119 | +const char * __null_terminated test_return(const char * p, char * q, std::string &str) { |
| 120 | + if (p) |
| 121 | + return p; // expected-error {{returning 'const char *' from a function with incompatible result type 'const char * __terminated_by(0)' (aka 'const char *') is an unsafe operation}} |
| 122 | + if (q) |
| 123 | + return q; // expected-error {{returning 'char *' from a function with incompatible result type 'const char * __terminated_by(0)' (aka 'const char *') is an unsafe operation}} |
| 124 | + return str.c_str(); |
| 125 | +} |
| 126 | + |
| 127 | +void test_array(char * cstr) { |
| 128 | + const char arr[__null_terminated 3] = {'h', 'i', '\0'}; |
| 129 | + // expected-error@+1 {{array 'arr2' with '__terminated_by' attribute is initialized with an incorrect terminator (expected: 0; got 'i')}} |
| 130 | + const char arr2[__null_terminated 2] = {'h', 'i'}; |
| 131 | + const char * __null_terminated arr3[] = {"hello", "world"}; |
| 132 | + // expected-error@+1 {{initializing 'const char * __terminated_by(0)' (aka 'const char *') with an expression of incompatible type 'char *' is an unsafe operation}} |
| 133 | + const char * __null_terminated arr4[] = {"hello", "world", cstr}; |
| 134 | + |
| 135 | + // expected-error@+1 {{assigning to 'const char * __terminated_by(0)' (aka 'const char *') from incompatible type 'char *' is an unsafe operation}} |
| 136 | + arr3[0] = cstr; |
| 137 | +} |
| 138 | + |
| 139 | +struct T { |
| 140 | + int a; |
| 141 | + const char * __null_terminated p; |
| 142 | + struct TT { |
| 143 | + int a; |
| 144 | + const char * __null_terminated p; |
| 145 | + } tt; |
| 146 | +}; |
| 147 | +void test_compound(char * cstr) { |
| 148 | + std::string str; |
| 149 | + T t = {42, "hello"}; |
| 150 | + T t2 = {.a = 42}; |
| 151 | + T t3 = {.p = str.c_str()}; |
| 152 | + // expected-error@+1 {{initializing 'const char * __terminated_by(0)' (aka 'const char *') with an expression of incompatible type 'char *' is an unsafe operation}} |
| 153 | + T t4 = {42, "hello", {.p = cstr}}; |
| 154 | + |
| 155 | + // expected-error@+1 {{initializing 'const char * __terminated_by(0)' (aka 'const char *') with an expression of incompatible type 'char *' is an unsafe operation}} |
| 156 | + t4 = (struct T){42, "hello", {.p = cstr}}; |
| 157 | +} |
| 158 | + |
| 159 | + // expected-error@+3 {{initializing 'const char * __terminated_by(0)' (aka 'const char *') with an expression of incompatible type 'char *' is an unsafe operation}} |
| 160 | +class C { |
| 161 | + const char * __null_terminated p; |
| 162 | + const char * __null_terminated q = (char *) 1; // warn |
| 163 | + struct T t; |
| 164 | +public: |
| 165 | + // expected-error@+1 2{{initializing 'const char * __terminated_by(0)' (aka 'const char *') with an expression of incompatible type 'char *' is an unsafe operation}} |
| 166 | + C(char * p): p(p), t({0, p}) {}; |
| 167 | + C(const char * __null_terminated p, struct T t); |
| 168 | +}; |
| 169 | + |
| 170 | +void f(const C &c); |
| 171 | +C test_class(char * cstr) { |
| 172 | + // expected-error@+2 {{passing 'char *' to parameter of incompatible type 'const char * __terminated_by(0)' (aka 'const char *') is an unsafe operation}} |
| 173 | + // expected-error@+1 {{initializing 'const char * __terminated_by(0)' (aka 'const char *') with an expression of incompatible type 'char *' is an unsafe operation}} |
| 174 | + C c{cstr, {0, cstr}}; |
| 175 | + // expected-error@+2 {{passing 'char *' to parameter of incompatible type 'const char * __terminated_by(0)' (aka 'const char *') is an unsafe operation}} |
| 176 | + // expected-error@+1 {{initializing 'const char * __terminated_by(0)' (aka 'const char *') with an expression of incompatible type 'char *' is an unsafe operation}} |
| 177 | + C c1(cstr, {0, cstr}); |
| 178 | + // expected-error@+2 {{passing 'char *' to parameter of incompatible type 'const char * __terminated_by(0)' (aka 'const char *') is an unsafe operation}} |
| 179 | + // expected-error@+1 {{initializing 'const char * __terminated_by(0)' (aka 'const char *') with an expression of incompatible type 'char *' is an unsafe operation}} |
| 180 | + C *c2 = new C(cstr, {0, cstr}); |
| 181 | + // expected-error@+2 {{passing 'char *' to parameter of incompatible type 'const char * __terminated_by(0)' (aka 'const char *') is an unsafe operation}} |
| 182 | + // expected-error@+1 {{initializing 'const char * __terminated_by(0)' (aka 'const char *') with an expression of incompatible type 'char *' is an unsafe operation}} |
| 183 | + f(C{cstr, {0, cstr}}); |
| 184 | + |
| 185 | + C("hello", {0, "hello"}); |
| 186 | + if (1-1) |
| 187 | + return {cstr, {}}; // expected-error {{passing 'char *' to parameter of incompatible type 'const char * __terminated_by(0)' (aka 'const char *') is an unsafe operation}} |
| 188 | + return {"hello", {}}; |
| 189 | +} |
| 190 | + |
| 191 | + |
| 192 | +// Test input/output __null_terminated parameter. |
| 193 | +// expected-note@+1 {{candidate function not viable:}} |
| 194 | +void g(const char * __null_terminated *p); |
| 195 | +void test_output(const char * __null_terminated p) { |
| 196 | + const char * __null_terminated local_nt = p; |
| 197 | + const char * const_local; |
| 198 | + char * local; |
| 199 | + |
| 200 | + g(&local_nt); // safe |
| 201 | + // expected-error@+1 {{passing 'const char **' to parameter of incompatible type 'const char * __terminated_by(0)*' (aka 'const char **') that adds '__terminated_by' attribute is not allowed}} |
| 202 | + g(&const_local); |
| 203 | + // expected-error@+1 {{no matching function for call to 'g'}} |
| 204 | + g(&local); |
| 205 | +} |
0 commit comments