Fix memory leak in proto - name mapping

==15745== 3 bytes in 1 blocks are definitely lost in loss record 5 of 615
==15745==    at 0x4C2A2DB: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==15745==    by 0x71858C1: strdup (strdup.c:42)
==15745==    by 0xA20814: SCProtoNameInit (util-proto-name.c:75)
==15745==    by 0x952D1B: PostConfLoadedSetup (suricata.c:1983)
==15745==    by 0x9537CD: main (suricata.c:2112)

Also, clean up and add a check to make sure it's initialized only once.
pull/859/head
Victor Julien 12 years ago
parent bb4def7949
commit fd6fd9ce48

@ -27,12 +27,18 @@
#include "suricata-common.h" #include "suricata-common.h"
#include "util-proto-name.h" #include "util-proto-name.h"
static int init_once = 0;
/** /**
* \brief Function to load the protocol names from the specified protocol * \brief Function to load the protocol names from the specified protocol
* file. * file.
*/ */
void SCProtoNameInit() void SCProtoNameInit()
{ {
BUG_ON(init_once);
init_once++;
memset(known_proto, 0x00, sizeof(known_proto));
/* Load the known protocols name from the /etc/protocols file */ /* Load the known protocols name from the /etc/protocols file */
FILE *fp = fopen(PROTO_FILE,"r"); FILE *fp = fopen(PROTO_FILE,"r");
if (fp != NULL) { if (fp != NULL) {
@ -71,6 +77,10 @@ void SCProtoNameInit()
char *cname = strtok_r(NULL, " \t", &ptr); char *cname = strtok_r(NULL, " \t", &ptr);
#endif /* __WIN32 */ #endif /* __WIN32 */
if (known_proto[proto] != NULL) {
SCFree(known_proto[proto]);
}
if (cname != NULL) { if (cname != NULL) {
known_proto[proto] = SCStrdup(cname); known_proto[proto] = SCStrdup(cname);
} else { } else {
@ -111,9 +121,10 @@ uint8_t SCProtoNameValid(uint16_t proto)
*/ */
void SCProtoNameDeInit() void SCProtoNameDeInit()
{ {
int cnt;
/* clears the memory of loaded protocol names */ /* clears the memory of loaded protocol names */
for (uint8_t cnt=0;cnt < 255;cnt++) { for (cnt = 0; cnt < 255; cnt++) {
if(known_proto[cnt] != NULL) if (known_proto[cnt] != NULL)
SCFree(known_proto[cnt]); SCFree(known_proto[cnt]);
} }
} }

Loading…
Cancel
Save