Change ParseSize api to not leak memory and only setup pcre once.

pull/559/head
Victor Julien 12 years ago
parent 3d78cc8ca6
commit eedd4329da

@ -1777,6 +1777,8 @@ int main(int argc, char **argv)
SCLogWarning(SC_ERR_THREAD_INIT, "Unable to set thread name"); SCLogWarning(SC_ERR_THREAD_INIT, "Unable to set thread name");
} }
ParseSizeInit();
RunModeRegisterRunModes(); RunModeRegisterRunModes();
/* By default use IDS mode, but if nfq or ipfw /* By default use IDS mode, but if nfq or ipfw
@ -2177,6 +2179,7 @@ int main(int argc, char **argv)
MagicDeinit(); MagicDeinit();
TmqhCleanup(); TmqhCleanup();
TmModuleRunDeInit(); TmModuleRunDeInit();
ParseSizeDeinit();
#ifdef HAVE_AF_PACKET #ifdef HAVE_AF_PACKET
AFPPeersListClean(); AFPPeersListClean();

@ -28,39 +28,49 @@
#include "util-debug.h" #include "util-debug.h"
#include "util-unittest.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*$" #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; void ParseSizeInit(void) {
pcre_extra *parse_regex_study;
const char *eb; const char *eb;
int eo; int eo;
int opts = 0; 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); parse_regex = pcre_compile(PARSE_REGEX, opts, &eb, &eo, NULL);
if (parse_regex == NULL) { if (parse_regex == NULL) {
SCLogError(SC_ERR_PCRE_COMPILE, "Compile of \"%s\" failed at offset " SCLogError(SC_ERR_PCRE_COMPILE, "Compile of \"%s\" failed at offset "
"%" PRId32 ": %s", PARSE_REGEX, eo, eb); "%" PRId32 ": %s", PARSE_REGEX, eo, eb);
retval = -2; exit(EXIT_FAILURE);
goto end;
} }
parse_regex_study = pcre_study(parse_regex, 0, &eb); parse_regex_study = pcre_study(parse_regex, 0, &eb);
if (eb != NULL) { if (eb != NULL) {
SCLogError(SC_ERR_PCRE_STUDY, "pcre study failed: %s", eb); SCLogError(SC_ERR_PCRE_STUDY, "pcre study failed: %s", eb);
retval = -2; exit(EXIT_FAILURE);
goto end;
} }
}
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, pcre_exec_ret = pcre_exec(parse_regex, parse_regex_study, size, strlen(size), 0, 0,
ov, MAX_SUBSTRINGS); ov, MAX_SUBSTRINGS);
@ -76,7 +86,6 @@ static int ParseSizeString(const char *size, double *res)
goto end; goto end;
} }
const char *str_ptr;
r = pcre_get_substring((char *)size, ov, MAX_SUBSTRINGS, 1, r = pcre_get_substring((char *)size, ov, MAX_SUBSTRINGS, 1,
&str_ptr); &str_ptr);
if (r < 0) { if (r < 0) {
@ -97,22 +106,21 @@ static int ParseSizeString(const char *size, double *res)
retval = -1; retval = -1;
goto end; goto end;
} }
pcre_free_substring(str_ptr);
if (pcre_exec_ret == 3) { if (pcre_exec_ret == 3) {
r = pcre_get_substring((char *)size, ov, MAX_SUBSTRINGS, 2, r = pcre_get_substring((char *)size, ov, MAX_SUBSTRINGS, 2,
&str_ptr); &str_ptr2);
if (r < 0) { if (r < 0) {
SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_get_substring failed"); SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_get_substring failed");
retval = -2; retval = -2;
goto end; goto end;
} }
if (strcasecmp(str_ptr, "kb") == 0) { if (strcasecmp(str_ptr2, "kb") == 0) {
*res *= 1024; *res *= 1024;
} else if (strcasecmp(str_ptr, "mb") == 0) { } else if (strcasecmp(str_ptr2, "mb") == 0) {
*res *= 1024 * 1024; *res *= 1024 * 1024;
} else if (strcasecmp(str_ptr, "gb") == 0) { } else if (strcasecmp(str_ptr2, "gb") == 0) {
*res *= 1024 * 1024 * 1024; *res *= 1024 * 1024 * 1024;
} else { } else {
/* not possible */ /* not possible */
@ -122,6 +130,10 @@ static int ParseSizeString(const char *size, double *res)
retval = 0; retval = 0;
end: end:
if (str_ptr != NULL)
pcre_free_substring(str_ptr);
if (str_ptr2 != NULL)
pcre_free_substring(str_ptr2);
return retval; return retval;
} }

@ -49,4 +49,7 @@ int ParseSizeStringU32(const char *, uint32_t *);
int ParseSizeStringU64(const char *, uint64_t *); int ParseSizeStringU64(const char *, uint64_t *);
void UtilMiscRegisterTests(void); void UtilMiscRegisterTests(void);
void ParseSizeInit(void);
void ParseSizeDeinit(void);
#endif /* __UTIL_MISC_H__ */ #endif /* __UTIL_MISC_H__ */

Loading…
Cancel
Save