• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

OISF / suricata / 23374838686

21 Mar 2026 07:29AM UTC coverage: 59.341% (-20.0%) from 79.315%
23374838686

Pull #15075

github

web-flow
Merge 90b4e834f into 6587e363a
Pull Request #15075: Stack 8001 v16.4

38 of 70 new or added lines in 10 files covered. (54.29%)

34165 existing lines in 563 files now uncovered.

119621 of 201584 relevant lines covered (59.34%)

650666.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

11.54
/src/thread-storage.c
1
/* Copyright (C) 2024 Open Information Security Foundation
2
 *
3
 * You can copy, redistribute or modify this Program under the terms of
4
 * the GNU General Public License version 2 as published by the Free
5
 * Software Foundation.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 * GNU General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU General Public License
13
 * version 2 along with this program; if not, write to the Free Software
14
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15
 * 02110-1301, USA.
16
 */
17

18
#include "suricata-common.h"
19
#include "thread-storage.h"
20
#include "util-storage.h"
21
#include "util-unittest.h"
22

23
const StorageEnum storage_type = STORAGE_THREAD;
24

25
unsigned int ThreadStorageSize(void)
26
{
1✔
27
    return StorageGetSize(storage_type);
1✔
28
}
1✔
29

30
void *ThreadGetStorageById(const ThreadVars *tv, ThreadStorageId id)
UNCOV
31
{
×
UNCOV
32
    return StorageGetById(tv->storage, storage_type, id.id);
×
UNCOV
33
}
×
34

35
int ThreadSetStorageById(ThreadVars *tv, ThreadStorageId id, void *ptr)
UNCOV
36
{
×
UNCOV
37
    return StorageSetById(tv->storage, storage_type, id.id, ptr);
×
UNCOV
38
}
×
39

40
void *ThreadAllocStorageById(ThreadVars *tv, ThreadStorageId id)
UNCOV
41
{
×
UNCOV
42
    return StorageAllocByIdPrealloc(tv->storage, storage_type, id.id);
×
UNCOV
43
}
×
44

45
void ThreadFreeStorageById(ThreadVars *tv, ThreadStorageId id)
46
{
×
47
    StorageFreeById(tv->storage, storage_type, id.id);
×
48
}
×
49

50
void ThreadFreeStorage(ThreadVars *tv)
UNCOV
51
{
×
UNCOV
52
    if (ThreadStorageSize() > 0)
×
UNCOV
53
        StorageFreeAll(tv->storage, storage_type);
×
UNCOV
54
}
×
55

56
ThreadStorageId ThreadStorageRegister(const char *name, const unsigned int size,
57
        void *(*Alloc)(unsigned int), void (*Free)(void *))
UNCOV
58
{
×
UNCOV
59
    int id = StorageRegister(storage_type, name, size, Alloc, Free);
×
UNCOV
60
    ThreadStorageId tsi = { .id = id };
×
UNCOV
61
    return tsi;
×
UNCOV
62
}
×
63

64
#ifdef UNITTESTS
65

66
static void *StorageTestAlloc(unsigned int size)
67
{
68
    return SCCalloc(1, size);
69
}
70

71
static void StorageTestFree(void *x)
72
{
73
    SCFree(x);
74
}
75

76
static int ThreadStorageTest01(void)
77
{
78
    StorageCleanup();
79
    StorageInit();
80

81
    ThreadStorageId id1 = ThreadStorageRegister("test", 8, StorageTestAlloc, StorageTestFree);
82
    FAIL_IF(id1.id < 0);
83

84
    ThreadStorageId id2 = ThreadStorageRegister("variable", 24, StorageTestAlloc, StorageTestFree);
85
    FAIL_IF(id2.id < 0);
86

87
    ThreadStorageId id3 =
88
            ThreadStorageRegister("store", sizeof(void *), StorageTestAlloc, StorageTestFree);
89
    FAIL_IF(id3.id < 0);
90

91
    FAIL_IF(StorageFinalize() < 0);
92

93
    ThreadVars *tv = SCCalloc(1, sizeof(ThreadVars) + ThreadStorageSize());
94
    FAIL_IF_NULL(tv);
95

96
    void *ptr = ThreadGetStorageById(tv, id1);
97
    FAIL_IF_NOT_NULL(ptr);
98

99
    ptr = ThreadGetStorageById(tv, id2);
100
    FAIL_IF_NOT_NULL(ptr);
101

102
    ptr = ThreadGetStorageById(tv, id3);
103
    FAIL_IF_NOT_NULL(ptr);
104

105
    void *ptr1a = ThreadAllocStorageById(tv, id1);
106
    FAIL_IF_NULL(ptr1a);
107

108
    void *ptr2a = ThreadAllocStorageById(tv, id2);
109
    FAIL_IF_NULL(ptr2a);
110

111
    void *ptr3a = ThreadAllocStorageById(tv, id3);
112
    FAIL_IF_NULL(ptr3a);
113

114
    void *ptr1b = ThreadGetStorageById(tv, id1);
115
    FAIL_IF(ptr1a != ptr1b);
116

117
    void *ptr2b = ThreadGetStorageById(tv, id2);
118
    FAIL_IF(ptr2a != ptr2b);
119

120
    void *ptr3b = ThreadGetStorageById(tv, id3);
121
    FAIL_IF(ptr3a != ptr3b);
122

123
    ThreadFreeStorage(tv);
124
    StorageCleanup();
125
    SCFree(tv);
126
    PASS;
127
}
128

