Skip to content

Commit 0e85f55

Browse files
committed
Roll back destructor detection
1 parent f89068c commit 0e85f55

3 files changed

Lines changed: 6 additions & 80 deletions

File tree

cpp/misra/src/rules/RULE-21-6-1/DynamicMemoryShouldNotBeUsed.ql

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -217,13 +217,13 @@ abstract class DynamicMemoryDeallocatingFunction extends Function { }
217217
* A function that directly deallocates dynamic memory.
218218
*
219219
* Includes C deallocation functions (`free`) and C++ deallocation functions
220-
* (`operator delete`, `operator delete[]`, `~T()` for any `T`.)
220+
* (`operator delete` and `operator delete[]`).
221+
*
222+
* Note that this includes destructors, as they may or may not deallocate. If
223+
* the destructor deallocates, the query should alert on the deallocation site.
221224
*/
222225
class DirectDynamicMemoryDeallocatingFunction extends DynamicMemoryDeallocatingFunction {
223-
DirectDynamicMemoryDeallocatingFunction() {
224-
this instanceof DeallocationFunction or
225-
this instanceof Destructor
226-
}
226+
DirectDynamicMemoryDeallocatingFunction() { this instanceof DeallocationFunction }
227227
}
228228

229229
/**
@@ -264,12 +264,7 @@ where
264264
* Excludes realloc (already caught as allocation).
265265
*/
266266

267-
(
268-
call.getTarget() instanceof DynamicMemoryDeallocatingFunction and
269-
(
270-
call instanceof DestructorCall implies not call.isCompilerGenerated() // Exclude RAII constructor calls.
271-
)
272-
) and
267+
call.getTarget() instanceof DynamicMemoryDeallocatingFunction and
273268
not call.getTarget() instanceof DynamicMemoryAllocatingFunction and // Exclude `realloc`.
274269
message = "Call to '" + call.getTarget().getName() + "' that dynamically deallocates memory."
275270
)

cpp/misra/test/rules/RULE-21-6-1/DynamicMemoryShouldNotBeUsed.expected

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -138,34 +138,3 @@
138138
| test.cpp:340:13:340:51 | call to operator new | Call to dynamic memory allocating function 'operator new'. |
139139
| test.cpp:343:14:344:31 | call to forward_list | Initialization of an object of class 'forward_list' that uses 'std::allocator<T>'. |
140140
| test.cpp:343:14:344:31 | call to operator new | Call to dynamic memory allocating function 'operator new'. |
141-
| test.cpp:405:20:405:20 | call to vector | Initialization of an object of class 'vector' that uses 'std::allocator<T>'. |
142-
| test.cpp:406:19:406:19 | call to deque | Initialization of an object of class 'deque' that uses 'std::allocator<T>'. |
143-
| test.cpp:407:18:407:18 | call to list | Initialization of an object of class 'list' that uses 'std::allocator<T>'. |
144-
| test.cpp:408:26:408:27 | call to forward_list | Initialization of an object of class 'forward_list' that uses 'std::allocator<T>'. |
145-
| test.cpp:409:17:409:17 | call to set | Initialization of an object of class 'set' that uses 'std::allocator<T>'. |
146-
| test.cpp:410:22:410:22 | call to map | Initialization of an object of class 'map' that uses 'std::allocator<T>'. |
147-
| test.cpp:411:22:411:23 | call to multiset | Initialization of an object of class 'multiset' that uses 'std::allocator<T>'. |
148-
| test.cpp:412:27:412:28 | call to multimap | Initialization of an object of class 'multimap' that uses 'std::allocator<T>'. |
149-
| test.cpp:413:27:413:28 | call to unordered_set | Initialization of an object of class 'unordered_set' that uses 'std::allocator<T>'. |
150-
| test.cpp:414:32:414:34 | call to unordered_multiset | Initialization of an object of class 'unordered_multiset' that uses 'std::allocator<T>'. |
151-
| test.cpp:415:37:415:39 | call to unordered_multimap | Initialization of an object of class 'unordered_multimap' that uses 'std::allocator<T>'. |
152-
| test.cpp:416:22:416:23 | call to valarray | Initialization of an object of class 'valarray' that dynamically allocates memory internally. |
153-
| test.cpp:418:5:418:11 | call to ~vector | Call to '~vector' that dynamically deallocates memory. |
154-
| test.cpp:419:5:419:10 | call to ~deque | Call to '~deque' that dynamically deallocates memory. |
155-
| test.cpp:420:5:420:9 | call to ~list | Call to '~list' that dynamically deallocates memory. |
156-
| test.cpp:421:6:421:18 | call to ~forward_list | Call to '~forward_list' that dynamically deallocates memory. |
157-
| test.cpp:422:5:422:8 | call to ~set | Call to '~set' that dynamically deallocates memory. |
158-
| test.cpp:423:5:423:8 | call to ~map | Call to '~map' that dynamically deallocates memory. |
159-
| test.cpp:425:6:425:14 | call to ~multimap | Call to '~multimap' that dynamically deallocates memory. |
160-
| test.cpp:426:6:426:19 | call to ~unordered_set | Call to '~unordered_set' that dynamically deallocates memory. |
161-
| test.cpp:427:7:427:25 | call to ~unordered_multiset | Call to '~unordered_multiset' that dynamically deallocates memory. |
162-
| test.cpp:428:7:428:25 | call to ~unordered_multimap | Call to '~unordered_multimap' that dynamically deallocates memory. |
163-
| test.cpp:429:6:429:14 | call to ~valarray | Call to '~valarray' that dynamically deallocates memory. |
164-
| test.cpp:431:15:431:17 | call to basic_string | Initialization of an object of class 'basic_string' that uses 'std::allocator<T>'. |
165-
| test.cpp:432:16:432:19 | call to basic_string | Initialization of an object of class 'basic_string' that uses 'std::allocator<T>'. |
166-
| test.cpp:433:18:433:23 | call to basic_string | Initialization of an object of class 'basic_string' that uses 'std::allocator<T>'. |
167-
| test.cpp:434:18:434:23 | call to basic_string | Initialization of an object of class 'basic_string' that uses 'std::allocator<T>'. |
168-
| test.cpp:436:7:436:19 | call to ~basic_string | Call to '~basic_string' that dynamically deallocates memory. |
169-
| test.cpp:437:8:437:20 | call to ~basic_string | Call to '~basic_string' that dynamically deallocates memory. |
170-
| test.cpp:438:10:438:22 | call to ~basic_string | Call to '~basic_string' that dynamically deallocates memory. |
171-
| test.cpp:439:10:439:22 | call to ~basic_string | Call to '~basic_string' that dynamically deallocates memory. |

