| 1 |
// RUN: %clang_cc1 -std=c++20 -Wunsafe-buffer-usage \ |
| 2 |
// RUN: -fsafe-buffer-usage-suggestions -verify %s |
| 3 |
|
| 4 |
[[clang::unsafe_buffer_usage]] |
| 5 |
void deprecatedFunction3(); |
| 6 |
|
| 7 |
void deprecatedFunction4(int z); |
| 8 |
|
| 9 |
void someFunction(); |
| 10 |
|
| 11 |
[[clang::unsafe_buffer_usage]] |
| 12 |
void overloading(int* x); |
| 13 |
|
| 14 |
void overloading(char c[]); |
| 15 |
|
| 16 |
void overloading(int* x, int size); |
| 17 |
|
| 18 |
[[clang::unsafe_buffer_usage]] |
| 19 |
void deprecatedFunction4(int z); |
| 20 |
|
| 21 |
void caller(int z, int* x, int size, char c[]) { |
| 22 |
deprecatedFunction3(); // expected-warning{{function introduces unsafe buffer manipulation}} |
| 23 |
deprecatedFunction4(z); // expected-warning{{function introduces unsafe buffer manipulation}} |
| 24 |
someFunction(); |
| 25 |
|
| 26 |
overloading(x); // expected-warning{{function introduces unsafe buffer manipulation}} |
| 27 |
overloading(x, size); |
| 28 |
overloading(c); |
| 29 |
} |
| 30 |
|
| 31 |
[[clang::unsafe_buffer_usage]] |
| 32 |
void overloading(char c[]); |
| 33 |
|
| 34 |
// Test variadics |
| 35 |
[[clang::unsafe_buffer_usage]] |
| 36 |
void testVariadics(int *ptr, ...); |
| 37 |
|
| 38 |
template<typename T, typename... Args> |
| 39 |
[[clang::unsafe_buffer_usage]] |
| 40 |
T adder(T first, Args... args); |
| 41 |
|
| 42 |
template <typename T> |
| 43 |
void foo(T t) {} |
| 44 |
|
| 45 |
template<> |
| 46 |
[[clang::unsafe_buffer_usage]] |
| 47 |
void foo<int *>(int *t) {} |
| 48 |
|
| 49 |
void caller1(int *p, int *q) { |
| 50 |
testVariadics(p, q); // expected-warning{{function introduces unsafe buffer manipulation}} |
| 51 |
adder(p, q); // expected-warning{{function introduces unsafe buffer manipulation}} |
| 52 |
|
| 53 |
int x; |
| 54 |
foo(x); |
| 55 |
foo(&x); // expected-warning{{function introduces unsafe buffer manipulation}} |
| 56 |
} |
| 57 |
|
| 58 |
// Test virtual functions |
| 59 |
class BaseClass { |
| 60 |
public: |
| 61 |
[[clang::unsafe_buffer_usage]] |
| 62 |
virtual void func() {} |
| 63 |
|
| 64 |
virtual void func1() {} |
| 65 |
}; |
| 66 |
|
| 67 |
class DerivedClass : public BaseClass { |
| 68 |
public: |
| 69 |
void func() {} |
| 70 |
|
| 71 |
[[clang::unsafe_buffer_usage]] |
| 72 |
void func1() {} |
| 73 |
}; |
| 74 |
|
| 75 |
void testInheritance() { |
| 76 |
DerivedClass DC; |
| 77 |
DC.func(); |
| 78 |
DC.func1(); // expected-warning{{function introduces unsafe buffer manipulation}} |
| 79 |
|
| 80 |
BaseClass *BC; |
| 81 |
BC->func(); // expected-warning{{function introduces unsafe buffer manipulation}} |
| 82 |
BC->func1(); |
| 83 |
|
| 84 |
BC = &DC; |
| 85 |
BC->func(); // expected-warning{{function introduces unsafe buffer manipulation}} |
| 86 |
BC->func1(); |
| 87 |
} |
| 88 |
|
| 89 |
class UnsafeMembers { |
| 90 |
public: |
| 91 |
UnsafeMembers() {} |
| 92 |
|
| 93 |
[[clang::unsafe_buffer_usage]] |
| 94 |
UnsafeMembers(int) {} |
| 95 |
|
| 96 |
[[clang::unsafe_buffer_usage]] |
| 97 |
explicit operator int() { return 0; } |
| 98 |
|
| 99 |
[[clang::unsafe_buffer_usage]] |
| 100 |
void Method() {} |
| 101 |
|
| 102 |
[[clang::unsafe_buffer_usage]] |
| 103 |
void operator()() {} |
| 104 |
|
| 105 |
[[clang::unsafe_buffer_usage]] |
| 106 |
int operator+(UnsafeMembers) { return 0; } |
| 107 |
}; |
| 108 |
|
| 109 |
template <class... Vs> |
| 110 |
int testFoldExpression(Vs&&... v) { |
| 111 |
return (... + v); // expected-warning{{function introduces unsafe buffer manipulation}} |
| 112 |
} |
| 113 |
|
| 114 |
// https://github.com/llvm/llvm-project/issues/80482 |
| 115 |
void testClassMembers() { |
| 116 |
UnsafeMembers(3); // expected-warning{{function introduces unsafe buffer manipulation}} |
| 117 |
|
| 118 |
(void)static_cast<int>(UnsafeMembers()); // expected-warning{{function introduces unsafe buffer manipulation}} |
| 119 |
|
| 120 |
UnsafeMembers().Method(); // expected-warning{{function introduces unsafe buffer manipulation}} |
| 121 |
|
| 122 |
UnsafeMembers()(); // expected-warning{{function introduces unsafe buffer manipulation}} |
| 123 |
|
| 124 |
testFoldExpression(UnsafeMembers(), UnsafeMembers()); |
| 125 |
} |