]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
Change ParseSize api to not leak memory and only setup pcre once.
authorVictor Julien <victor@inliniac.net>
Wed, 25 Sep 2013 08:15:30 +0000 (10:15 +0200)
committerVictor Julien <victor@inliniac.net>
Wed, 25 Sep 2013 08:15:30 +0000 (10:15 +0200)
src/suricata.c
src/util-misc.c
src/util-misc.h

index 5b65700bb44c5c210a6fe06d07e69402ed0c9a88..061b37bb791ad12569b3547c53a74e737cd1c9e6 100644 (file)
@@ -1777,6 +1777,8 @@ int main(int argc, char **argv)
         SCLogWarning(SC_ERR_THREAD_INIT, "Unable to set thread name");
     }
 
+    ParseSizeInit();
+
     RunModeRegisterRunModes();
 
     /* By default use IDS mode, but if nfq or ipfw
@@ -2177,6 +2179,7 @@ int main(int argc, char **argv)
     MagicDeinit();
     TmqhCleanup();
     TmModuleRunDeInit();
+    ParseSizeDeinit();
 
 #ifdef HAVE_AF_PACKET
     AFPPeersListClean();
index cc716f24a12c6ea86fcfba40a911128426a01440..4b0847fc4537a7979c3818b883b328f1c8d72ea3 100644 (file)
 #include "util-debug.h"
 #include "util-unittest.h"
 
-/* size string parsing API */
-
-static int ParseSizeString(const char *size, double *res)
-{
 #define PARSE_REGEX "^\\s*(\\d+(?:.\\d+)?)\\s*([a-zA-Z]{2})?\\s*$"
+static pcre *parse_regex = NULL;
+static pcre_extra *parse_regex_study = NULL;
 
-    pcre *parse_regex;
-    pcre_extra *parse_regex_study;
+void ParseSizeInit(void) {
     const char *eb;
     int eo;
     int opts = 0;
-#define MAX_SUBSTRINGS 30
-    int pcre_exec_ret;
-    int r;
-    int ov[MAX_SUBSTRINGS];
-    int retval = 0;
-
-    *res = 0;
 
     parse_regex = pcre_compile(PARSE_REGEX, opts, &eb, &eo, NULL);
     if (parse_regex == NULL) {
         SCLogError(SC_ERR_PCRE_COMPILE, "Compile of \"%s\" failed at offset "
                    "%" PRId32 ": %s", PARSE_REGEX, eo, eb);
-        retval = -2;
-        goto end;
+        exit(EXIT_FAILURE);
     }
-
     parse_regex_study = pcre_study(parse_regex, 0, &eb);
     if (eb != NULL) {
         SCLogError(SC_ERR_PCRE_STUDY, "pcre study failed: %s", eb);
-        retval = -2;
-        goto end;
+        exit(EXIT_FAILURE);
     }
+}
+
+void ParseSizeDeinit(void) {
+
+    if (parse_regex != NULL)
+        pcre_free(parse_regex);
+    if (parse_regex_study != NULL)
+        pcre_free_study(parse_regex_study);
+}
+
+/* size string parsing API */
+
+static int ParseSizeString(const char *size, double *res)
+{
+#define MAX_SUBSTRINGS 30
+    int pcre_exec_ret;
+    int r;
+    int ov[MAX_SUBSTRINGS];
+    int retval = 0;
+    const char *str_ptr = NULL;
+    const char *str_ptr2 = NULL;
+
+    *res = 0;
 
     pcre_exec_ret = pcre_exec(parse_regex, parse_regex_study, size, strlen(size), 0, 0,
                     ov, MAX_SUBSTRINGS);
@@ -76,7 +86,6 @@ static int ParseSizeString(const char *size, double *res)
         goto end;
     }
 
-    const char *str_ptr;
     r = pcre_get_substring((char *)size, ov, MAX_SUBSTRINGS, 1,
                              &str_ptr);
     if (r < 0) {
@@ -97,22 +106,21 @@ static int ParseSizeString(const char *size, double *res)
         retval = -1;
         goto end;
     }
-    pcre_free_substring(str_ptr);
 
     if (pcre_exec_ret == 3) {
         r = pcre_get_substring((char *)size, ov, MAX_SUBSTRINGS, 2,
-                                 &str_ptr);
+                                 &str_ptr2);
         if (r < 0) {
             SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_get_substring failed");
             retval = -2;
             goto end;
         }
 
-        if (strcasecmp(str_ptr, "kb") == 0) {
+        if (strcasecmp(str_ptr2, "kb") == 0) {
             *res *= 1024;
-        } else if (strcasecmp(str_ptr, "mb") == 0) {
+        } else if (strcasecmp(str_ptr2, "mb") == 0) {
             *res *= 1024 * 1024;
-        } else if (strcasecmp(str_ptr, "gb") == 0) {
+        } else if (strcasecmp(str_ptr2, "gb") == 0) {
             *res *= 1024 * 1024 * 1024;
         } else {
             /* not possible */
@@ -122,6 +130,10 @@ static int ParseSizeString(const char *size, double *res)
 
     retval = 0;
 end:
+    if (str_ptr != NULL)
+        pcre_free_substring(str_ptr);
+    if (str_ptr2 != NULL)
+        pcre_free_substring(str_ptr2);
     return retval;
 }
 
index f7dc48d6662ce6965d1b2b8ef1787927980ffb5d..2245f829331ac197cdc408c25e69a7018efd1813 100644 (file)
@@ -49,4 +49,7 @@ int ParseSizeStringU32(const char *, uint32_t *);
 int ParseSizeStringU64(const char *, uint64_t *);
 void UtilMiscRegisterTests(void);
 
+void ParseSizeInit(void);
+void ParseSizeDeinit(void);
+
 #endif /* __UTIL_MISC_H__ */