]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
iprep: unify free handling
authorVictor Julien <vjulien@oisf.net>
Wed, 23 Feb 2022 10:05:40 +0000 (11:05 +0100)
committerJeff Lucovsky <jeff@lucovsky.org>
Fri, 11 Mar 2022 14:03:32 +0000 (09:03 -0500)
Introduce a new util function to free a Hosts iprep code. It also
handles the Host use_cnt decrement.

This change makes sure we also decrement the use_cnt when cleaning
up when shutting down the host table.

Move the BUG_ON check for use_cnt into the HostClearMemory() func
to check it in more cases.

(cherry picked from commit 172d2b28a58d923ddbc5644dd3bfb52e353b8a55)

src/host.c
src/reputation.c
src/reputation.h

index 7db2d958f4a30055e3a667728a048239c3a487ff..4a08378293a9702eef847a15fc903cd4c9083ed6 100644 (file)
@@ -158,12 +158,13 @@ error:
 void HostClearMemory(Host *h)
 {
     if (h->iprep != NULL) {
-        SCFree(h->iprep);
-        h->iprep = NULL;
+        SRepFreeHostData(h);
     }
 
     if (HostStorageSize() > 0)
         HostFreeStorage(h);
+
+    BUG_ON(SC_ATOMIC_GET(h->use_cnt) > 0);
 }
 
 #define HOST_DEFAULT_HASHSIZE 4096
@@ -313,7 +314,6 @@ void HostShutdown(void)
 
     /* free spare queue */
     while((h = HostDequeue(&host_spare_q))) {
-        BUG_ON(SC_ATOMIC_GET(h->use_cnt) > 0);
         HostFree(h);
     }
 
index 95f56532d69fb6bb4be639f56989c294e507ed32..330bf349c47523d5db7ce0dea605eadc4ce32d92 100644 (file)
@@ -37,6 +37,7 @@
 #include "conf.h"
 #include "detect.h"
 #include "reputation.h"
+#include "util-validate.h"
 
 /** effective reputation version, atomic as the host
  *  time out code will use it to check if a host's
@@ -175,6 +176,14 @@ void SRepReloadComplete(void)
     SCLogDebug("effective Reputation version %u", SRepGetEffectiveVersion());
 }
 
+void SRepFreeHostData(Host *h)
+{
+    SCFree(h->iprep);
+    h->iprep = NULL;
+    DEBUG_VALIDATE_BUG_ON(SC_ATOMIC_GET(h->use_cnt) != 1);
+    HostDecrUsecnt(h);
+}
+
 /** \brief Set effective reputation version after
  *         reputation initialization is complete. */
 static void SRepInitComplete(void)
@@ -205,11 +214,7 @@ int SRepHostTimedOut(Host *h)
     if (r->version < eversion) {
         SCLogDebug("host %p has reputation version %u, "
                 "effective version is %u", h, r->version, eversion);
-
-        SCFree(h->iprep);
-        h->iprep = NULL;
-
-        HostDecrUsecnt(h);
+        SRepFreeHostData(h);
         return 1;
     }
 
index 48d990ae00132f143f48d25613d26cc82289a10b..52c0756e80f769df227632e1d453d45c5fc09549 100644 (file)
@@ -41,6 +41,7 @@ typedef struct SReputation_ {
     uint8_t rep[SREP_MAX_CATS];
 } SReputation;
 
+void SRepFreeHostData(Host *h);
 uint8_t SRepCatGetByShortname(char *shortname);
 int SRepInit(struct DetectEngineCtx_ *de_ctx);
 void SRepDestroy(struct DetectEngineCtx_ *de_ctx);