--- lbbs/src/bwf.c 2025/11/07 04:51:06 1.1 +++ lbbs/src/bwf.c 2025/11/07 09:18:58 1.4 @@ -56,9 +56,15 @@ int bwf_load(const char *filename) return -3; } - if (line[len_line - 1] == '\n') + while (len_line > 0 && (line[len_line - 1] == '\n' || line[len_line - 1] == '\r')) { line[len_line - 1] = '\0'; + len_line--; + } + + if (len_line == 0) + { + continue; } if (p > bwf_pattern_str) @@ -87,6 +93,8 @@ int bwf_load(const char *filename) log_error("Debug: bwf_pattern_str: %s\n", bwf_pattern_str); #endif + bwf_unload(); + bwf_code = pcre2_compile((PCRE2_SPTR)bwf_pattern_str, PCRE2_ZERO_TERMINATED, PCRE2_CASELESS, &errorcode, &erroroffset, NULL); if (bwf_code == NULL) { @@ -109,9 +117,12 @@ void bwf_unload(void) int check_badwords(char *str, char c_mask) { pcre2_match_data *match_data; + PCRE2_SIZE startoffset = 0; PCRE2_SIZE *ovector; + uint32_t match_count; int ret; int i; + int total_match_count = 0; if (bwf_code == NULL) { @@ -121,37 +132,44 @@ int check_badwords(char *str, char c_mas match_data = pcre2_match_data_create_from_pattern(bwf_code, NULL); - ret = pcre2_match(bwf_code, (PCRE2_SPTR)str, PCRE2_ZERO_TERMINATED, 0, 0, match_data, NULL); - if (ret == PCRE2_ERROR_NOMATCH) - { - ret = 0; - } - else if (ret < 0) - { - log_error("pcre2_match() error: %d\n", ret); - } - else if (ret == 0) + while (1) { - log_error("Vector of offsets is too small\n"); - } - else // ret >= 1 - { - ovector = pcre2_get_ovector_pointer(match_data); - i = ret - 1; - - if (ovector[i * 2] == -1 || ovector[i * 2 + 1] == -1) + ret = pcre2_match(bwf_code, (PCRE2_SPTR)str, PCRE2_ZERO_TERMINATED, startoffset, 0, match_data, NULL); + if (ret == PCRE2_ERROR_NOMATCH) + { + ret = total_match_count; + break; + } + else if (ret < 0) + { + log_error("pcre2_match() error: %d\n", ret); + } + else if (ret == 0) { - log_error("Bug: match pattern #%d with invalid offsets [%d, %d)", - i, ovector[i * 2], ovector[i * 2 + 1]); - ret = -2; + log_error("Vector of offsets is too small\n"); } - else + else // ret >= 1 { + ovector = pcre2_get_ovector_pointer(match_data); + match_count = pcre2_get_ovector_count(match_data); + + i = ret - 1; + if (ovector[i * 2] == -1 || ovector[i * 2 + 1] == -1) + { + log_error("Bug: match pattern #%d of %d with invalid offsets [%d, %d)", + i, match_count, ovector[i * 2], ovector[i * 2 + 1]); + ret = -2; + } + else + { #ifdef _DEBUG - log_error("Debug: match pattern #%d at offsets [%d, %d]\n", - i, ovector[i * 2], ovector[i * 2 + 1] - ovector[i * 2]); + log_error("Debug: match pattern #%d of %d at offsets [%d, %d]\n", + i, match_count, ovector[i * 2], ovector[i * 2 + 1] - ovector[i * 2]); #endif - memset(str + ovector[i * 2], c_mask, ovector[i * 2 + 1] - ovector[i * 2]); + memset(str + ovector[i * 2], c_mask, ovector[i * 2 + 1] - ovector[i * 2]); + total_match_count++; + startoffset = ovector[i * 2 + 1]; + } } }