From 2ce8895f0ad7b7b3df583b9c9b5518b12b3bc08f Mon Sep 17 00:00:00 2001 From: Victor Julien Date: Wed, 27 Nov 2013 16:28:37 +0100 Subject: [PATCH] address and port: reduce memory allocs --- src/detect-engine-address.c | 20 +++++++------------- src/detect-engine-port.c | 11 +++-------- 2 files changed, 10 insertions(+), 21 deletions(-) diff --git a/src/detect-engine-address.c b/src/detect-engine-address.c index 9e2d9356ca..66d70f11cc 100644 --- a/src/detect-engine-address.c +++ b/src/detect-engine-address.c @@ -600,27 +600,24 @@ int DetectAddressParseString(DetectAddress *dd, char *str) char *ip2 = NULL; char *mask = NULL; int r = 0; + char ipstr[256]; while (*str != '\0' && *str == ' ') str++; - char *ipdup = SCStrdup(str); - if (unlikely(ipdup == NULL)) - return -1; - SCLogDebug("str %s", str); - /* first handle 'any' */ if (strcasecmp(str, "any") == 0) { dd->flags |= ADDRESS_FLAG_ANY; - SCFree(ipdup); - SCLogDebug("address is \'any\'"); - return 0; } - /* we dup so we can put a nul-termination in it later */ - ip = ipdup; + strlcpy(ipstr, str, sizeof(ipstr)); + SCLogDebug("str %s", str); + + /* we work with a copy so that we can put a + * nul-termination in it later */ + ip = ipstr; /* handle the negation case */ if (ip[0] == '!') { @@ -757,14 +754,11 @@ int DetectAddressParseString(DetectAddress *dd, char *str) } - SCFree(ipdup); - BUG_ON(dd->ip.family == 0); return 0; error: - SCFree(ipdup); return -1; } diff --git a/src/detect-engine-port.c b/src/detect-engine-port.c index 8b4659027a..4bd51d9163 100644 --- a/src/detect-engine-port.c +++ b/src/detect-engine-port.c @@ -1402,11 +1402,9 @@ error: DetectPort *PortParse(char *str) { char *port2 = NULL; DetectPort *dp = NULL; - char *portdup = SCStrdup(str); - if (unlikely(portdup == NULL)) { - return NULL; - } + char portstr[16]; + strlcpy(portstr, str, sizeof(portstr)); dp = DetectPortInit(); if (dp == NULL) @@ -1415,7 +1413,7 @@ DetectPort *PortParse(char *str) { /* XXX better input validation */ /* we dup so we can put a nul-termination in it later */ - char *port = portdup; + char *port = portstr; /* handle the negation case */ if (port[0] == '!') { @@ -1457,14 +1455,11 @@ DetectPort *PortParse(char *str) { } } - SCFree(portdup); return dp; error: if (dp != NULL) DetectPortCleanupList(dp); - - if (portdup) SCFree(portdup); return NULL; }