LCOV - code coverage report
Current view: top level - src - match_regexp.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 68.4 % 19 13
Test Date: 2026-03-31 13:51:38 Functions: 100.0 % 1 1
Branches: 60.0 % 10 6

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

Generated by: LCOV version 2.0-1