|
4 | 4 |
|
5 | 5 | static tss_t k; |
6 | 6 |
|
7 | | -void do_free(void *d) { free(d); } |
| 7 | +void t1(void *data) {} |
| 8 | +void t2(void *data) { free(tss_get(k)); } |
| 9 | +void t3(void *data) { |
| 10 | + void *p = tss_get(k); |
| 11 | + free(p); |
| 12 | +} |
8 | 13 |
|
| 14 | +void do_free(void *d) { free(d); } |
9 | 15 | void maybe_free(void *d) {} |
10 | 16 |
|
11 | 17 | void m1() { |
| 18 | + thrd_t id; |
12 | 19 | tss_create(&k, free); // COMPLIANT |
| 20 | + thrd_create(&id, t1, NULL); |
| 21 | + thrd_join(id, NULL); |
| 22 | + tss_delete(k); |
| 23 | +} |
| 24 | + |
| 25 | +void m1a() { |
| 26 | + thrd_t id; |
| 27 | + tss_create(&k, free); // NON_COMPLIANT - Doesn't wait for thread to cleanup |
| 28 | + // resources; if tss_delete is called prior to thread |
| 29 | + // termination the destructor won't be called. |
| 30 | + thrd_create(&id, t1, NULL); |
| 31 | + tss_delete(k); |
| 32 | +} |
| 33 | + |
| 34 | +void m1b() { |
| 35 | + tss_create(&k, free); // COMPLIANT - No threads created. |
13 | 36 | tss_delete(k); |
14 | 37 | } |
15 | 38 |
|
16 | 39 | void m2() { |
| 40 | + thrd_t id; |
17 | 41 | tss_create(&k, do_free); // COMPLIANT |
| 42 | + thrd_create(&id, t1, NULL); |
| 43 | + thrd_join(id, NULL); |
| 44 | + tss_delete(k); |
| 45 | +} |
| 46 | + |
| 47 | +void m2a() { |
| 48 | + thrd_t id; |
| 49 | + tss_create(&k, do_free); // NON_COMPLIANT - Doesn't wait for thread to cleanup |
| 50 | + // resources; if tss_delete is called prior to thread |
| 51 | + // termination the destructor won't be called. |
| 52 | + thrd_create(&id, t1, NULL); |
| 53 | + tss_delete(k); |
| 54 | +} |
| 55 | + |
| 56 | +void m2b() { |
| 57 | + tss_create(&k, do_free); // COMPLIANT - No threads created. |
18 | 58 | tss_delete(k); |
19 | 59 | } |
20 | 60 |
|
21 | 61 | void m3() { |
| 62 | + thrd_t id; |
22 | 63 | tss_create(&k, maybe_free); // COMPLIANT |
| 64 | + thrd_create(&id, t1, NULL); |
| 65 | + thrd_join(id, NULL); |
23 | 66 | tss_delete(k); |
24 | 67 | } |
25 | 68 |
|
26 | | -void m1a() { |
27 | | - tss_create(&k, free); // NON_COMPLIANT - The memory is deallocated, but the |
28 | | - // usage pattern is non-standard and may lead to errors. |
29 | | - free(tss_get(k)); |
| 69 | +void m3a() { |
| 70 | + thrd_t id; |
| 71 | + tss_create(&k, |
| 72 | + maybe_free); // NON_COMPLIANT - Doesn't wait for thread to cleanup |
| 73 | + // resources; if tss_delete is called prior to thread |
| 74 | + // termination the destructor won't be called. |
| 75 | + thrd_create(&id, t1, NULL); |
| 76 | + tss_delete(k); |
30 | 77 | } |
31 | 78 |
|
32 | | -void m2a() { |
33 | | - tss_create(&k, |
34 | | - do_free); // NON_COMPLIANT - The memory is deallocated, but the |
35 | | - // usage pattern is non-standard and may lead to errors. |
36 | | - free(tss_get(k)); |
| 79 | +void m3b() { |
| 80 | + tss_create(&k, maybe_free); // COMPLIANT - No threads created. |
| 81 | + tss_delete(k); |
37 | 82 | } |
38 | 83 |
|
39 | | -void m3a() { |
40 | | - tss_create( |
41 | | - &k, maybe_free); // NON_COMPLIANT - The memory is deallocated, but the |
42 | | - // usage pattern is non-standard and may lead to errors. |
43 | | - free(tss_get(k)); |
| 84 | +void m4() { |
| 85 | + thrd_t id; |
| 86 | + |
| 87 | + tss_create(&k, free); // NON_COMPLIANT - The memory is deallocated, but the |
| 88 | + // usage pattern is non-standard and may lead to errors. |
| 89 | + thrd_create(&id, t2, NULL); |
| 90 | + thrd_join(id, NULL); |
| 91 | + tss_delete(k); |
44 | 92 | } |
45 | 93 |
|
46 | | -void m1b() { |
47 | | - tss_create(&k, NULL); // COMPLIANT |
48 | | - free(tss_get(k)); |
| 94 | +void m5() { |
| 95 | + tss_create(&k, NULL); // NON_COMPLIANT - `tss_delete` should be called. |
49 | 96 | } |
50 | 97 |
|
51 | | -void m2b() { |
| 98 | +void m5a() { |
| 99 | + thrd_t id; |
| 100 | + |
52 | 101 | tss_create(&k, NULL); // COMPLIANT |
53 | | - free(tss_get(k)); |
| 102 | + thrd_create(&id, t2, NULL); |
| 103 | + thrd_join(id, NULL); |
| 104 | + tss_delete(k); |
54 | 105 | } |
55 | 106 |
|
56 | | -void m3b() { |
| 107 | +void m5aa() { |
| 108 | + thrd_t id; |
| 109 | + |
57 | 110 | tss_create(&k, NULL); // COMPLIANT |
58 | | - free(tss_get(k)); |
| 111 | + thrd_create(&id, t3, NULL); |
| 112 | + thrd_join(id, NULL); |
| 113 | + tss_delete(k); |
59 | 114 | } |
60 | 115 |
|
61 | | -void m4() { |
62 | | - tss_create(&k, free); // NON_COMPLIANT |
63 | | -} |
| 116 | +void m5b() { |
| 117 | + thrd_t id; |
64 | 118 |
|
65 | | -void m5() { |
66 | | - tss_create(&k, do_free); // NON_COMPLIANT |
| 119 | + tss_create(&k, NULL); // COMPLIANT - Cleanup can happen before OR after |
| 120 | + // `tss_delete` is called; so there is no need to wait. |
| 121 | + thrd_create(&id, t2, NULL); |
| 122 | + tss_delete(k); |
67 | 123 | } |
68 | 124 |
|
69 | | -void m6() { |
70 | | - tss_create(&k, maybe_free); // NON_COMPLIANT |
| 125 | +void m5bb() { |
| 126 | + thrd_t id; |
| 127 | + |
| 128 | + tss_create(&k, NULL); // COMPLIANT - Cleanup can happen before OR after |
| 129 | + // `tss_delete` is called; so there is no need to wait. |
| 130 | + thrd_create(&id, t3, NULL); |
| 131 | + tss_delete(k); |
71 | 132 | } |
72 | 133 |
|
73 | | -void m4a() { |
74 | | - tss_create(&k, NULL); // NON_COMPLIANT |
| 134 | +void m6() { |
| 135 | + tss_create(&k, free); // NON_COMPLIANT |
75 | 136 | } |
76 | 137 |
|
77 | | -void m5a() { |
78 | | - tss_create(&k, NULL); // NON_COMPLIANT |
| 138 | +void m7() { |
| 139 | + tss_create(&k, do_free); // NON_COMPLIANT |
79 | 140 | } |
80 | 141 |
|
81 | | -void m6a() { |
82 | | - tss_create(&k, NULL); // NON_COMPLIANT |
| 142 | +void m8() { |
| 143 | + tss_create(&k, maybe_free); // NON_COMPLIANT |
83 | 144 | } |
84 | 145 |
|
85 | | -void m4b() { |
| 146 | +void m9() { |
86 | 147 | tss_create(&k, NULL); // NON_COMPLIANT |
87 | | - tss_delete(k); |
88 | 148 | } |
89 | 149 |
|
90 | | -void m5b() { |
| 150 | +void m10() { |
91 | 151 | tss_create(&k, NULL); // NON_COMPLIANT |
92 | | - tss_delete(k); |
93 | 152 | } |
94 | 153 |
|
95 | | -void m6b() { |
| 154 | +void m11() { |
96 | 155 | tss_create(&k, NULL); // NON_COMPLIANT |
97 | | - tss_delete(k); |
98 | 156 | } |
0 commit comments