cpp/misra/test/rules/RULE-21-6-1/test.cpp

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -401,42 +401,4 @@ void test_compliant_pointers() {
401401
int *q = &y; // COMPLIANT: raw pointer to static storage, no heap involved
402402
}
403403

404-
void test_explicit_calls_to_RAII_destructors_of_containers_or_strings() {
405-
std::vector<int> v; // NON_COMPLIANT: uses std::allocator
406-
std::deque<int> d; // NON_COMPLIANT: uses std::allocator
407-
std::list<int> l; // NON_COMPLIANT: uses std::allocator
408-
std::forward_list<int> fl; // NON_COMPLIANT: uses std::allocator
409-
std::set<int> s; // NON_COMPLIANT: uses std::allocator
410-
std::map<int, int> m; // NON_COMPLIANT: uses std::allocator
411-
std::multiset<int> ms; // NON_COMPLIANT: uses std::allocator
412-
std::multimap<int, int> mm; // NON_COMPLIANT: uses std::allocator
413-
std::unordered_set<int> us; // NON_COMPLIANT: uses std::allocator
414-
std::unordered_multiset<int> ums; // NON_COMPLIANT: uses std::allocator
415-
std::unordered_multimap<int, int> umm; // NON_COMPLIANT: uses std::allocator
416-
std::valarray<int> va; // NON_COMPLIANT: uses std::allocator
417-
418-
v.~vector(); // NON_COMPLIANT: explicit call to RAII destructor
419-
d.~deque(); // NON_COMPLIANT: explicit call to RAII destructor
420-
l.~list(); // NON_COMPLIANT: explicit call to RAII destructor
421-
fl.~forward_list(); // NON_COMPLIANT: explicit call to RAII destructor
422-
s.~set(); // NON_COMPLIANT: explicit call to RAII destructor
423-
m.~map(); // NON_COMPLIANT: explicit call to RAII destructor
424-
ms.~multiset(); // NON_COMPLIANT: explicit call to RAII destructor
425-
mm.~multimap(); // NON_COMPLIANT: explicit call to RAII destructor
426-
us.~unordered_set(); // NON_COMPLIANT: explicit call to RAII destructor
427-
ums.~unordered_multiset(); // NON_COMPLIANT: explicit call to RAII destructor
428-
umm.~unordered_multimap(); // NON_COMPLIANT: explicit call to RAII destructor
429-
va.~valarray(); // NON_COMPLIANT: explicit call to RAII destructor
430-
431-
std::string str; // NON_COMPLIANT: uses std::allocator
432-
std::wstring wstr; // NON_COMPLIANT: uses std::allocator
433-
std::u16string u16str; // NON_COMPLIANT: uses std::allocator
434-
std::u32string u32str; // NON_COMPLIANT: uses std::allocator
435-
436-
str.~basic_string(); // NON_COMPLIANT: explicit call to RAII destructor
437-
wstr.~basic_string(); // NON_COMPLIANT: explicit call to RAII destructor
438-
u16str.~basic_string(); // NON_COMPLIANT: explicit call to RAII destructor
439-
u32str.~basic_string(); // NON_COMPLIANT: explicit call to RAII destructor
440-
}
441-
442404
int main() { return 0; }

0 commit comments

Comments
 (0)