27 */ |
27 */ |
28 |
28 |
29 #include "util_allocator.h" |
29 #include "util_allocator.h" |
30 #include "cx/test.h" |
30 #include "cx/test.h" |
31 |
31 |
|
32 #if !defined(__clang__) && __GNUC__ > 11 |
|
33 // this utility is explicitly designed to track UAF |
|
34 #pragma GCC diagnostic ignored "-Wuse-after-free" |
|
35 #endif |
|
36 |
32 static void cx_testing_allocator_track(CxTestingAllocator *alloc, void *ptr) { |
37 static void cx_testing_allocator_track(CxTestingAllocator *alloc, void *ptr) { |
33 for (size_t i = 0; i < alloc->tracked_count; i++) { |
38 for (size_t i = 0; i < alloc->tracked_count; i++) { |
34 if (alloc->tracked[i] == ptr) return; // is already tracked |
39 if (alloc->tracked[i] == ptr) return; // is already tracked |
35 } |
40 } |
36 |
41 |
81 data->alloc_total++; |
86 data->alloc_total++; |
82 if (ptr == NULL) { |
87 if (ptr == NULL) { |
83 data->alloc_failed++; |
88 data->alloc_failed++; |
84 } else { |
89 } else { |
85 data->free_total++; |
90 data->free_total++; |
86 #if !defined(__clang__) && __GNUC__ > 11 |
|
87 #pragma GCC diagnostic push |
|
88 #pragma GCC diagnostic ignored "-Wuse-after-free" |
|
89 #endif |
|
90 if (!cx_testing_allocator_untrack(data, mem)) { |
91 if (!cx_testing_allocator_untrack(data, mem)) { |
91 data->free_failed++; |
92 data->free_failed++; |
92 } |
93 } |
93 #if !defined(__clang__) && __GNUC__ > 11 |
|
94 #pragma GCC diagnostic pop |
|
95 #endif |
|
96 cx_testing_allocator_track(data, ptr); |
94 cx_testing_allocator_track(data, ptr); |
97 } |
95 } |
98 return ptr; |
96 return ptr; |
99 } |
97 } |
100 } |
98 } |