]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
host/storage: use dedicated 'id' type
authorJuliana Fajardini <jufajardini@gmail.com>
Wed, 14 Apr 2021 22:12:28 +0000 (23:12 +0100)
committerVictor Julien <victor@inliniac.net>
Thu, 15 Apr 2021 09:43:45 +0000 (11:43 +0200)
- Wrap the id in a HostStorageId struct to avoid id confusion
with other storage API calls.
- Fix formatting with clang script.

src/detect-engine-tag.c
src/detect-engine-threshold.c
src/detect-engine-threshold.h
src/host-bit.c
src/host-storage.c
src/host-storage.h

index f79548eb6112c4752a572e796dc3337872297245..27febe3f33624f2bb0c7b0e67415929fe5ef35bc 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2007-2013 Open Information Security Foundation
+/* Copyright (C) 2007-2021 Open Information Security Foundation
  *
  * You can copy, redistribute or modify this Program under the terms of
  * the GNU General Public License version 2 as published by the Free
@@ -45,7 +45,7 @@
 SC_ATOMIC_DECLARE(unsigned int, num_tags);  /**< Atomic counter, to know if we
                                                  have tagged hosts/sessions,
                                                  to avoid locking */
-static int host_tag_id = -1;                /**< Host storage id for tags */
+static HostStorageId host_tag_id = { .id = -1 }; /**< Host storage id for tags */
 static FlowStorageId flow_tag_id = { .id = -1 }; /**< Flow storage id for tags */
 
 void TagInitCtx(void)
@@ -53,7 +53,7 @@ void TagInitCtx(void)
     SC_ATOMIC_INIT(num_tags);
 
     host_tag_id = HostStorageRegister("tag", sizeof(void *), NULL, DetectTagDataListFree);
