iprep: unify free handling

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 172d2b28a5)
pull/7127/head
Victor Julien 4 years ago committed by Shivani Bhardwaj
parent 5753833e2e
commit 1ec3a574c7

@ -156,12 +156,13 @@ error:
void HostClearMemory(Host *h) void HostClearMemory(Host *h)
{ {
if (h->iprep != NULL) { if (h->iprep != NULL) {
SCFree(h->iprep); SRepFreeHostData(h);
h->iprep = NULL;
} }
if (HostStorageSize() > 0) if (HostStorageSize() > 0)
HostFreeStorage(h); HostFreeStorage(h);
BUG_ON(SC_ATOMIC_GET(h->use_cnt) > 0);
} }
#define HOST_DEFAULT_HASHSIZE 4096 #define HOST_DEFAULT_HASHSIZE 4096
@ -311,7 +312,6 @@ void HostShutdown(void)
/* free spare queue */ /* free spare queue */
while((h = HostDequeue(&host_spare_q))) { while((h = HostDequeue(&host_spare_q))) {
BUG_ON(SC_ATOMIC_GET(h->use_cnt) > 0);
HostFree(h); HostFree(h);
} }

@ -38,6 +38,7 @@
#include "conf.h" #include "conf.h"
#include "detect.h" #include "detect.h"
#include "reputation.h" #include "reputation.h"
#include "util-validate.h"
/** effective reputation version, atomic as the host /** effective reputation version, atomic as the host
* time out code will use it to check if a host's * 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()); 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 /** \brief Set effective reputation version after
* reputation initialization is complete. */ * reputation initialization is complete. */
static void SRepInitComplete(void) static void SRepInitComplete(void)
@ -205,11 +214,7 @@ int SRepHostTimedOut(Host *h)
if (r->version < eversion) { if (r->version < eversion) {
SCLogDebug("host %p has reputation version %u, " SCLogDebug("host %p has reputation version %u, "
"effective version is %u", h, r->version, eversion); "effective version is %u", h, r->version, eversion);
SRepFreeHostData(h);
SCFree(h->iprep);
h->iprep = NULL;
HostDecrUsecnt(h);
return 1; return 1;
} }

@ -41,6 +41,7 @@ typedef struct SReputation_ {
uint8_t rep[SREP_MAX_CATS]; uint8_t rep[SREP_MAX_CATS];
} SReputation; } SReputation;
void SRepFreeHostData(Host *h);
uint8_t SRepCatGetByShortname(char *shortname); uint8_t SRepCatGetByShortname(char *shortname);
int SRepInit(struct DetectEngineCtx_ *de_ctx); int SRepInit(struct DetectEngineCtx_ *de_ctx);
void SRepDestroy(struct DetectEngineCtx_ *de_ctx); void SRepDestroy(struct DetectEngineCtx_ *de_ctx);

Loading…
Cancel
Save