From 67f0d0f00d5c5341eddf665c4144fa0b0fb16e97 Mon Sep 17 00:00:00 2001 From: weiss Date: Mon, 3 Jul 2017 13:20:55 +0800 Subject: [PATCH 1/4] fix regex bug when prts_closure num > 4,pt_error can't enough to use ,and pt_error don't store new prts_closure, when need back ,will error --- src/os_regex/os_regex_execute.c | 99 +++++++++++++++++++-------------- 1 file changed, 57 insertions(+), 42 deletions(-) diff --git a/src/os_regex/os_regex_execute.c b/src/os_regex/os_regex_execute.c index faba57c3b..079daa2dd 100644 --- a/src/os_regex/os_regex_execute.c +++ b/src/os_regex/os_regex_execute.c @@ -7,6 +7,7 @@ * Foundation */ +#include "shared.h" #include #include #include @@ -116,15 +117,16 @@ static const char *_OS_Regex(const char *pattern, const char *str, const char ** const char *pt = pattern; const char *next_pt; - const char *pt_error[4] = {NULL, NULL, NULL, NULL}; - const char *pt_error_str[4] = {NULL, NULL, NULL, NULL}; + char **pt_error = NULL; + char **pt_error_str = NULL; + unsigned int pt_size = 0; /* Will loop the whole string, trying to find a match */ do { switch (*pt) { case '\0': if (!(flags & END_SET) || ((flags & END_SET) && (*st == '\0'))) { - return (r_code); + goto cleanpt; } break; @@ -145,7 +147,7 @@ static const char *_OS_Regex(const char *pattern, const char *str, const char ** pt++; if (*pt == '\0') { if (!(flags & END_SET) || ((flags & END_SET) && (*st == '\0'))) { - return (r_code); + goto cleanpt; } } break; @@ -247,20 +249,29 @@ static const char *_OS_Regex(const char *pattern, const char *str, const char ** if (ok_here) { next_pt += ok_here; } - - if (!pt_error[0]) { - pt_error[0] = pt; - pt_error_str[0] = st; - } else if (!pt_error[1]) { - pt_error[1] = pt; - pt_error_str[1] = st; - } else if (!pt_error[2]) { - pt_error[2] = pt; - pt_error_str[2] = st; - - } else if (!pt_error[3]) { - pt_error[3] = pt; - pt_error_str[3] = st; + if (!pt_error) { + os_calloc(2, sizeof(char *), pt_error); + pt_error[0] = NULL; + pt_error[1] = NULL; + os_calloc(2, sizeof(char *), pt_error_str); + pt_error_str[0] = NULL; + pt_error_str[1] = NULL; + } else { + while (pt_error[pt_size] != NULL) { + pt_size++; + } + os_realloc(pt_error, + sizeof(char *) * (pt_size + 2), + pt_error); + pt_error[pt_size+1] = NULL; + os_realloc(pt_error_str, + sizeof(char *) * (pt_size + 2), + pt_error_str); + pt_error_str[pt_size+1] = NULL; + } + if(!pt_error[pt_size]){ + pt_error[pt_size] = pt; + pt_error_str[pt_size] = st; } pt = next_pt; @@ -295,7 +306,7 @@ static const char *_OS_Regex(const char *pattern, const char *str, const char ** else if ((*(pt + 3) == '\0') && (_regex_matched == 1) && (r_code)) { r_code = st; if (!(flags & END_SET) || ((flags & END_SET) && (*st == '\0'))) { - return (r_code); + goto cleanpt; } } @@ -333,31 +344,25 @@ static const char *_OS_Regex(const char *pattern, const char *str, const char ** } /* Error Handling */ - if (pt_error[3]) { - pt = pt_error[3]; - st = pt_error_str[3]; - pt_error[3] = NULL; - continue; - } else if (pt_error[2]) { - pt = pt_error[2]; - st = pt_error_str[2]; - pt_error[2] = NULL; - continue; - } else if (pt_error[1]) { - pt = pt_error[1]; - st = pt_error_str[1]; - pt_error[1] = NULL; - continue; - } else if (pt_error[0]) { - pt = pt_error[0]; - st = pt_error_str[0]; - pt_error[0] = NULL; - continue; + while(pt_size>0){ + if(!pt_error[pt_size]){ + pt_size--; + } + break; + } + if(pt_error){ + if(pt_error[pt_size]){ + pt = pt_error[pt_size]; + st = pt_error_str[pt_size]; + pt_error_str[pt_size] = NULL; + continue; + } } else if (flags & BEGIN_SET) { /* If we get an error and the "^" option is * set, we can return "not matched" in here. */ - return (NULL); + r_code = NULL; + goto cleanpt; } else if (st_error) { st = st_error; st_error = NULL; @@ -408,9 +413,19 @@ static const char *_OS_Regex(const char *pattern, const char *str, const char ** (pt++) && ENDOFFILE(pt)) ) { - return (r_code); + goto cleanpt; } - return (NULL); + r_code = NULL; + cleanpt: + if(pt_error){ + os_free(pt_error); + pt_error = NULL; + } + if(pt_error_str){ + os_free(pt_error_str); + pt_error_str = NULL; + } + return (r_code); } From 8ba1c9d25c0c65a94bffaa3c3e78c562d6d1eef3 Mon Sep 17 00:00:00 2001 From: weiss Date: Tue, 11 Jul 2017 18:35:57 +0800 Subject: [PATCH 2/4] Update os_regex_execute.c --- src/os_regex/os_regex_execute.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/os_regex/os_regex_execute.c b/src/os_regex/os_regex_execute.c index 079daa2dd..2a4f459b3 100644 --- a/src/os_regex/os_regex_execute.c +++ b/src/os_regex/os_regex_execute.c @@ -117,8 +117,8 @@ static const char *_OS_Regex(const char *pattern, const char *str, const char ** const char *pt = pattern; const char *next_pt; - char **pt_error = NULL; - char **pt_error_str = NULL; + const char **pt_error = NULL; + const char **pt_error_str = NULL; unsigned int pt_size = 0; /* Will loop the whole string, trying to find a match */ From 0d403e100cf315638d98de4b20345540b9ae85a8 Mon Sep 17 00:00:00 2001 From: weiss Date: Wed, 12 Jul 2017 10:15:22 +0800 Subject: [PATCH 3/4] Update Makefile --- src/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Makefile b/src/Makefile index ed6343c33..ad50ff13a 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1149,7 +1149,7 @@ test_os_zlib: tests/test_os_zlib.o ${ZLIB_LIB} test_os_xml: tests/test_os_xml.o ${os_xml_o} ${OSSEC_CCBIN} ${OSSEC_CFLAGS} $^ ${OSSEC_LDFLAGS} -o $@ -test_os_regex: tests/test_os_regex.c ${os_regex_o} +test_os_regex: tests/test_os_regex.c ${shared_o} ${os_xml_o} ${os_net_o} ${os_regex_o} ${OSSEC_CCBIN} ${OSSEC_CFLAGS} $^ ${OSSEC_LDFLAGS} -o $@ test_os_crypto: tests/test_os_crypto.c ${crypto_o} ${shared_o} ${os_xml_o} ${os_net_o} ${os_regex_o} ${ZLIB_LIB} From f350641e910867aa8b490ffb1c6ede201b287f68 Mon Sep 17 00:00:00 2001 From: weiss Date: Thu, 13 Jul 2017 11:52:34 +0800 Subject: [PATCH 4/4] Update os_regex_execute.c fix memory free bug --- src/os_regex/os_regex_execute.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/os_regex/os_regex_execute.c b/src/os_regex/os_regex_execute.c index 2a4f459b3..6b94f0d74 100644 --- a/src/os_regex/os_regex_execute.c +++ b/src/os_regex/os_regex_execute.c @@ -419,10 +419,28 @@ static const char *_OS_Regex(const char *pattern, const char *str, const char ** r_code = NULL; cleanpt: if(pt_error){ + pt_size = 0; + do{ + if(pt_error[pt_size]){ + os_free(pt_error[pt_size]); + pt_error[pt_size] = NULL; + }else{ + break; + } + }while(pt_size++); os_free(pt_error); pt_error = NULL; } if(pt_error_str){ + pt_size = 0; + do{ + if(pt_error_str[pt_size]){ + os_free(pt_error_str[pt_size]); + pt_error_str[pt_size] = NULL; + }else{ + break; + } + }while(pt_size++); os_free(pt_error_str); pt_error_str = NULL; }