-    if (host_tag_id == -1) {
+    if (host_tag_id.id == -1) {
         FatalError(SC_ERR_FATAL, "Can't initiate host storage for tag");
     }
     flow_tag_id = FlowStorageRegister("tag", sizeof(void *), NULL, DetectTagDataListFree);
index 1ba59a50901061ec839e631451313b6e1c167589..5856406c65bc07d90bad9aab1b781f1e3b86de40 100644 (file)
 #include "util-var-name.h"
 #include "tm-threads.h"
 
-static int host_threshold_id = -1; /**< host storage id for thresholds */
+static HostStorageId host_threshold_id = { .id = -1 };     /**< host storage id for thresholds */
 static IPPairStorageId ippair_threshold_id = { .id = -1 }; /**< ip pair storage id for thresholds */
 
-int ThresholdHostStorageId(void)
+HostStorageId ThresholdHostStorageId(void)
 {
     return host_threshold_id;
 }
@@ -80,7 +80,7 @@ int ThresholdHostStorageId(void)
 void ThresholdInit(void)
 {
     host_threshold_id = HostStorageRegister("threshold", sizeof(void *), NULL, ThresholdListFree);
-    if (host_threshold_id == -1) {
+    if (host_threshold_id.id == -1) {
         FatalError(SC_ERR_FATAL,
                    "Can't initiate host storage for thresholding");
     }
index 9bab025de7dbb8e29b4ed5eaa3b7cdf0803d19a1..b55c1d2c13bbf423b2833f0e210d3f1d0d08390e 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2007-2010 Open Information Security Foundation
+/* Copyright (C) 2007-2021 Open Information Security Foundation
  *
  * You can copy, redistribute or modify this Program under the terms of
  * the GNU General Public License version 2 as published by the Free
 #include "detect.h"
 #include "host.h"
 #include "ippair.h"
+#include "host-storage.h"
 
 void ThresholdInit(void);
 
-int ThresholdHostStorageId(void);
+HostStorageId ThresholdHostStorageId(void);
 int ThresholdHostHasThreshold(Host *);
 
 int ThresholdIPPairHasThreshold(IPPair *pair);
index 7df8501317a78d9e7497df6fa444f0b72cb9c98c..a1f0b73b20b9e1afe216767e38720190a4b985fa 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2014 Open Information Security Foundation
+/* Copyright (C) 2014-2021 Open Information Security Foundation
  *
  * You can copy, redistribute or modify this Program under the terms of
  * the GNU General Public License version 2 as published by the Free
@@ -38,7 +38,7 @@
 #include "util-unittest.h"
 #include "host-storage.h"
 
-static int host_bit_id = -1;                /**< Host storage id for bits */
+static HostStorageId host_bit_id = { .id = -1 }; /**< Host storage id for bits */
 
 static void HostBitFreeAll(void *store)
 {
@@ -49,7 +49,7 @@ static void HostBitFreeAll(void *store)
 void HostBitInitCtx(void)
 {
     host_bit_id = HostStorageRegister("bit", sizeof(void *), NULL, HostBitFreeAll);
-    if (host_bit_id == -1) {
+    if (host_bit_id.id == -1) {
         FatalError(SC_ERR_FATAL, "Can't initiate host storage for bits");
     }
 }
index ba1f812a466d4ca804fbe5d8913bf51e7d26534b..72261de99d52b5190430c707eae58007f59cdac8 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2007-2013 Open Information Security Foundation
+/* Copyright (C) 2007-2021 Open Information Security Foundation
  *
  * You can copy, redistribute or modify this Program under the terms of
  * the GNU General Public License version 2 as published by the Free
@@ -56,8 +56,12 @@ unsigned int HostStorageSize(void)
  * It has to be called once during the init of the sub system
  */
 
-int HostStorageRegister(const char *name, const unsigned int size, void *(*Alloc)(unsigned int), void (*Free)(void *)) {
-    return StorageRegister(STORAGE_HOST, name, size, Alloc, Free);
+HostStorageId HostStorageRegister(const char *name, const unsigned int size,
+        void *(*Alloc)(unsigned int), void (*Free)(void *))
+{
+    int id = StorageRegister(STORAGE_HOST, name, size, Alloc, Free);
+    HostStorageId hsi = { .id = id };
+    return hsi;
 }
 
 /**
@@ -68,9 +72,9 @@ int HostStorageRegister(const char *name, const unsigned int size, void *(*Alloc
  * \param ptr pointer to the data to store
  */
 
-int HostSetStorageById(Host *h, int id, void *ptr)
+int HostSetStorageById(Host *h, HostStorageId id, void *ptr)
 {
-    return StorageSetById((Storage *)((void *)h + sizeof(Host)), STORAGE_HOST, id, ptr);
+    return StorageSetById((Storage *)((void *)h + sizeof(Host)), STORAGE_HOST, id.id, ptr);
 }
 
 /**
@@ -81,9 +85,9 @@ int HostSetStorageById(Host *h, int id, void *ptr)
  *
  */
 
-void *HostGetStorageById(Host *h, int id)
+void *HostGetStorageById(Host *h, HostStorageId id)
 {
-    return StorageGetById((Storage *)((void *)h + sizeof(Host)), STORAGE_HOST, id);
+    return StorageGetById((Storage *)((void *)h + sizeof(Host)), STORAGE_HOST, id.id);
 }
 
 /**
@@ -92,14 +96,14 @@ void *HostGetStorageById(Host *h, int id)
 
 /* Start of "private" function */
 
-void *HostAllocStorageById(Host *h, int id)
+void *HostAllocStorageById(Host *h, HostStorageId id)
 {
-    return StorageAllocByIdPrealloc((Storage *)((void *)h + sizeof(Host)), STORAGE_HOST, id);
+    return StorageAllocByIdPrealloc((Storage *)((void *)h + sizeof(Host)), STORAGE_HOST, id.id);
 }
 
-void HostFreeStorageById(Host *h, int id)
+void HostFreeStorageById(Host *h, HostStorageId id)
 {
-    StorageFreeById((Storage *)((void *)h + sizeof(Host)), STORAGE_HOST, id);
+    StorageFreeById((Storage *)((void *)h + sizeof(Host)), STORAGE_HOST, id.id);
 }
 
 void HostFreeStorage(Host *h)
@@ -126,14 +130,15 @@ static int HostStorageTest01(void)
 {
     StorageInit();
 
-    int id1 = HostStorageRegister("test", 8, StorageTestAlloc, StorageTestFree);
-    if (id1 < 0)
+    HostStorageId id1 = HostStorageRegister("test", 8, StorageTestAlloc, StorageTestFree);
+    if (id1.id < 0)
         goto error;
-    int id2 = HostStorageRegister("variable", 24, StorageTestAlloc, StorageTestFree);
-    if (id2 < 0)
+    HostStorageId id2 = HostStorageRegister("variable", 24, StorageTestAlloc, StorageTestFree);
+    if (id2.id < 0)
         goto error;
-    int id3 = HostStorageRegister("store", sizeof(void *), StorageTestAlloc, StorageTestFree);
-    if (id3 < 0)
+    HostStorageId id3 =
+            HostStorageRegister("store", sizeof(void *), StorageTestAlloc, StorageTestFree);
+    if (id3.id < 0)
         goto error;
 
     if (StorageFinalize() < 0)
@@ -205,8 +210,8 @@ static int HostStorageTest02(void)
 {
     StorageInit();
 
-    int id1 = HostStorageRegister("test", sizeof(void *), NULL, StorageTestFree);
-    if (id1 < 0)
+    HostStorageId id1 = HostStorageRegister("test", sizeof(void *), NULL, StorageTestFree);
+    if (id1.id < 0)
         goto error;
 
     if (StorageFinalize() < 0)
@@ -255,14 +260,14 @@ static int HostStorageTest03(void)
 {
     StorageInit();
 
-    int id1 = HostStorageRegister("test1", sizeof(void *), NULL, StorageTestFree);
-    if (id1 < 0)
+    HostStorageId id1 = HostStorageRegister("test1", sizeof(void *), NULL, StorageTestFree);
+    if (id1.id < 0)
         goto error;
-    int id2 = HostStorageRegister("test2", sizeof(void *), NULL, StorageTestFree);
-    if (id2 < 0)
+    HostStorageId id2 = HostStorageRegister("test2", sizeof(void *), NULL, StorageTestFree);
+    if (id2.id < 0)
         goto error;
-    int id3 = HostStorageRegister("test3", 32, StorageTestAlloc, StorageTestFree);
-    if (id3 < 0)
+    HostStorageId id3 = HostStorageRegister("test3", 32, StorageTestAlloc, StorageTestFree);
+    if (id3.id < 0)
         goto error;
 
     if (StorageFinalize() < 0)
index b2bccbe26ece6300fee432ce0a742ff318ae0628..25e8b962c1108bacee2379ce1a715726df8c9233 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2007-2013 Open Information Security Foundation
+/* Copyright (C) 2007-2021 Open Information Security Foundation
  *
  * You can copy, redistribute or modify this Program under the terms of
  * the GNU General Public License version 2 as published by the Free
 #include "util-storage.h"
 #include "host.h"
 
+typedef struct HostStorageId_ {
+    int id;
+} HostStorageId;
+
 unsigned int HostStorageSize(void);
 
-void *HostGetStorageById(Host *h, int id);
-int HostSetStorageById(Host *h, int id, void *ptr);
-void *HostAllocStorageById(Host *h, int id);
+void *HostGetStorageById(Host *h, HostStorageId id);
+int HostSetStorageById(Host *h, HostStorageId id, void *ptr);
+void *HostAllocStorageById(Host *h, HostStorageId id);
 
-void HostFreeStorageById(Host *h, int id);
+void HostFreeStorageById(Host *h, HostStorageId id);
 void HostFreeStorage(Host *h);
 
 void RegisterHostStorageTests(void);
 
-int HostStorageRegister(const char *name, const unsigned int size, void *(*Alloc)(unsigned int), void (*Free)(void *));
+HostStorageId HostStorageRegister(const char *name, const unsigned int size,
+        void *(*Alloc)(unsigned int), void (*Free)(void *));
 
 #endif /* __HOST_STORAGE_H__ */