129
static int ThreadStorageTest02(void)
130
{
131
    StorageCleanup();
132
    StorageInit();
133

134
    ThreadStorageId id1 = ThreadStorageRegister("test", sizeof(void *), NULL, StorageTestFree);
135
    FAIL_IF(id1.id < 0);
136

137
    FAIL_IF(StorageFinalize() < 0);
138

139
    ThreadVars *tv = SCCalloc(1, sizeof(ThreadVars) + ThreadStorageSize());
140
    FAIL_IF_NULL(tv);
141

142
    void *ptr = ThreadGetStorageById(tv, id1);
143
    FAIL_IF_NOT_NULL(ptr);
144

145
    void *ptr1a = SCMalloc(128);
146
    FAIL_IF_NULL(ptr1a);
147

148
    ThreadSetStorageById(tv, id1, ptr1a);
149

150
    void *ptr1b = ThreadGetStorageById(tv, id1);
151
    FAIL_IF(ptr1a != ptr1b);
152

153
    ThreadFreeStorage(tv);
154
    StorageCleanup();
155
    SCFree(tv);
156
    PASS;
157
}
158

159
static int ThreadStorageTest03(void)
160
{
161
    StorageCleanup();
162
    StorageInit();
163

164
    ThreadStorageId id1 = ThreadStorageRegister("test1", sizeof(void *), NULL, StorageTestFree);
165
    FAIL_IF(id1.id < 0);
166

167
    ThreadStorageId id2 = ThreadStorageRegister("test2", sizeof(void *), NULL, StorageTestFree);
168
    FAIL_IF(id2.id < 0);
169

170
    ThreadStorageId id3 = ThreadStorageRegister("test3", 32, StorageTestAlloc, StorageTestFree);
171
    FAIL_IF(id3.id < 0);
172

173
    FAIL_IF(StorageFinalize() < 0);
174

175
    ThreadVars *tv = SCCalloc(1, sizeof(ThreadVars) + ThreadStorageSize());
176
    FAIL_IF_NULL(tv);
177

178
    void *ptr = ThreadGetStorageById(tv, id1);
179
    FAIL_IF_NOT_NULL(ptr);
180

181
    void *ptr1a = SCMalloc(128);
182
    FAIL_IF_NULL(ptr1a);
183

184
    ThreadSetStorageById(tv, id1, ptr1a);
185

186
    void *ptr2a = SCMalloc(256);
187
    FAIL_IF_NULL(ptr2a);
188

189
    ThreadSetStorageById(tv, id2, ptr2a);
190

191
    void *ptr3a = ThreadAllocStorageById(tv, id3);
192
    FAIL_IF_NULL(ptr3a);
193

194
    void *ptr1b = ThreadGetStorageById(tv, id1);
195
    FAIL_IF(ptr1a != ptr1b);
196

197
    void *ptr2b = ThreadGetStorageById(tv, id2);
198
    FAIL_IF(ptr2a != ptr2b);
199

200
    void *ptr3b = ThreadGetStorageById(tv, id3);
201
    FAIL_IF(ptr3a != ptr3b);
202

203
    ThreadFreeStorage(tv);
204
    StorageCleanup();
205
    SCFree(tv);
206
    PASS;
207
}
208
#endif
209

210
void RegisterThreadStorageTests(void)
UNCOV
211
{
×
212
#ifdef UNITTESTS
213
    UtRegisterTest("ThreadStorageTest01", ThreadStorageTest01);
214
    UtRegisterTest("ThreadStorageTest02", ThreadStorageTest02);
215
    UtRegisterTest("ThreadStorageTest03", ThreadStorageTest03);
216
#endif
UNCOV
217
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc