1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
// RUN: %check_clang_tidy %s cppcoreguidelines-owning-memory %t \
// RUN: -config='{CheckOptions: \
// RUN: [{key: cppcoreguidelines-owning-memory.LegacyResourceProducers, value: "::malloc;::aligned_alloc;::realloc;::calloc;::fopen;::freopen;::tmpfile"}, \
// RUN: {key: cppcoreguidelines-owning-memory.LegacyResourceConsumers, value: "::free;::realloc;::freopen;::fclose"}]}' \
// RUN: -- -nostdlib -nostdinc++
namespace gsl {
template <class T>
using owner = T;
} // namespace gsl
extern "C" {
using size_t = decltype(sizeof(void*));
using FILE = int;
void *malloc(size_t ByteCount);
void *aligned_alloc(size_t Alignment, size_t Size);
void *calloc(size_t Count, size_t SizeSingle);
void *realloc(void *Resource, size_t NewByteCount);
void free(void *Resource);
FILE *tmpfile(void);
FILE *fopen(const char *filename, const char *mode);
FILE *freopen(const char *filename, const char *mode, FILE *stream);
void fclose(FILE *Resource);
}
namespace std {
using ::FILE;
using ::size_t;
using ::fclose;
using ::fopen;
using ::freopen;
using ::tmpfile;
using ::aligned_alloc;
using ::calloc;
using ::free;
using ::malloc;
using ::realloc;
} // namespace std
void nonOwningCall(int *Resource, size_t Size) {}
void nonOwningCall(FILE *Resource) {}
void consumesResource(gsl::owner<int *> Resource, size_t Size) {}
void consumesResource(gsl::owner<FILE *> Resource) {}
void testNonCasted(void *Resource) {}
void testNonCastedOwner(gsl::owner<void *> Resource) {}
FILE *fileFactory1() { return ::fopen("new_file.txt", "w"); }
// CHECK-MESSAGES: [[@LINE-1]]:24: warning: returning a newly created resource of type 'FILE *' (aka 'int *') or 'gsl::owner<>' from a function whose return type is not 'gsl::owner<>'
gsl::owner<FILE *> fileFactory2() { return std::fopen("new_file.txt", "w"); } // Ok
int *arrayFactory1() { return (int *)std::malloc(100); }
// CHECK-MESSAGES: [[@LINE-1]]:24: warning: returning a newly created resource of type 'int *' or 'gsl::owner<>' from a function whose return type is not 'gsl::owner<>'
gsl::owner<int *> arrayFactory2() { return (int *)std::malloc(100); } // Ok
void *dataFactory1() { return std::malloc(100); }
// CHECK-MESSAGES: [[@LINE-1]]:24: warning: returning a newly created resource of type 'void *' or 'gsl::owner<>' from a function whose return type is not 'gsl::owner<>'
gsl::owner<void *> dataFactory2() { return std::malloc(100); } // Ok
void test_resource_creators() {
const unsigned int ByteCount = 25 * sizeof(int);
int Bad = 42;
int *IntArray1 = (int *)std::malloc(ByteCount);
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: initializing non-owner 'int *' with a newly created 'gsl::owner<>'
int *IntArray2 = static_cast<int *>(std::malloc(ByteCount)); // Bad
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: initializing non-owner 'int *' with a newly created 'gsl::owner<>'
void *IntArray3 = std::malloc(ByteCount);
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: initializing non-owner 'void *' with a newly created 'gsl::owner<>'
int *IntArray4 = (int *)::malloc(ByteCount);
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: initializing non-owner 'int *' with a newly created 'gsl::owner<>'
int *IntArray5 = static_cast<int *>(::malloc(ByteCount)); // Bad
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: initializing non-owner 'int *' with a newly created 'gsl::owner<>'
void *IntArray6 = ::malloc(ByteCount);
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: initializing non-owner 'void *' with a newly created 'gsl::owner<>'
gsl::owner<int *> IntArray7 = (int *)malloc(ByteCount); // Ok
gsl::owner<void *> IntArray8 = malloc(ByteCount); // Ok
gsl::owner<int *> IntArray9 = &Bad;
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: expected initialization with value of type 'gsl::owner<>'; got 'int *'
nonOwningCall((int *)malloc(ByteCount), 25);
// CHECK-MESSAGES: [[@LINE-1]]:24: warning: initializing non-owner argument of type 'int *' with a newly created 'gsl::owner<>'
nonOwningCall((int *)::malloc(ByteCount), 25);
// CHECK-MESSAGES: [[@LINE-1]]:24: warning: initializing non-owner argument of type 'int *' with a newly created 'gsl::owner<>'
consumesResource((int *)malloc(ByteCount), 25); // Ok
consumesResource((int *)::malloc(ByteCount), 25); // Ok
testNonCasted(malloc(ByteCount));
// CHECK-MESSAGES: [[@LINE-1]]:17: warning: initializing non-owner argument of type 'void *' with a newly created 'gsl::owner<>'
testNonCastedOwner(gsl::owner<void *>(malloc(ByteCount))); // Ok
testNonCastedOwner(malloc(ByteCount)); // Ok
FILE *File1 = std::fopen("test_name.txt", "w+");
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: initializing non-owner 'FILE *' (aka 'int *') with a newly created 'gsl::owner<>'
FILE *File2 = ::fopen("test_name.txt", "w+");
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: initializing non-owner 'FILE *' (aka 'int *') with a newly created 'gsl::owner<>'
gsl::owner<FILE *> File3 = ::fopen("test_name.txt", "w+"); // Ok
FILE *File4;
File4 = ::fopen("test_name.txt", "w+");
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: assigning newly created 'gsl::owner<>' to non-owner 'FILE *' (aka 'int *')
gsl::owner<FILE *> File5;
File5 = ::fopen("test_name.txt", "w+"); // Ok
File5 = File1;
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: expected assignment source to be of type 'gsl::owner<>'; got 'FILE *' (aka 'int *')
gsl::owner<FILE *> File6 = File1;
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: expected initialization with value of type 'gsl::owner<>'; got 'FILE *' (aka 'int *')
FILE *File7 = tmpfile();
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: initializing non-owner 'FILE *' (aka 'int *') with a newly created 'gsl::owner<>'
gsl::owner<FILE *> File8 = tmpfile(); // Ok
nonOwningCall(::fopen("test_name.txt", "r"));
// CHECK-MESSAGES: [[@LINE-1]]:17: warning: initializing non-owner argument of type 'FILE *' (aka 'int *') with a newly created 'gsl::owner<>'
nonOwningCall(std::fopen("test_name.txt", "r"));
// CHECK-MESSAGES: [[@LINE-1]]:17: warning: initializing non-owner argument of type 'FILE *' (aka 'int *') with a newly created 'gsl::owner<>'
consumesResource(::fopen("test_name.txt", "r")); // Ok
int *HeapPointer3 = (int *)aligned_alloc(16ul, 4ul * 32ul);
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: initializing non-owner 'int *' with a newly created 'gsl::owner<>'
gsl::owner<int *> HeapPointer4 = static_cast<int *>(aligned_alloc(16ul, 4ul * 32ul)); // Ok
void *HeapPointer5 = calloc(10ul, 4ul);
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: initializing non-owner 'void *' with a newly created 'gsl::owner<>'
gsl::owner<void *> HeapPointer6 = calloc(10ul, 4ul); // Ok
}
void test_legacy_consumers() {
int StackInteger = 42;
int *StackPointer = &StackInteger;
int *HeapPointer1 = (int *)malloc(100);
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: initializing non-owner 'int *' with a newly created 'gsl::owner<>'
gsl::owner<int *> HeapPointer2 = (int *)malloc(100);
std::free(StackPointer);
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: calling legacy resource function without passing a 'gsl::owner<>'
std::free(HeapPointer1);
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: calling legacy resource function without passing a 'gsl::owner<>'
std::free(HeapPointer2); // Ok
// CHECK MESSAGES: [[@LINE-1]]:3: warning: calling legacy resource function without passing a 'gsl::owner<>'
// FIXME: the check complains about initialization of 'void *' with new created owner.
// This happens, because the argument of `free` is not marked as 'owner<>' (and cannot be),
// and the check will not figure out could be meant as owner.
// This property will probably never be fixed, because it is probably a rather rare
// use-case and 'owner<>' should be wrapped in RAII classes anyway!
std::free(std::malloc(100)); // Ok but silly :)
// CHECK-MESSAGES: [[@LINE-1]]:13: warning: initializing non-owner argument of type 'void *' with a newly created 'gsl::owner<>'
// Demonstrate, that multi-argument functions are diagnosed as well.
std::realloc(StackPointer, 200);
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: calling legacy resource function without passing a 'gsl::owner<>'
std::realloc(HeapPointer1, 200);
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: calling legacy resource function without passing a 'gsl::owner<>'
std::realloc(HeapPointer2, 200); // Ok
std::realloc(std::malloc(100), 200); // Ok but silly
// CHECK-MESSAGES: [[@LINE-1]]:16: warning: initializing non-owner argument of type 'void *' with a newly created 'gsl::owner<>'
fclose(fileFactory1());
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: calling legacy resource function without passing a 'gsl::owner<>'
fclose(fileFactory2()); // Ok, same as FIXME with `free(malloc(100))` applies here
// CHECK-MESSAGES: [[@LINE-1]]:10: warning: initializing non-owner argument of type 'FILE *' (aka 'int *') with a newly created 'gsl::owner<>'
gsl::owner<FILE *> File1 = fopen("testfile.txt", "r"); // Ok
FILE *File2 = freopen("testfile.txt", "w", File1);
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: initializing non-owner 'FILE *' (aka 'int *') with a newly created 'gsl::owner<>'
// CHECK-MESSAGES: [[@LINE-2]]:17: warning: calling legacy resource function without passing a 'gsl::owner<>'
// FIXME: The warning for not passing and owner<> is a false positive since both the filename and the
// mode are not supposed to be owners but still pointers. The check is to coarse for
// this function. Maybe `freopen` gets special treatment.
gsl::owner<FILE *> File3 = freopen("testfile.txt", "w", File2); // Bad, File2 no owner
// CHECK-MESSAGES: [[@LINE-1]]:30: warning: calling legacy resource function without passing a 'gsl::owner<>'
FILE *TmpFile = tmpfile();
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: initializing non-owner 'FILE *' (aka 'int *') with a newly created 'gsl::owner<>'
FILE *File6 = freopen("testfile.txt", "w", TmpFile); // Bad, both return and argument
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: initializing non-owner 'FILE *' (aka 'int *') with a newly created 'gsl::owner<>'
// CHECK-MESSAGES: [[@LINE-2]]:17: warning: calling legacy resource function without passing a 'gsl::owner<>'
}
|