Fix radix and stateful detect engine memory leaks.

remotes/origin/master-1.0.x
Victor Julien 15 years ago
parent 747daf4bce
commit bc7c9d928f

@ -114,7 +114,7 @@ void DetectEngineStateFree(DetectEngineState *state) {
if (state == NULL)
return;
if (state->head == NULL) {
if (state->head != NULL) {
DeStateStoreFree(state->head);
}
@ -134,7 +134,7 @@ void DetectEngineStateReset(DetectEngineState *state) {
SCReturn;
}
if (state->head == NULL) {
if (state->head != NULL) {
DeStateStoreFree(state->head);
}
state->head = NULL;
@ -453,6 +453,8 @@ continue_where_we_left_off() {
#ifdef UNITTESTS
#include "flow-util.h"
static int DeStateTest01(void) {
SCLogDebug("sizeof(DetectEngineState)\t\t%"PRIuMAX,
(uintmax_t)sizeof(DetectEngineState));
@ -700,7 +702,6 @@ static int DeStateTest04(void) {
uint32_t httplen5 = sizeof(httpbuf5) - 1; /* minus the \0 */
uint32_t httplen6 = sizeof(httpbuf6) - 1; /* minus the \0 */
uint32_t httplen7 = sizeof(httpbuf7) - 1; /* minus the \0 */
HtpState *http_state = NULL;
memset(&th_v, 0, sizeof(th_v));
memset(&p, 0, sizeof(p));
@ -842,9 +843,8 @@ static int DeStateTest04(void) {
result = 1;
end:
if (http_state != NULL) {
HTPStateFree(http_state);
}
CLEAR_FLOW(&f);
if (det_ctx != NULL) {
DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
}

@ -141,8 +141,7 @@ static SCRadixUserData *SCRadixAllocSCRadixUserData(uint8_t netmask, void *user)
{
SCRadixUserData *user_data = SCMalloc(sizeof(SCRadixUserData));
if (user_data == NULL) {
SCLogError(SC_ERR_FATAL, "Fatal error encountered in SCRadixAllocSCRadixUserData. Exiting...");
exit(EXIT_FAILURE);
return NULL;
}
memset(user_data, 0, sizeof(SCRadixUserData));
@ -238,11 +237,13 @@ static SCRadixPrefix *SCRadixCreatePrefix(uint8_t *key_stream,
}
if ( (prefix = SCMalloc(sizeof(SCRadixPrefix))) == NULL)
return NULL;
goto error;
memset(prefix, 0, sizeof(SCRadixPrefix));
if ( (prefix->stream = SCMalloc(key_bitlen / 8)) == NULL)
return NULL;
goto error;
memset(prefix->stream, 0, key_bitlen / 8);
memcpy(prefix->stream, key_stream, key_bitlen / 8);
@ -250,6 +251,13 @@ static SCRadixPrefix *SCRadixCreatePrefix(uint8_t *key_stream,
prefix->user_data = SCRadixAllocSCRadixUserData(netmask, user);
return prefix;
error:
if (prefix != NULL) {
SCFree(prefix);
}
return NULL;
}
/**
@ -477,6 +485,10 @@ static void SCRadixReleaseNode(SCRadixNode *node, SCRadixTree *tree)
{
if (node != NULL) {
SCRadixReleasePrefix(node->prefix, tree);
if (node->netmasks != NULL)
SCFree(node->netmasks);
SCFree(node);
}
@ -577,6 +589,11 @@ static SCRadixNode *SCRadixAddKey(uint8_t *key_stream, uint16_t key_bitlen,
int j = 0;
int temp = 0;
if (tree == NULL) {
SCLogError(SC_ERR_INVALID_ARGUMENT, "Argument \"tree\" NULL");
return NULL;
}
/* chop the ip address against a netmask */
SCRadixChopIPAddressAgainstNetmask(key_stream, netmask, key_bitlen);
@ -586,11 +603,6 @@ static SCRadixNode *SCRadixAddKey(uint8_t *key_stream, uint16_t key_bitlen,
return NULL;
}
if (tree == NULL) {
SCLogError(SC_ERR_INVALID_ARGUMENT, "Argument \"tree\" NULL");
return NULL;
}
/* the very first element in the radix tree */
if (tree->head == NULL) {
node = SCRadixCreateNode();
@ -599,6 +611,7 @@ static SCRadixNode *SCRadixAddKey(uint8_t *key_stream, uint16_t key_bitlen,
tree->head = node;
if (netmask == 255 || (netmask == 32 && key_bitlen == 32) || (netmask == 128 && key_bitlen == 128))
return node;
/* if we have reached here, we are actually having a proper netblock in
* our hand(i.e. < 32 for ipv4 and < 128 for ipv6). Add the netmask for
* this node. The reason we add netmasks other than 32 and 128, is

@ -148,5 +148,4 @@ void SCRadixPrintNodeInfo(SCRadixNode *, int, void (*PrintData)(void*));
void SCRadixRegisterTests(void);
#endif /* __UTIL_RADIX_TREE_H__ */

Loading…
Cancel
Save