You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: test/testautovariables.cpp
+93Lines changed: 93 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -4452,6 +4452,99 @@ class TestAutoVariables : public TestFixture {
4452
4452
"}\n");
4453
4453
ASSERT_EQUALS("[test.cpp:5:14] -> [test.cpp:5:18] -> [test.cpp:6:7]: (error) Using pointer that is a temporary. [danglingTemporaryLifetime]\n",
4454
4454
errout_str());
4455
+
4456
+
check("struct A { const int* data[2]; };\n"
4457
+
"A g() {\n"
4458
+
" int x = 0;\n"
4459
+
" A a;\n"
4460
+
" a.data[0] = &x;\n"
4461
+
" return a;\n"
4462
+
"}\n");
4463
+
ASSERT_EQUALS("[test.cpp:5:17] -> [test.cpp:3:9] -> [test.cpp:6:12]: (error) Returning object that points to local variable 'x' that will be invalid when returning. [returnDanglingLifetime]\n",
4464
+
errout_str());
4465
+
4466
+
check("struct A { const int* data[2]; };\n"
4467
+
"A g() {\n"
4468
+
" int x = 0;\n"
4469
+
" A a[2];\n"
4470
+
" a[0].data[0] = &x;\n"
4471
+
" return a[0];\n"
4472
+
"}\n");
4473
+
ASSERT_EQUALS("[test.cpp:5:20] -> [test.cpp:3:9] -> [test.cpp:6:13]: (error) Returning object that points to local variable 'x' that will be invalid when returning. [returnDanglingLifetime]\n",
4474
+
errout_str());
4475
+
4476
+
check("struct A { const int* data[2]; };\n"
4477
+
"A* g() {\n"
4478
+
" int x = 0;\n"
4479
+
" static A arr[2];\n"
4480
+
" arr[0].data[0] = &x;\n"
4481
+
" return arr;\n"
4482
+
"}\n");
4483
+
ASSERT_EQUALS("[test.cpp:5:22] -> [test.cpp:3:9] -> [test.cpp:6:12]: (error) Returning pointer to local variable 'x' that will be invalid when returning. [returnDanglingLifetime]\n",
4484
+
errout_str());
4485
+
4486
+
check("struct A { const int* p; };\n"
4487
+
"A g(int i) {\n"
4488
+
" int x = 0;\n"
4489
+
" A arr[2];\n"
4490
+
" arr[i].p = &x;\n"
4491
+
" return arr[i];\n"
4492
+
"}\n");
4493
+
ASSERT_EQUALS("[test.cpp:5:16] -> [test.cpp:3:9] -> [test.cpp:6:15]: (error) Returning object that points to local variable 'x' that will be invalid when returning. [returnDanglingLifetime]\n",
4494
+
errout_str());
4495
+
4496
+
check("struct A { const int* p; };\n"
4497
+
"A* g(int i) {\n"
4498
+
" int x = 0;\n"
4499
+
" static A arr[2];\n"
4500
+
" arr[i].p = &x;\n"
4501
+
" return arr;\n"
4502
+
"}\n");
4503
+
ASSERT_EQUALS("[test.cpp:5:16] -> [test.cpp:3:9] -> [test.cpp:6:12]: (error) Returning pointer to local variable 'x' that will be invalid when returning. [returnDanglingLifetime]\n",
4504
+
errout_str());
4505
+
4506
+
check("struct A { const int* p; };\n"
4507
+
"struct B { A arr[2]; };\n"
4508
+
"B g(int i) {\n"
4509
+
" int x = 0;\n"
4510
+
" B b;\n"
4511
+
" b.arr[i].p = &x;\n"
4512
+
" return b;\n"
4513
+
"}\n");
4514
+
ASSERT_EQUALS("[test.cpp:6:18] -> [test.cpp:4:9] -> [test.cpp:7:12]: (error) Returning object that points to local variable 'x' that will be invalid when returning. [returnDanglingLifetime]\n",
4515
+
errout_str());
4516
+
4517
+
check("struct A { const int* p; };\n"
4518
+
"A* g() {\n"
4519
+
" int x = 0;\n"
4520
+
" static A a;\n"
4521
+
" A* ap = &a;\n"
4522
+
" ap->p = &x;\n"
4523
+
" (*ap).p = &x;\n"
4524
+
" return ap;\n"
4525
+
"}\n");
4526
+
ASSERT_EQUALS("", errout_str());
4527
+
4528
+
check("struct A { int* a; int* b; };\n"
4529
+
"static A g;\n"
4530
+
"int* f() {\n"
4531
+
" int x = 0;\n"
4532
+
" g.a = &x;\n"
4533
+
" return g.b;\n"
4534
+
"}\n");
4535
+
ASSERT_EQUALS("[test.cpp:5:11] -> [test.cpp:4:9] -> [test.cpp:5:6]: (error) Non-local variable 'g.a' will use pointer to local variable 'x'. [danglingLifetime]\n",
4536
+
errout_str());
4537
+
4538
+
check("struct A { int* a; int* b; };\n"
4539
+
"static A g;\n"
4540
+
"int* f() {\n"
4541
+
" int x = 0;\n"
4542
+
" g.a = &x;\n"
4543
+
" return g.a;\n"
4544
+
"}\n");
4545
+
ASSERT_EQUALS("[test.cpp:5:11] -> [test.cpp:4:9] -> [test.cpp:5:6]: (error) Non-local variable 'g.a' will use pointer to local variable 'x'. [danglingLifetime]\n"
4546
+
"[test.cpp:5:11] -> [test.cpp:4:9] -> [test.cpp:6:13]: (error) Returning pointer to local variable 'x' that will be invalid when returning. [returnDanglingLifetime]\n",
0 commit comments