From b807059c34e56c852be83c324cc1e79109a9f6d0 Mon Sep 17 00:00:00 2001 From: Juliana Fajardini Date: Wed, 14 Apr 2021 23:12:28 +0100 Subject: [PATCH] host/storage: use dedicated 'id' type - 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 | 6 ++-- src/detect-engine-threshold.c | 6 ++-- src/detect-engine-threshold.h | 5 ++-- src/host-bit.c | 6 ++-- src/host-storage.c | 55 +++++++++++++++++++---------------- src/host-storage.h | 17 +++++++---- 6 files changed, 53 insertions(+), 42 deletions(-) diff --git a/src/detect-engine-tag.c b/src/detect-engine-tag.c index f79548eb61..27febe3f33 100644 --- a/src/detect-engine-tag.c +++ b/src/detect-engine-tag.c @@ -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); diff --git a/src/detect-engine-threshold.c b/src/detect-engine-threshold.c index 1ba59a5090..5856406c65 100644 --- a/src/detect-engine-threshold.c +++ b/src/detect-engine-threshold.c @@ -69,10 +69,10 @@ #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"); } diff --git a/src/detect-engine-threshold.h b/src/detect-engine-threshold.h index 9bab025de7..b55c1d2c13 100644 --- a/src/detect-engine-threshold.h +++ b/src/detect-engine-threshold.h @@ -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 @@ -28,10 +28,11 @@ #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); diff --git a/src/host-bit.c b/src/host-bit.c index 7df8501317..a1f0b73b20 100644 --- a/src/host-bit.c +++ b/src/host-bit.c @@ -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"); } } diff --git a/src/host-storage.c b/src/host-storage.c index ba1f812a46..72261de99d 100644 --- a/src/host-storage.c +++ b/src/host-storage.c @@ -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) diff --git a/src/host-storage.h b/src/host-storage.h index b2bccbe26e..25e8b962c1 100644 --- a/src/host-storage.h +++ b/src/host-storage.h @@ -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 @@ -29,17 +29,22 @@ #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__ */ -- 2.47.2