Branch data Line data Source code
1 : : #include "precizer.h"
2 : :
3 : : /**
4 : : * @brief Match one relative path against a compiled PCRE2 pattern
5 : : *
6 : : * @param[in] compiled_pattern Pre-compiled PCRE2 pattern
7 : : * @param[in] relative_path Relative path to check
8 : : * @return MATCH, NOT_MATCH, or REGEXP_ERROR
9 : : */
10 : 7124 : REGEXP match_regexp(
11 : : pcre2_code *compiled_pattern,
12 : : const memory *relative_path)
13 : : {
14 [ + + + + ]: 7124 : if(compiled_pattern == NULL || relative_path == NULL)
15 : : {
16 : 2 : return(REGEXP_ERROR);
17 : : }
18 : :
19 : 7122 : size_t path_length = 0;
20 : :
21 [ + + ]: 7122 : if(SUCCESS != m_string_length(relative_path,&path_length))
22 : : {
23 : 1 : return(REGEXP_ERROR);
24 : : }
25 : :
26 : 7121 : const char *runtime_relative_path = m_text(relative_path);
27 : :
28 : 7121 : const unsigned char *path_as_pcre2_subject = (const unsigned char *)runtime_relative_path;
29 : 7121 : uint32_t match_options = 0;
30 : :
31 : 7121 : pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(compiled_pattern,NULL);
32 : :
33 [ - + ]: 7121 : if(match_data == NULL)
34 : : {
35 : 0 : slog(ERROR,"PCRE2 failed to allocate match data for path: %s\n",runtime_relative_path);
36 : 0 : return(REGEXP_ERROR);
37 : : }
38 : :
39 : 7121 : int match_result = pcre2_match(compiled_pattern,path_as_pcre2_subject,path_length,0,match_options,match_data,NULL);
40 : :
41 : 7121 : pcre2_match_data_free(match_data);
42 : :
43 [ + + ]: 7121 : if(match_result > 0)
44 : : {
45 : 1004 : return(MATCH);
46 : : }
47 : :
48 [ + + ]: 6117 : if(match_result == PCRE2_ERROR_NOMATCH)
49 : : {
50 : 6116 : return(NOT_MATCH);
51 : : }
52 : :
53 : : /* match_result == 0: ovector too small (should not happen with create_from_pattern)
54 : : match_result < 0 and not NOMATCH: other PCRE2 error */
55 : : PCRE2_UCHAR8 error_message_buffer[MAX_CHARACTERS];
56 : :
57 : 1 : pcre2_get_error_message(match_result,error_message_buffer,MAX_CHARACTERS);
58 : :
59 : 1 : slog(ERROR,"PCRE2 match error %d: %s for path: %s\n",match_result,error_message_buffer,runtime_relative_path);
60 : :
61 : 1 : return(REGEXP_ERROR);
62 : : }
|