Debian Codegraph @

clang/test/Analysis/undefined-new-element.cpp

raw
1 // RUN: %clang_analyze_cc1 %s -analyzer-checker=core.uninitialized.NewArraySize -analyzer-output=text -verify
2
3 #include "Inputs/system-header-simulator-cxx.h"
4
5 void checkUndefinedElmenetCountValue() {
6 int n;
7 // expected-note@-1{{'n' declared without an initial value}}
8
9 int *arr = new int[n]; // expected-warning{{Element count in new[] is a garbage value}}
10 // expected-note@-1{{Element count in new[] is a garbage value}}
11 }
12
13 void checkUndefinedElmenetCountMultiDimensionalValue() {
14 int n;
15 // expected-note@-1{{'n' declared without an initial value}}
16
17 auto *arr = new int[n][5]; // expected-warning{{Element count in new[] is a garbage value}}
18 // expected-note@-1{{Element count in new[] is a garbage value}}
19 }
20
21 void checkUndefinedElmenetCountReference() {
22 int n;
23 // expected-note@-1{{'n' declared without an initial value}}
24
25 int &ref = n;
26 // expected-note@-1{{'ref' initialized here}}
27
28 int *arr = new int[ref]; // expected-warning{{Element count in new[] is a garbage value}}
29 // expected-note@-1{{Element count in new[] is a garbage value}}
30 }
31
32 void checkUndefinedElmenetCountMultiDimensionalReference() {
33 int n;
34 // expected-note@-1{{'n' declared without an initial value}}
35
36 int &ref = n;
37 // expected-note@-1{{'ref' initialized here}}
38
39 auto *arr = new int[ref][5]; // expected-warning{{Element count in new[] is a garbage value}}
40 // expected-note@-1{{Element count in new[] is a garbage value}}
41 }
42
43 int foo() {
44 int n;
45
46 return n;
47 }
48
49 void checkUndefinedElmenetCountFunction() {
50 int *arr = new int[foo()]; // expected-warning{{Element count in new[] is a garbage value}}
51 // expected-note@-1{{Element count in new[] is a garbage value}}
52 }
53
54 void checkUndefinedElmenetCountMultiDimensionalFunction() {
55 auto *arr = new int[foo()][5]; // expected-warning{{Element count in new[] is a garbage value}}
56 // expected-note@-1{{Element count in new[] is a garbage value}}
57 }
58
59 void *malloc(size_t);
60
61 void checkUndefinedPlacementElementCount() {
62 int n;
63 // expected-note@-1{{'n' declared without an initial value}}
64
65 void *buffer = malloc(sizeof(std::string) * 10);
66 std::string *p =
67 ::new (buffer) std::string[n]; // expected-warning{{Element count in new[] is a garbage value}}
68 // expected-note@-1{{Element count in new[] is a garbage value}}
69 }