1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
|
// RUN: %clang_analyze_cc1 -std=c++14 -analyzer-checker=cplusplus.InnerPointer \
// RUN: -Wno-dangling -Wno-dangling-field -Wno-return-stack-address \
// RUN: %s -analyzer-output=text -verify
#include "Inputs/system-header-simulator-cxx.h"
namespace std {
template <typename T>
void func_ref(T &a);
template <typename T>
void func_const_ref(const T &a);
template <typename T>
void func_value(T a);
string my_string = "default";
void default_arg(int a = 42, string &b = my_string);
template <class T>
T *addressof(T &arg);
char *data(std::string &c);
} // end namespace std
void consume(const char *) {}
void consume(const wchar_t *) {}
void consume(const char16_t *) {}
void consume(const char32_t *) {}
//=--------------------------------------=//
// `std::string` member functions //
//=--------------------------------------=//
void deref_after_scope_char(bool cond) {
const char *c, *d;
{
std::string s;
c = s.c_str(); // expected-note {{Pointer to inner buffer of 'std::string' obtained here}}
d = s.data(); // expected-note {{Pointer to inner buffer of 'std::string' obtained here}}
} // expected-note {{Inner buffer of 'std::string' deallocated by call to destructor}}
// expected-note@-1 {{Inner buffer of 'std::string' deallocated by call to destructor}}
std::string s;
const char *c2 = s.c_str();
if (cond) {
// expected-note@-1 {{Assuming 'cond' is true}}
// expected-note@-2 {{Taking true branch}}
// expected-note@-3 {{Assuming 'cond' is false}}
// expected-note@-4 {{Taking false branch}}
consume(c); // expected-warning {{Inner pointer of container used after re/deallocation}}
// expected-note@-1 {{Inner pointer of container used after re/deallocation}}
} else {
consume(d); // expected-warning {{Inner pointer of container used after re/deallocation}}
// expected-note@-1 {{Inner pointer of container used after re/deallocation}}
}
}
void deref_after_scope_char_data_non_const() {
char *c;
{
std::string s;
c = s.data(); // expected-note {{Pointer to inner buffer of 'std::string' obtained here}}
} // expected-note {{Inner buffer of 'std::string' deallocated by call to destructor}}
std::string s;
char *c2 = s.data();
consume(c); // expected-warning {{Inner pointer of container used after re/deallocation}}
// expected-note@-1 {{Inner pointer of container used after re/deallocation}}
}
void deref_after_scope_wchar_t(bool cond) {
const wchar_t *c, *d;
{
std::wstring s;
c = s.c_str(); // expected-note {{Pointer to inner buffer of 'std::wstring' obtained here}}
d = s.data(); // expected-note {{Pointer to inner buffer of 'std::wstring' obtained here}}
} // expected-note {{Inner buffer of 'std::wstring' deallocated by call to destructor}}
// expected-note@-1 {{Inner buffer of 'std::wstring' deallocated by call to destructor}}
std::wstring s;
const wchar_t *c2 = s.c_str();
if (cond) {
// expected-note@-1 {{Assuming 'cond' is true}}
// expected-note@-2 {{Taking true branch}}
// expected-note@-3 {{Assuming 'cond' is false}}
// expected-note@-4 {{Taking false branch}}
consume(c); // expected-warning {{Inner pointer of container used after re/deallocation}}
// expected-note@-1 {{Inner pointer of container used after re/deallocation}}
} else {
consume(d); // expected-warning {{Inner pointer of container used after re/deallocation}}
// expected-note@-1 {{Inner pointer of container used after re/deallocation}}
}
}
void deref_after_scope_char16_t_cstr() {
const char16_t *c16;
{
std::u16string s16;
c16 = s16.c_str(); // expected-note {{Pointer to inner buffer of 'std::u16string' obtained here}}
} // expected-note {{Inner buffer of 'std::u16string' deallocated by call to destructor}}
std::u16string s16;
const char16_t *c16_2 = s16.c_str();
consume(c16); // expected-warning {{Inner pointer of container used after re/deallocation}}
// expected-note@-1 {{Inner pointer of container used after re/deallocation}}
}
void deref_after_scope_char32_t_data() {
const char32_t *c32;
{
std::u32string s32;
c32 = s32.data(); // expected-note {{Pointer to inner buffer of 'std::u32string' obtained here}}
} // expected-note {{Inner buffer of 'std::u32string' deallocated by call to destructor}}
std::u32string s32;
const char32_t *c32_2 = s32.data();
consume(c32); // expected-warning {{Inner pointer of container used after re/deallocation}}
// expected-note@-1 {{Inner pointer of container used after re/deallocation}}
}
void multiple_symbols(bool cond) {
const char *c1, *d1;
{
std::string s1;
c1 = s1.c_str(); // expected-note {{Pointer to inner buffer of 'std::string' obtained here}}
d1 = s1.data(); // expected-note {{Pointer to inner buffer of 'std::string' obtained here}}
const char *local = s1.c_str();
consume(local); // no-warning
} // expected-note {{Inner buffer of 'std::string' deallocated by call to destructor}}
// expected-note@-1 {{Inner buffer of 'std::string' deallocated by call to destructor}}
std::string s2;
const char *c2 = s2.c_str();
if (cond) {
// expected-note@-1 {{Assuming 'cond' is true}}
// expected-note@-2 {{Taking true branch}}
// expected-note@-3 {{Assuming 'cond' is false}}
// expected-note@-4 {{Taking false branch}}
consume(c1); // expected-warning {{Inner pointer of container used after re/deallocation}}
// expected-note@-1 {{Inner pointer of container used after re/deallocation}}
} else {
consume(d1); // expected-warning {{Inner pointer of container used after re/deallocation}}
} // expected-note@-1 {{Inner pointer of container used after re/deallocation}}
}
void deref_after_scope_ok(bool cond) {
const char *c, *d;
std::string s;
{
c = s.c_str();
d = s.data();
}
if (cond)
consume(c); // no-warning
else
consume(d); // no-warning
}
void deref_after_equals() {
const char *c;
std::string s = "hello";
c = s.c_str(); // expected-note {{Pointer to inner buffer of 'std::string' obtained here}}
s = "world"; // expected-note {{Inner buffer of 'std::string' reallocated by call to 'operator='}}
consume(c); // expected-warning {{Inner pointer of container used after re/deallocation}}
// expected-note@-1 {{Inner pointer of container used after re/deallocation}}
}
void deref_after_plus_equals() {
const char *c;
std::string s = "hello";
c = s.data(); // expected-note {{Pointer to inner buffer of 'std::string' obtained here}}
s += " world"; // expected-note {{Inner buffer of 'std::string' reallocated by call to 'operator+='}}
consume(c); // expected-warning {{Inner pointer of container used after re/deallocation}}
// expected-note@-1 {{Inner pointer of container used after re/deallocation}}
}
void deref_after_clear() {
const char *c;
std::string s;
c = s.c_str(); // expected-note {{Pointer to inner buffer of 'std::string' obtained here}}
s.clear(); // expected-note {{Inner buffer of 'std::string' reallocated by call to 'clear'}}
consume(c); // expected-warning {{Inner pointer of container used after re/deallocation}}
// expected-note@-1 {{Inner pointer of container used after re/deallocation}}
}
void deref_after_append() {
const char *c;
std::string s = "hello";
c = s.c_str(); // expected-note {{Pointer to inner buffer of 'std::string' obtained here}}
s.append(2, 'x'); // expected-note {{Inner buffer of 'std::string' reallocated by call to 'append'}}
consume(c); // expected-warning {{Inner pointer of container used after re/deallocation}}
// expected-note@-1 {{Inner pointer of container used after re/deallocation}}
}
void deref_after_assign() {
const char *c;
std::string s;
c = s.data(); // expected-note {{Pointer to inner buffer of 'std::string' obtained here}}
s.assign(4, 'a'); // expected-note {{Inner buffer of 'std::string' reallocated by call to 'assign'}}
consume(c); // expected-warning {{Inner pointer of container used after re/deallocation}}
// expected-note@-1 {{Inner pointer of container used after re/deallocation}}
}
void deref_after_erase() {
const char *c;
std::string s = "hello";
c = s.c_str(); // expected-note {{Pointer to inner buffer of 'std::string' obtained here}}
s.erase(0, 2); // expected-note {{Inner buffer of 'std::string' reallocated by call to 'erase'}}
consume(c); // expected-warning {{Inner pointer of container used after re/deallocation}}
// expected-note@-1 {{Inner pointer of container used after re/deallocation}}
}
void deref_after_insert() {
const char *c;
std::string s = "ello";
c = s.c_str(); // expected-note {{Pointer to inner buffer of 'std::string' obtained here}}
s.insert(0, 1, 'h'); // expected-note {{Inner buffer of 'std::string' reallocated by call to 'insert'}}
consume(c); // expected-warning {{Inner pointer of container used after re/deallocation}}
// expected-note@-1 {{Inner pointer of container used after re/deallocation}}
}
void deref_after_replace() {
const char *c;
std::string s = "hello world";
c = s.c_str(); // expected-note {{Pointer to inner buffer of 'std::string' obtained here}}
s.replace(6, 5, "string"); // expected-note {{Inner buffer of 'std::string' reallocated by call to 'replace'}}
consume(c); // expected-warning {{Inner pointer of container used after re/deallocation}}
// expected-note@-1 {{Inner pointer of container used after re/deallocation}}
}
void deref_after_pop_back() {
const char *c;
std::string s;
c = s.c_str(); // expected-note {{Pointer to inner buffer of 'std::string' obtained here}}
s.pop_back(); // expected-note {{Inner buffer of 'std::string' reallocated by call to 'pop_back'}}
consume(c); // expected-warning {{Inner pointer of container used after re/deallocation}}
// expected-note@-1 {{Inner pointer of container used after re/deallocation}}
}
void deref_after_push_back() {
const char *c;
std::string s;
c = s.data(); // expected-note {{Pointer to inner buffer of 'std::string' obtained here}}
s.push_back('c'); // expected-note {{Inner buffer of 'std::string' reallocated by call to 'push_back'}}
consume(c); // expected-warning {{Inner pointer of container used after re/deallocation}}
// expected-note@-1 {{Inner pointer of container used after re/deallocation}}
}
void deref_after_reserve() {
const char *c;
std::string s;
c = s.c_str(); // expected-note {{Pointer to inner buffer of 'std::string' obtained here}}
s.reserve(5); // expected-note {{Inner buffer of 'std::string' reallocated by call to 'reserve'}}
consume(c); // expected-warning {{Inner pointer of container used after re/deallocation}}
// expected-note@-1 {{Inner pointer of container used after re/deallocation}}
}
void deref_after_resize() {
const char *c;
std::string s;
c = s.data(); // expected-note {{Pointer to inner buffer of 'std::string' obtained here}}
s.resize(5); // expected-note {{Inner buffer of 'std::string' reallocated by call to 'resize'}}
consume(c); // expected-warning {{Inner pointer of container used after re/deallocation}}
// expected-note@-1 {{Inner pointer of container used after re/deallocation}}
}
void deref_after_shrink_to_fit() {
const char *c;
std::string s;
c = s.data(); // expected-note {{Pointer to inner buffer of 'std::string' obtained here}}
s.shrink_to_fit(); // expected-note {{Inner buffer of 'std::string' reallocated by call to 'shrink_to_fit'}}
consume(c); // expected-warning {{Inner pointer of container used after re/deallocation}}
// expected-note@-1 {{Inner pointer of container used after re/deallocation}}
}
void deref_after_swap() {
const char *c;
std::string s1, s2;
c = s1.data(); // expected-note {{Pointer to inner buffer of 'std::string' obtained here}}
s1.swap(s2); // expected-note {{Inner buffer of 'std::string' reallocated by call to 'swap'}}
consume(c); // expected-warning {{Inner pointer of container used after re/deallocation}}
// expected-note@-1 {{Inner pointer of container used after re/deallocation}}
}
void deref_after_std_data() {
const char *c;
std::string s;
c = std::data(s); // expected-note {{Pointer to inner buffer of 'std::string' obtained here}}
s.push_back('c'); // expected-note {{Inner buffer of 'std::string' reallocated by call to 'push_back'}}
consume(c); // expected-warning {{Inner pointer of container used after re/deallocation}}
// expected-note@-1 {{Inner pointer of container used after re/deallocation}}
}
struct S {
std::string s;
const char *name() {
return s.c_str(); // expected-note {{Pointer to inner buffer of 'std::string' obtained here}}
// expected-note@-1 {{Pointer to inner buffer of 'std::string' obtained here}}
}
void clear() {
s.clear(); // expected-note {{Inner buffer of 'std::string' reallocated by call to 'clear'}}
}
~S() {} // expected-note {{Inner buffer of 'std::string' deallocated by call to destructor}}
};
void cleared_through_method() {
S x;
const char *c = x.name(); // expected-note {{Calling 'S::name'}}
// expected-note@-1 {{Returning from 'S::name'}}
x.clear(); // expected-note {{Calling 'S::clear'}}
// expected-note@-1 {{Returning; inner buffer was reallocated}}
consume(c); // expected-warning {{Inner pointer of container used after re/deallocation}}
// expected-note@-1 {{Inner pointer of container used after re/deallocation}}
}
void destroyed_through_method() {
S y;
const char *c = y.name(); // expected-note {{Calling 'S::name'}}
// expected-note@-1 {{Returning from 'S::name'}}
y.~S(); // expected-note {{Calling '~S'}}
// expected-note@-1 {{Returning; inner buffer was deallocated}}
consume(c); // expected-warning {{Inner pointer of container used after re/deallocation}}
// expected-note@-1 {{Inner pointer of container used after re/deallocation}}
}
//=---------------------------=//
// Other STL functions //
//=---------------------------=//
void STL_func_ref() {
const char *c;
std::string s;
c = s.c_str(); // expected-note {{Pointer to inner buffer of 'std::string' obtained here}}
std::func_ref(s); // expected-note {{Inner buffer of 'std::string' reallocated by call to 'func_ref'}}
consume(c); // expected-warning {{Inner pointer of container used after re/deallocation}}
// expected-note@-1 {{Inner pointer of container used after re/deallocation}}
}
void STL_func_const_ref() {
const char *c;
std::string s;
c = s.c_str();
std::func_const_ref(s);
consume(c); // no-warning
}
void STL_func_value() {
const char *c;
std::string s;
c = s.c_str();
std::func_value(s);
consume(c); // no-warning
}
void func_ptr_known() {
const char *c;
std::string s;
void (*func_ptr)(std::string &) = std::func_ref<std::string>;
c = s.c_str(); // expected-note {{Pointer to inner buffer of 'std::string' obtained here}}
func_ptr(s); // expected-note {{Inner buffer of 'std::string' reallocated by call to 'func_ref'}}
consume(c); // expected-warning {{Inner pointer of container used after re/deallocation}}
// expected-note@-1 {{Inner pointer of container used after re/deallocation}}
}
void func_ptr_unknown(void (*func_ptr)(std::string &)) {
const char *c;
std::string s;
c = s.c_str();
func_ptr(s);
consume(c); // no-warning
}
void func_default_arg() {
const char *c;
std::string s;
c = s.c_str(); // expected-note {{Pointer to inner buffer of 'std::string' obtained here}}
default_arg(3, s); // expected-note {{Inner buffer of 'std::string' reallocated by call to 'default_arg'}}
consume(c); // expected-warning {{Inner pointer of container used after re/deallocation}}
// expected-note@-1 {{Inner pointer of container used after re/deallocation}}
}
void func_addressof() {
const char *c;
std::string s;
c = s.c_str();
addressof(s);
consume(c); // no-warning
}
void func_std_data() {
const char *c;
std::string s;
c = std::data(s);
consume(c); // no-warning
}
struct T {
std::string to_string() { return s; }
private:
std::string s;
};
const char *escape_via_return_temp() {
T x;
return x.to_string().c_str(); // expected-note {{Pointer to inner buffer of 'std::string' obtained here}}
// expected-note@-1 {{Inner buffer of 'std::string' deallocated by call to destructor}}
// expected-warning@-2 {{Inner pointer of container used after re/deallocation}}
// expected-note@-3 {{Inner pointer of container used after re/deallocation}}
}
const char *escape_via_return_local() {
std::string s;
return s.c_str(); // expected-note {{Pointer to inner buffer of 'std::string' obtained here}}
// expected-note@-1 {{Inner buffer of 'std::string' deallocated by call to destructor}}
// expected-warning@-2 {{Inner pointer of container used after re/deallocation}}
// expected-note@-3 {{Inner pointer of container used after re/deallocation}}
}
char *c();
class A {};
void no_CXXRecordDecl() {
A a, *b;
*(void **)&b = c() + 1;
*b = a; // no-crash
}
void checkReference(std::string &s) {
const char *c = s.c_str();
}
|