From cee1e6cc9bfb25b6630719045e15fbf0a3044069 Mon Sep 17 00:00:00 2001 From: Mole Shang <135e2@135e2.dev> Date: Tue, 8 Aug 2023 21:10:36 +0800 Subject: utils: fix multi-patterns matching --- src/utils/utils.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) (limited to 'src/utils') diff --git a/src/utils/utils.c b/src/utils/utils.c index 16d39d9..f3ebbb4 100644 --- a/src/utils/utils.c +++ b/src/utils/utils.c @@ -34,14 +34,14 @@ int regex_match(const char *subject, str_array_t patterns, match_data = pcre2_match_data_create_from_pattern(re, NULL); - unsigned char i = 0; while (offset < subject_len && (rc = pcre2_match(re, (PCRE2_SPTR)subject, (PCRE2_SIZE)subject_len, offset, 0, match_data, NULL)) > 0) { // results->str = realloc(results->str, sizeof(char *) * (rc + // results->n)); - resize_str_array(results, rc + i); + int valid_n = results->n; + resize_str_array(results, rc - 1 + results->n); ovector = pcre2_get_ovector_pointer(match_data); DEBUG_PRINT("Get %d captures.\n", rc - 1); @@ -53,14 +53,14 @@ int regex_match(const char *subject, str_array_t patterns, /* Here we need to manually control the str array, * as PCRE2_SPTR == const unsigned char * (which cannot be directly casted) */ - results->str[j] = malloc(substring_length + 1); - sprintf(results->str[j], "%.*s", (int)substring_length, substring); + results->str[valid_n + j - 1] = malloc(substring_length + 1); + sprintf(results->str[valid_n + j - 1], "%.*s", (int)substring_length, + substring); DEBUG_PRINT("index: %2d, substring_length: %d\n", j, (int)substring_length); } offset = ovector[1]; DEBUG_PRINT("offset: %zu, subject_len: %zu\n", offset, subject_len); - i++; } pcre2_match_data_free(match_data); pcre2_code_free(re); @@ -69,7 +69,6 @@ int regex_match(const char *subject, str_array_t patterns, switch (rc) { case PCRE2_ERROR_NOMATCH: DEBUG_PRINT("No match found.\n"); - return 0; break; case 0: LOG("PCRE2", @@ -95,7 +94,6 @@ int repchr(char *str, char t, char r) { return c; } - void free_and_nullify(void *p) { if (p) { free(p); -- cgit v1.2.3