Debian Codegraph @

clang/test/SemaCXX/warn-unsafe-buffer-usage-warning-data-invocation.cpp

raw
1 // RUN: %clang_cc1 -std=c++20 -Wno-all -Wunsafe-buffer-usage -Wno-unsafe-buffer-usage-in-container\
2 // RUN: -fsafe-buffer-usage-suggestions \
3 // RUN: -fblocks -include %s -verify %s
4
5 // RUN: %clang -x c++ -frtti -fsyntax-only -fblocks -include %s %s 2>&1 | FileCheck --allow-empty %s
6 // RUN: %clang_cc1 -std=c++11 -fblocks -include %s %s 2>&1 | FileCheck --allow-empty %s
7 // RUN: %clang_cc1 -std=c++20 -fblocks -include %s %s 2>&1 | FileCheck --allow-empty %s
8 // CHECK-NOT: [-Wunsafe-buffer-usage]
9
10 #ifndef INCLUDED
11 #define INCLUDED
12 #pragma clang system_header
13
14 // no spanification warnings for system headers
15 #else
16
17 typedef __INTPTR_TYPE__ intptr_t;
18
19 namespace std {
20 class type_info;
21 class bad_cast;
22 class bad_typeid;
23 }
24 using size_t = __typeof(sizeof(int));
25 void *malloc(size_t);
26
27 void foo(int v) {
28 }
29
30 void foo(int *p){}
31
32 namespace std{
33 template <typename T> class span {
34
35 T *elements;
36
37 span(T *, unsigned){}
38
39 public:
40
41 constexpr span<T> subspan(size_t offset, size_t count) const {
42 return span<T> (elements+offset, count); // expected-warning{{unsafe pointer arithmetic}}
43 }
44
45 constexpr T* data() const noexcept {
46 return elements;
47 }
48
49
50 constexpr T* hello() const noexcept {
51 return elements;
52 }
53 };
54
55 template <typename T> class span_duplicate {
56 span_duplicate(T *, unsigned){}
57
58 T array[10];
59
60 public:
61
62 T* data() {
63 return array;
64 }
65
66 };
67 }
68
69 using namespace std;
70
71 class A {
72 int a, b, c;
73 };
74
75 class B {
76 int a, b, c;
77 };
78
79 struct Base {
80 virtual ~Base() = default;
81 };
82
83 struct Derived: Base {
84 int d;
85 };
86
87 void cast_without_data(int *ptr) {
88 A *a = (A*) ptr;
89 float *p = (float*) ptr;
90 }
91
92 void warned_patterns(std::span<int> span_ptr, std::span<Base> base_span, span<int> span_without_qual) {
93 A *a1 = (A*)span_ptr.data(); // expected-warning{{unsafe invocation of span::data}}
94 a1 = (A*)span_ptr.data(); // expected-warning{{unsafe invocation of span::data}}
95
96 a1 = (A*)(span_ptr.data()); // expected-warning{{unsafe invocation of span::data}}
97 A *a2 = (A*) (span_without_qual.data()); // expected-warning{{unsafe invocation of span::data}}
98
99 a2 = (A*) span_without_qual.data(); // expected-warning{{unsafe invocation of span::data}}
100
101 // TODO:: Should we warn when we cast from base to derived type?
102 Derived *b = dynamic_cast<Derived*> (base_span.data());// expected-warning{{unsafe invocation of span::data}}
103
104 // TODO:: This pattern is safe. We can add special handling for it, if we decide this
105 // is the recommended fixit for the unsafe invocations.
106 A *a3 = (A*)span_ptr.subspan(0, sizeof(A)).data(); // expected-warning{{unsafe invocation of span::data}}
107 }
108
109 void not_warned_patterns(std::span<A> span_ptr, std::span<Base> base_span) {
110 int *p = (int*) span_ptr.data(); // Cast to a smaller type
111
112 B *b = (B*) span_ptr.data(); // Cast to a type of same size.
113
114 p = (int*) span_ptr.data();
115 A *a = (A*) span_ptr.hello(); // Invoking other methods.
116
117 intptr_t k = (intptr_t) span_ptr.data();
118 k = (intptr_t) (span_ptr.data());
119 }
120
121 // We do not want to warn about other types
122 void other_classes(std::span_duplicate<int> span_ptr) {
123 int *p;
124 A *a = (A*)span_ptr.data();
125 a = (A*)span_ptr.data();
126 }
127
128 // Potential source for false negatives
129
130 A false_negatives(std::span<int> span_pt, span<A> span_A) {
131 int *ptr = span_pt.data();
132
133 A *a1 = (A*)ptr; //TODO: We want to warn here eventually.
134
135 A *a2= span_A.data();
136 return *a2; // TODO: Can cause OOB if span_pt is empty
137
138 }
139 #endif