Ignore:
Timestamp:
Oct 26, 2025, 12:17:47 PM (16 hours ago)
Author:
katerina
Message:

Fix for ticket #476 (move logfile monitoring module from PCRE to PCRE2).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/sh_log_parse_apache.c

    r541 r588  
    2828#define FIL__  _("sh_log_parse_apache.c")
    2929
    30 /* Debian/Ubuntu: libpcre3-dev */
    31 #ifdef HAVE_PCRE_PCRE_H
    32 #include <pcre/pcre.h>
     30/* Debian/Ubuntu: libpcre2-dev */
     31#define PCRE2_CODE_UNIT_WIDTH 8
     32#ifdef HAVE_PCRE2_PCRE2_H
     33#include <pcre2/pcre2.h>
    3334#else
    34 #include <pcre.h>
     35#include <pcre2.h>
    3536#endif
    3637
     
    4445
    4546struct sh_fileinfo_apache {
    46   pcre * line_regex;
    47   int * line_ovector;         /* captured substrings     */
    48   int    line_ovecnum;         /* how many captured       */
     47  pcre2_code * line_regex;
     48  PCRE2_SIZE * line_ovector;         /* captured substrings     */
     49  int          line_ovecnum;         /* how many captured       */
    4950 
    5051  int    pos_host;
     
    8283  volatile int          p_time = -1;
    8384  char                * f_time = NULL;
    84   const char * error;
    85   int          erroffset;
     85  int          error;
     86  size_t       erroffset;
    8687 
    8788  /* Take the address to keep gcc from putting them into registers.
     
    261262
    262263  result = SH_ALLOC(sizeof(struct sh_fileinfo_apache));
    263   result->line_regex = pcre_compile(sh_string_str(re_string), 0,
     264  result->line_regex = pcre2_compile((PCRE2_SPTR8)sh_string_str(re_string), sh_string_len(re_string), 0,
    264265                                    &error, &erroffset, NULL);
    265266  if (!(result->line_regex))
     
    285286  sh_string_destroy(&re_string);
    286287
    287   result->line_ovector  = SH_ALLOC(sizeof(int) * (nfields+1) * 3);
     288  result->line_ovector  = NULL;
    288289  result->line_ovecnum  = nfields;
    289290  result->pos_host      = p_host;
     
    304305  char         tstr[128];
    305306  char         sstr[128];
    306   const char * hstr;
     307  char      * hstr;
    307308  int          res;
    308   const char **hstr_addr = (const char **) &hstr;
     309  unsigned char **hstr_addr = (unsigned char **) &hstr;
     310  size_t       hstr_len;
    309311
    310312  struct sh_fileinfo_apache * info = (struct sh_fileinfo_apache *) fileinfo;
    311313
     314  pcre2_match_data * match_data = NULL;
     315 
    312316  if (sh_string_len(logline) > 0 && flag_err_debug == S_TRUE)
    313317    {
     
    324328    }
    325329
    326   res = pcre_exec(info->line_regex, NULL,
    327                   sh_string_str(logline), (int)sh_string_len(logline), 0,
    328                   0, info->line_ovector, (3*(1+info->line_ovecnum)));
    329 
    330   if (res == (1+info->line_ovecnum))
     330  match_data = pcre2_match_data_create_from_pattern(info->line_regex, NULL);
     331 
     332  res = pcre2_match(info->line_regex,
     333                    (PCRE2_SPTR8)sh_string_str(logline), (int)sh_string_len(logline), 0,
     334                    0, match_data, NULL);
     335
     336  if (res == 1+info->line_ovecnum) /* successful match */
    331337    {
    332338      struct sh_logrecord * record;
    333339      time_t timestamp = 0;
    334 
     340      size_t size;
     341
     342      info->line_ovector = pcre2_get_ovector_pointer(match_data);
     343     
    335344      if (info->pos_time > 0)
    336345        {
    337           res = pcre_copy_substring(sh_string_str(logline),
    338                                     info->line_ovector, res,
    339                                     info->pos_time, tstr, sizeof(tstr));
    340           if (res <= 0)
     346          size = sizeof(tstr);
     347          res = pcre2_substring_copy_bynumber(match_data,  info->pos_time,
     348                                              (PCRE2_UCHAR8 *)tstr, &size);
     349          if (res != 0)
    341350            goto corrupt;
    342351        }
    343352      else
    344353        {
    345           res = 0;
     354          res = -1;
    346355          timestamp = 0;
    347356          info->format_time = sh_util_strdup(_("%d/%b/%Y:%T"));
     
    349358        }
    350359
    351       if (res > 0)
     360      if (res == 0)
    352361        {
    353362          struct tm btime;
     
    371380      if (info->pos_status > 0)
    372381        {
    373           res = pcre_copy_substring(sh_string_str(logline),
    374                                     info->line_ovector, res,
    375                                     info->pos_status, sstr, sizeof(sstr));
    376           if (res <= 0)
     382          size = sizeof(sstr);
     383          res = pcre2_substring_copy_bynumber(match_data,  info->pos_status,
     384                                              (PCRE2_UCHAR8 *)sstr, &size);
     385          if (res != 0)
    377386            goto corrupt;
    378387        }
     
    384393      if (info->pos_host > 0)
    385394        {
    386           res = pcre_get_substring(sh_string_str(logline),
    387                                    info->line_ovector, res,
    388                                    info->pos_host, hstr_addr);
    389           if (res <= 0)
     395          res = pcre2_substring_get_bynumber(match_data, info->pos_host,
     396                                             hstr_addr, &hstr_len);
     397          if (res != 0)
    390398            goto corrupt;
    391399        }
     
    401409
    402410      if (hstr)
    403         record->host = sh_string_new_from_lchar(hstr, strlen(hstr));
     411        record->host = sh_string_new_from_lchar(hstr, hstr_len);
    404412      else
    405413        record->host = sh_string_new_from_lchar(sh.host.name, strlen(sh.host.name));
     
    409417      record->pid       = PID_INVALID;
    410418
    411       pcre_free_substring(hstr);
     419      /* does nothing if hstr == NULL */
     420      pcre2_substring_free((PCRE2_UCHAR8 *)hstr);
     421
     422      pcre2_match_data_free(match_data);
    412423      return record;
    413424    }
     
    415426    {
    416427      char msg[128];
    417       sl_snprintf(msg, sizeof(msg), _("Incorrect number of captured subexpressions: %d vs %d"),
    418                   res, info->line_ovecnum);
     428      sl_snprintf(msg, sizeof(msg), _("Matching error: %d"), res);
    419429     
    420430      SH_MUTEX_LOCK(mutex_thread_nolog);
     
    440450    sh_string_destroy(&msg);
    441451  }
     452  pcre2_match_data_free(match_data);
    442453  return NULL;
    443454}
Note: See TracChangeset for help on using the changeset viewer.