cxx-uninitialized-object-ptr-ref.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. // RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.cplusplus.UninitializedObject \
  2. // RUN: -analyzer-config alpha.cplusplus.UninitializedObject:Pedantic=true -DPEDANTIC \
  3. // RUN: -analyzer-config alpha.cplusplus.UninitializedObject:CheckPointeeInitialization=true \
  4. // RUN: -std=c++11 -verify %s
  5. // RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.cplusplus.UninitializedObject \
  6. // RUN: -analyzer-config alpha.cplusplus.UninitializedObject:CheckPointeeInitialization=true \
  7. // RUN: -std=c++11 -verify %s
  8. //===----------------------------------------------------------------------===//
  9. // Concrete location tests.
  10. //===----------------------------------------------------------------------===//
  11. struct ConcreteIntLocTest {
  12. int *ptr;
  13. ConcreteIntLocTest() : ptr(reinterpret_cast<int *>(0xDEADBEEF)) {}
  14. };
  15. void fConcreteIntLocTest() {
  16. ConcreteIntLocTest();
  17. }
  18. //===----------------------------------------------------------------------===//
  19. // nonloc::LocAsInteger tests.
  20. //===----------------------------------------------------------------------===//
  21. using intptr_t = long;
  22. struct LocAsIntegerTest {
  23. intptr_t ptr; // expected-note{{uninitialized pointee 'reinterpret_cast<char *>(this->ptr)'}}
  24. int dontGetFilteredByNonPedanticMode = 0;
  25. LocAsIntegerTest(void *ptr) : ptr(reinterpret_cast<intptr_t>(ptr)) {} // expected-warning{{1 uninitialized field}}
  26. };
  27. void fLocAsIntegerTest() {
  28. char c;
  29. LocAsIntegerTest t(&c);
  30. }
  31. //===----------------------------------------------------------------------===//
  32. // Null pointer tests.
  33. //===----------------------------------------------------------------------===//
  34. class NullPtrTest {
  35. struct RecordType {
  36. int x;
  37. int y;
  38. };
  39. float *fptr = nullptr;
  40. int *ptr;
  41. RecordType *recPtr;
  42. public:
  43. NullPtrTest() : ptr(nullptr), recPtr(nullptr) {
  44. // All good!
  45. }
  46. };
  47. void fNullPtrTest() {
  48. NullPtrTest();
  49. }
  50. //===----------------------------------------------------------------------===//
  51. // Alloca tests.
  52. //===----------------------------------------------------------------------===//
  53. struct UntypedAllocaTest {
  54. void *allocaPtr;
  55. int dontGetFilteredByNonPedanticMode = 0;
  56. UntypedAllocaTest() : allocaPtr(__builtin_alloca(sizeof(int))) {
  57. // All good!
  58. }
  59. };
  60. void fUntypedAllocaTest() {
  61. UntypedAllocaTest();
  62. }
  63. struct TypedAllocaTest1 {
  64. int *allocaPtr; // expected-note{{uninitialized pointee 'this->allocaPtr'}}
  65. int dontGetFilteredByNonPedanticMode = 0;
  66. TypedAllocaTest1() // expected-warning{{1 uninitialized field}}
  67. : allocaPtr(static_cast<int *>(__builtin_alloca(sizeof(int)))) {}
  68. };
  69. void fTypedAllocaTest1() {
  70. TypedAllocaTest1();
  71. }
  72. struct TypedAllocaTest2 {
  73. int *allocaPtr;
  74. int dontGetFilteredByNonPedanticMode = 0;
  75. TypedAllocaTest2()
  76. : allocaPtr(static_cast<int *>(__builtin_alloca(sizeof(int)))) {
  77. *allocaPtr = 55555;
  78. // All good!
  79. }
  80. };
  81. void fTypedAllocaTest2() {
  82. TypedAllocaTest2();
  83. }
  84. //===----------------------------------------------------------------------===//
  85. // Heap pointer tests.
  86. //===----------------------------------------------------------------------===//
  87. class HeapPointerTest1 {
  88. struct RecordType {
  89. // TODO: we'd expect the note: {{uninitialized field 'this->recPtr->y'}}
  90. int x; // no-note
  91. // TODO: we'd expect the note: {{uninitialized field 'this->recPtr->y'}}
  92. int y; // no-note
  93. };
  94. // TODO: we'd expect the note: {{uninitialized pointee 'this->fptr'}}
  95. float *fptr = new float; // no-note
  96. // TODO: we'd expect the note: {{uninitialized pointee 'this->ptr'}}
  97. int *ptr; // no-note
  98. RecordType *recPtr;
  99. public:
  100. // TODO: we'd expect the warning: {{4 uninitialized fields}}
  101. HeapPointerTest1() : ptr(new int), recPtr(new RecordType) { // no-note
  102. }
  103. };
  104. void fHeapPointerTest1() {
  105. HeapPointerTest1();
  106. }
  107. class HeapPointerTest2 {
  108. struct RecordType {
  109. int x;
  110. int y;
  111. };
  112. float *fptr = new float(); // initializes to 0
  113. int *ptr;
  114. RecordType *recPtr;
  115. public:
  116. HeapPointerTest2() : ptr(new int{25}), recPtr(new RecordType{26, 27}) {
  117. // All good!
  118. }
  119. };
  120. void fHeapPointerTest2() {
  121. HeapPointerTest2();
  122. }
  123. //===----------------------------------------------------------------------===//
  124. // Stack pointer tests.
  125. //===----------------------------------------------------------------------===//
  126. class StackPointerTest1 {
  127. public:
  128. struct RecordType {
  129. int x;
  130. int y;
  131. };
  132. private:
  133. int *ptr;
  134. RecordType *recPtr;
  135. public:
  136. StackPointerTest1(int *_ptr, StackPointerTest1::RecordType *_recPtr) : ptr(_ptr), recPtr(_recPtr) {
  137. // All good!
  138. }
  139. };
  140. void fStackPointerTest1() {
  141. int ok_a = 28;
  142. StackPointerTest1::RecordType ok_rec{29, 30};
  143. StackPointerTest1(&ok_a, &ok_rec); // 'a', 'rec.x', 'rec.y' uninitialized
  144. }
  145. #ifdef PEDANTIC
  146. class StackPointerTest2 {
  147. public:
  148. struct RecordType {
  149. int x; // expected-note{{uninitialized field 'this->recPtr->x'}}
  150. int y; // expected-note{{uninitialized field 'this->recPtr->y'}}
  151. };
  152. private:
  153. int *ptr; // expected-note{{uninitialized pointee 'this->ptr'}}
  154. RecordType *recPtr;
  155. public:
  156. StackPointerTest2(int *_ptr, RecordType *_recPtr) : ptr(_ptr), recPtr(_recPtr) { // expected-warning{{3 uninitialized fields}}
  157. }
  158. };
  159. void fStackPointerTest2() {
  160. int a;
  161. StackPointerTest2::RecordType rec;
  162. StackPointerTest2(&a, &rec); // 'a', 'rec.x', 'rec.y' uninitialized
  163. }
  164. #else
  165. class StackPointerTest2 {
  166. public:
  167. struct RecordType {
  168. int x;
  169. int y;
  170. };
  171. private:
  172. int *ptr;
  173. RecordType *recPtr;
  174. public:
  175. StackPointerTest2(int *_ptr, RecordType *_recPtr) : ptr(_ptr), recPtr(_recPtr) {
  176. }
  177. };
  178. void fStackPointerTest2() {
  179. int a;
  180. StackPointerTest2::RecordType rec;
  181. StackPointerTest2(&a, &rec); // 'a', 'rec.x', 'rec.y' uninitialized
  182. }
  183. #endif // PEDANTIC
  184. class UninitPointerTest {
  185. struct RecordType {
  186. int x;
  187. int y;
  188. };
  189. int *ptr; // expected-note{{uninitialized pointer 'this->ptr'}}
  190. RecordType *recPtr;
  191. public:
  192. UninitPointerTest() : recPtr(new RecordType{13, 13}) { // expected-warning{{1 uninitialized field}}
  193. }
  194. };
  195. void fUninitPointerTest() {
  196. UninitPointerTest();
  197. }
  198. struct CharPointerTest {
  199. const char *str;
  200. int dontGetFilteredByNonPedanticMode = 0;
  201. CharPointerTest() : str("") {}
  202. };
  203. void fCharPointerTest() {
  204. CharPointerTest();
  205. }
  206. struct CyclicPointerTest1 {
  207. int *ptr;
  208. CyclicPointerTest1() : ptr(reinterpret_cast<int *>(&ptr)) {}
  209. };
  210. void fCyclicPointerTest1() {
  211. CyclicPointerTest1();
  212. }
  213. struct CyclicPointerTest2 {
  214. int **pptr; // no-crash
  215. CyclicPointerTest2() : pptr(reinterpret_cast<int **>(&pptr)) {}
  216. };
  217. void fCyclicPointerTest2() {
  218. CyclicPointerTest2();
  219. }
  220. //===----------------------------------------------------------------------===//
  221. // Void pointer tests.
  222. //===----------------------------------------------------------------------===//
  223. // Void pointer tests are mainly no-crash tests.
  224. void *malloc(int size);
  225. class VoidPointerTest1 {
  226. void *vptr;
  227. public:
  228. VoidPointerTest1(void *vptr, char) : vptr(vptr) {
  229. // All good!
  230. }
  231. };
  232. void fVoidPointerTest1() {
  233. void *vptr = malloc(sizeof(int));
  234. VoidPointerTest1(vptr, char());
  235. }
  236. class VoidPointerTest2 {
  237. void **vpptr;
  238. public:
  239. VoidPointerTest2(void **vpptr, char) : vpptr(vpptr) {
  240. // All good!
  241. }
  242. };
  243. void fVoidPointerTest2() {
  244. void *vptr = malloc(sizeof(int));
  245. VoidPointerTest2(&vptr, char());
  246. }
  247. class VoidPointerRRefTest1 {
  248. void *&&vptrrref; // expected-note {{here}}
  249. public:
  250. VoidPointerRRefTest1(void *vptr, char) : vptrrref(static_cast<void *&&>(vptr)) { // expected-warning {{binding reference member 'vptrrref' to stack allocated parameter 'vptr'}}
  251. // All good!
  252. }
  253. };
  254. void fVoidPointerRRefTest1() {
  255. void *vptr = malloc(sizeof(int));
  256. VoidPointerRRefTest1(vptr, char());
  257. }
  258. class VoidPointerRRefTest2 {
  259. void **&&vpptrrref; // expected-note {{here}}
  260. public:
  261. VoidPointerRRefTest2(void **vptr, char) : vpptrrref(static_cast<void **&&>(vptr)) { // expected-warning {{binding reference member 'vpptrrref' to stack allocated parameter 'vptr'}}
  262. // All good!
  263. }
  264. };
  265. void fVoidPointerRRefTest2() {
  266. void *vptr = malloc(sizeof(int));
  267. VoidPointerRRefTest2(&vptr, char());
  268. }
  269. class VoidPointerLRefTest {
  270. void *&vptrrref; // expected-note {{here}}
  271. public:
  272. VoidPointerLRefTest(void *vptr, char) : vptrrref(static_cast<void *&>(vptr)) { // expected-warning {{binding reference member 'vptrrref' to stack allocated parameter 'vptr'}}
  273. // All good!
  274. }
  275. };
  276. void fVoidPointerLRefTest() {
  277. void *vptr = malloc(sizeof(int));
  278. VoidPointerLRefTest(vptr, char());
  279. }
  280. struct CyclicVoidPointerTest {
  281. void *vptr; // no-crash
  282. CyclicVoidPointerTest() : vptr(&vptr) {}
  283. };
  284. void fCyclicVoidPointerTest() {
  285. CyclicVoidPointerTest();
  286. }
  287. struct IntDynTypedVoidPointerTest1 {
  288. void *vptr; // expected-note{{uninitialized pointee 'static_cast<int *>(this->vptr)'}}
  289. int dontGetFilteredByNonPedanticMode = 0;
  290. IntDynTypedVoidPointerTest1(void *vptr) : vptr(vptr) {} // expected-warning{{1 uninitialized field}}
  291. };
  292. void fIntDynTypedVoidPointerTest1() {
  293. int a;
  294. IntDynTypedVoidPointerTest1 tmp(&a);
  295. }
  296. struct RecordDynTypedVoidPointerTest {
  297. struct RecordType {
  298. int x; // expected-note{{uninitialized field 'static_cast<struct RecordDynTypedVoidPointerTest::RecordType *>(this->vptr)->x'}}
  299. int y; // expected-note{{uninitialized field 'static_cast<struct RecordDynTypedVoidPointerTest::RecordType *>(this->vptr)->y'}}
  300. };
  301. void *vptr;
  302. int dontGetFilteredByNonPedanticMode = 0;
  303. RecordDynTypedVoidPointerTest(void *vptr) : vptr(vptr) {} // expected-warning{{2 uninitialized fields}}
  304. };
  305. void fRecordDynTypedVoidPointerTest() {
  306. RecordDynTypedVoidPointerTest::RecordType a;
  307. RecordDynTypedVoidPointerTest tmp(&a);
  308. }
  309. struct NestedNonVoidDynTypedVoidPointerTest {
  310. struct RecordType {
  311. int x; // expected-note{{uninitialized field 'static_cast<struct NestedNonVoidDynTypedVoidPointerTest::RecordType *>(this->vptr)->x'}}
  312. int y; // expected-note{{uninitialized field 'static_cast<struct NestedNonVoidDynTypedVoidPointerTest::RecordType *>(this->vptr)->y'}}
  313. void *vptr; // expected-note{{uninitialized pointee 'static_cast<char *>(static_cast<struct NestedNonVoidDynTypedVoidPointerTest::RecordType *>(this->vptr)->vptr)'}}
  314. };
  315. void *vptr;
  316. int dontGetFilteredByNonPedanticMode = 0;
  317. NestedNonVoidDynTypedVoidPointerTest(void *vptr, void *c) : vptr(vptr) {
  318. static_cast<RecordType *>(vptr)->vptr = c; // expected-warning{{3 uninitialized fields}}
  319. }
  320. };
  321. void fNestedNonVoidDynTypedVoidPointerTest() {
  322. NestedNonVoidDynTypedVoidPointerTest::RecordType a;
  323. char c;
  324. NestedNonVoidDynTypedVoidPointerTest tmp(&a, &c);
  325. }
  326. //===----------------------------------------------------------------------===//
  327. // Multipointer tests.
  328. //===----------------------------------------------------------------------===//
  329. #ifdef PEDANTIC
  330. class MultiPointerTest1 {
  331. public:
  332. struct RecordType {
  333. int x;
  334. int y;
  335. };
  336. private:
  337. RecordType **mptr; // expected-note{{uninitialized pointee 'this->mptr'}}
  338. public:
  339. MultiPointerTest1(RecordType **p, int) : mptr(p) { // expected-warning{{1 uninitialized field}}
  340. }
  341. };
  342. void fMultiPointerTest1() {
  343. MultiPointerTest1::RecordType *p1;
  344. MultiPointerTest1::RecordType **mptr = &p1;
  345. MultiPointerTest1(mptr, int()); // '*mptr' uninitialized
  346. }
  347. #else
  348. class MultiPointerTest1 {
  349. public:
  350. struct RecordType {
  351. int x;
  352. int y;
  353. };
  354. private:
  355. RecordType **mptr;
  356. public:
  357. MultiPointerTest1(RecordType **p, int) : mptr(p) {}
  358. };
  359. void fMultiPointerTest1() {
  360. MultiPointerTest1::RecordType *p1;
  361. MultiPointerTest1::RecordType **mptr = &p1;
  362. MultiPointerTest1(mptr, int()); // '*mptr' uninitialized
  363. }
  364. #endif // PEDANTIC
  365. #ifdef PEDANTIC
  366. class MultiPointerTest2 {
  367. public:
  368. struct RecordType {
  369. int x; // expected-note{{uninitialized field 'this->mptr->x'}}
  370. int y; // expected-note{{uninitialized field 'this->mptr->y'}}
  371. };
  372. private:
  373. RecordType **mptr;
  374. public:
  375. MultiPointerTest2(RecordType **p, int) : mptr(p) { // expected-warning{{2 uninitialized fields}}
  376. }
  377. };
  378. void fMultiPointerTest2() {
  379. MultiPointerTest2::RecordType i;
  380. MultiPointerTest2::RecordType *p1 = &i;
  381. MultiPointerTest2::RecordType **mptr = &p1;
  382. MultiPointerTest2(mptr, int()); // '**mptr' uninitialized
  383. }
  384. #else
  385. class MultiPointerTest2 {
  386. public:
  387. struct RecordType {
  388. int x;
  389. int y;
  390. };
  391. private:
  392. RecordType **mptr;
  393. public:
  394. MultiPointerTest2(RecordType **p, int) : mptr(p) {
  395. }
  396. };
  397. void fMultiPointerTest2() {
  398. MultiPointerTest2::RecordType i;
  399. MultiPointerTest2::RecordType *p1 = &i;
  400. MultiPointerTest2::RecordType **mptr = &p1;
  401. MultiPointerTest2(mptr, int()); // '**mptr' uninitialized
  402. }
  403. #endif // PEDANTIC
  404. class MultiPointerTest3 {
  405. public:
  406. struct RecordType {
  407. int x;
  408. int y;
  409. };
  410. private:
  411. RecordType **mptr;
  412. public:
  413. MultiPointerTest3(RecordType **p, int) : mptr(p) {
  414. // All good!
  415. }
  416. };
  417. void fMultiPointerTest3() {
  418. MultiPointerTest3::RecordType i{31, 32};
  419. MultiPointerTest3::RecordType *p1 = &i;
  420. MultiPointerTest3::RecordType **mptr = &p1;
  421. MultiPointerTest3(mptr, int()); // '**mptr' uninitialized
  422. }
  423. //===----------------------------------------------------------------------===//
  424. // Incomplete pointee tests.
  425. //===----------------------------------------------------------------------===//
  426. class IncompleteType;
  427. struct IncompletePointeeTypeTest {
  428. IncompleteType *pImpl; //no-crash
  429. int dontGetFilteredByNonPedanticMode = 0;
  430. IncompletePointeeTypeTest(IncompleteType *A) : pImpl(A) {}
  431. };
  432. void fIncompletePointeeTypeTest(void *ptr) {
  433. IncompletePointeeTypeTest(reinterpret_cast<IncompleteType *>(ptr));
  434. }
  435. //===----------------------------------------------------------------------===//
  436. // Function pointer tests.
  437. //===----------------------------------------------------------------------===//
  438. struct FunctionPointerWithDifferentDynTypeTest {
  439. using Func1 = void *(*)();
  440. using Func2 = int *(*)();
  441. Func1 f; // no-crash
  442. FunctionPointerWithDifferentDynTypeTest(Func2 f) : f((Func1)f) {}
  443. };
  444. // Note that there isn't a function calling the constructor of
  445. // FunctionPointerWithDifferentDynTypeTest, because a crash could only be
  446. // reproduced without it.
  447. //===----------------------------------------------------------------------===//
  448. // Member pointer tests.
  449. //===----------------------------------------------------------------------===//
  450. struct UsefulFunctions {
  451. int a, b;
  452. void print() {}
  453. void dump() {}
  454. };
  455. #ifdef PEDANTIC
  456. struct PointerToMemberFunctionTest1 {
  457. void (UsefulFunctions::*f)(void); // expected-note{{uninitialized field 'this->f'}}
  458. PointerToMemberFunctionTest1() {}
  459. };
  460. void fPointerToMemberFunctionTest1() {
  461. PointerToMemberFunctionTest1(); // expected-warning{{1 uninitialized field}}
  462. }
  463. struct PointerToMemberFunctionTest2 {
  464. void (UsefulFunctions::*f)(void);
  465. PointerToMemberFunctionTest2(void (UsefulFunctions::*f)(void)) : f(f) {
  466. // All good!
  467. }
  468. };
  469. void fPointerToMemberFunctionTest2() {
  470. void (UsefulFunctions::*f)(void) = &UsefulFunctions::print;
  471. PointerToMemberFunctionTest2 a(f);
  472. }
  473. struct MultiPointerToMemberFunctionTest1 {
  474. void (UsefulFunctions::**f)(void); // expected-note{{uninitialized pointer 'this->f'}}
  475. MultiPointerToMemberFunctionTest1() {}
  476. };
  477. void fMultiPointerToMemberFunctionTest1() {
  478. MultiPointerToMemberFunctionTest1(); // expected-warning{{1 uninitialized field}}
  479. }
  480. struct MultiPointerToMemberFunctionTest2 {
  481. void (UsefulFunctions::**f)(void);
  482. MultiPointerToMemberFunctionTest2(void (UsefulFunctions::**f)(void)) : f(f) {
  483. // All good!
  484. }
  485. };
  486. void fMultiPointerToMemberFunctionTest2() {
  487. void (UsefulFunctions::*f)(void) = &UsefulFunctions::print;
  488. MultiPointerToMemberFunctionTest2 a(&f);
  489. }
  490. struct PointerToMemberDataTest1 {
  491. int UsefulFunctions::*d; // expected-note{{uninitialized field 'this->d'}}
  492. PointerToMemberDataTest1() {}
  493. };
  494. void fPointerToMemberDataTest1() {
  495. PointerToMemberDataTest1(); // expected-warning{{1 uninitialized field}}
  496. }
  497. struct PointerToMemberDataTest2 {
  498. int UsefulFunctions::*d;
  499. PointerToMemberDataTest2(int UsefulFunctions::*d) : d(d) {
  500. // All good!
  501. }
  502. };
  503. void fPointerToMemberDataTest2() {
  504. int UsefulFunctions::*d = &UsefulFunctions::a;
  505. PointerToMemberDataTest2 a(d);
  506. }
  507. struct MultiPointerToMemberDataTest1 {
  508. int UsefulFunctions::**d; // expected-note{{uninitialized pointer 'this->d'}}
  509. MultiPointerToMemberDataTest1() {}
  510. };
  511. void fMultiPointerToMemberDataTest1() {
  512. MultiPointerToMemberDataTest1(); // expected-warning{{1 uninitialized field}}
  513. }
  514. struct MultiPointerToMemberDataTest2 {
  515. int UsefulFunctions::**d;
  516. MultiPointerToMemberDataTest2(int UsefulFunctions::**d) : d(d) {
  517. // All good!
  518. }
  519. };
  520. void fMultiPointerToMemberDataTest2() {
  521. int UsefulFunctions::*d = &UsefulFunctions::a;
  522. MultiPointerToMemberDataTest2 a(&d);
  523. }
  524. #endif // PEDANTIC
  525. //===----------------------------------------------------------------------===//
  526. // Tests for list-like records.
  527. //===----------------------------------------------------------------------===//
  528. class ListTest1 {
  529. public:
  530. struct Node {
  531. Node *next = nullptr; // no crash
  532. int i;
  533. };
  534. private:
  535. Node *head = nullptr;
  536. public:
  537. ListTest1() {
  538. // All good!
  539. }
  540. };
  541. void fListTest1() {
  542. ListTest1();
  543. }
  544. class ListTest2 {
  545. public:
  546. struct Node {
  547. Node *next = nullptr;
  548. int i; // expected-note{{uninitialized field 'this->head->i'}}
  549. };
  550. private:
  551. Node *head = nullptr;
  552. public:
  553. ListTest2(Node *node, int) : head(node) { // expected-warning{{1 uninitialized field}}
  554. }
  555. };
  556. void fListTest2() {
  557. ListTest2::Node n;
  558. ListTest2(&n, int());
  559. }
  560. class CyclicList {
  561. public:
  562. struct Node {
  563. Node *next = nullptr;
  564. int i; // expected-note{{uninitialized field 'this->head->i'}}
  565. };
  566. private:
  567. Node *head = nullptr;
  568. public:
  569. CyclicList(Node *node, int) : head(node) { // expected-warning{{1 uninitialized field}}
  570. }
  571. };
  572. void fCyclicList() {
  573. /*
  574. n3
  575. / \
  576. this -- n1 -- n2
  577. */
  578. CyclicList::Node n1;
  579. CyclicList::Node n2;
  580. n2.next = &n1;
  581. n2.i = 50;
  582. CyclicList::Node n3;
  583. n3.next = &n2;
  584. n3.i = 50;
  585. n1.next = &n3;
  586. // note that n1.i is uninitialized
  587. CyclicList(&n1, int());
  588. }
  589. struct RingListTest {
  590. RingListTest *next; // no-crash
  591. RingListTest() : next(this) {}
  592. };
  593. void fRingListTest() {
  594. RingListTest();
  595. }
  596. //===----------------------------------------------------------------------===//
  597. // Tests for classes containing references.
  598. //===----------------------------------------------------------------------===//
  599. class ReferenceTest1 {
  600. public:
  601. struct RecordType {
  602. int x;
  603. int y;
  604. };
  605. private:
  606. RecordType &lref;
  607. RecordType &&rref;
  608. public:
  609. ReferenceTest1(RecordType &lref, RecordType &rref) : lref(lref), rref(static_cast<RecordType &&>(rref)) {
  610. // All good!
  611. }
  612. };
  613. void fReferenceTest1() {
  614. ReferenceTest1::RecordType d{33, 34};
  615. ReferenceTest1(d, d);
  616. }
  617. #ifdef PEDANTIC
  618. class ReferenceTest2 {
  619. public:
  620. struct RecordType {
  621. int x; // expected-note{{uninitialized field 'this->lref.x'}}
  622. int y; // expected-note{{uninitialized field 'this->lref.y'}}
  623. };
  624. private:
  625. RecordType &lref;
  626. RecordType &&rref;
  627. public:
  628. ReferenceTest2(RecordType &lref, RecordType &rref)
  629. : lref(lref), rref(static_cast<RecordType &&>(rref)) { // expected-warning{{2 uninitialized fields}}
  630. }
  631. };
  632. void fReferenceTest2() {
  633. ReferenceTest2::RecordType c;
  634. ReferenceTest2(c, c);
  635. }
  636. #else
  637. class ReferenceTest2 {
  638. public:
  639. struct RecordType {
  640. int x;
  641. int y;
  642. };
  643. private:
  644. RecordType &lref;
  645. RecordType &&rref;
  646. public:
  647. ReferenceTest2(RecordType &lref, RecordType &rref)
  648. : lref(lref), rref(static_cast<RecordType &&>(rref)) {
  649. }
  650. };
  651. void fReferenceTest2() {
  652. ReferenceTest2::RecordType c;
  653. ReferenceTest2(c, c);
  654. }
  655. #endif // PEDANTIC
  656. class ReferenceTest3 {
  657. public:
  658. struct RecordType {
  659. int x; // expected-note{{uninitialized field 'this->lref.x'}}
  660. int y; // expected-note{{uninitialized field 'this->lref.y'}}
  661. };
  662. private:
  663. RecordType &lref;
  664. RecordType &&rref;
  665. public:
  666. ReferenceTest3(RecordType &lref, RecordType &rref)
  667. : lref(lref), rref(static_cast<RecordType &&>(rref)) { // expected-warning{{2 uninitialized fields}}
  668. }
  669. };
  670. void fReferenceTest3() {
  671. ReferenceTest3::RecordType c, d{35, 36};
  672. ReferenceTest3(c, d);
  673. }
  674. class ReferenceTest4 {
  675. public:
  676. struct RecordType {
  677. int x; // expected-note{{uninitialized field 'this->rref.x'}}
  678. int y; // expected-note{{uninitialized field 'this->rref.y'}}
  679. };
  680. private:
  681. RecordType &lref;
  682. RecordType &&rref;
  683. public:
  684. ReferenceTest4(RecordType &lref, RecordType &rref)
  685. : lref(lref), rref(static_cast<RecordType &&>(rref)) { // expected-warning{{2 uninitialized fields}}
  686. }
  687. };
  688. void fReferenceTest5() {
  689. ReferenceTest4::RecordType c, d{37, 38};
  690. ReferenceTest4(d, c);
  691. }