Source
xxxxxxxxxx
static void strncpy_alloc(char **str, size_t *alloc_len, size_t *offset, const char *src, size_t n, size_t limit)
/*
** Copyright (C) 2001-2025 Zabbix SIA
**
** This program is free software: you can redistribute it and/or modify it under the terms of
** the GNU Affero General Public License as published by the Free Software Foundation, version 3.
**
** This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
** without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
** See the GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License along with this program.
** If not, see <https://www.gnu.org/licenses/>.
**/
struct zbx_regexp
{
pcre2_code *pcre2_regexp;
pcre2_match_context *match_ctx;
};
typedef struct
{
int rm_so;
int rm_eo;
}
zbx_regmatch_t;
/* Max number of supported capture groups in regular expressions. */
/* Group \0 contains the matching part of string, groups \1 ...\9 */
/* contain captured groups (substrings). */
ZBX_PTR_VECTOR_IMPL(expression, zbx_expression_t *)
typedef struct
{
zbx_regmatch_t groups[ZBX_REGEXP_GROUPS_MAX];
}
zbx_match_t;
ZBX_PTR_VECTOR_DECL(match, zbx_match_t *)
ZBX_PTR_VECTOR_IMPL(match, zbx_match_t *)
static void zbx_match_free(zbx_match_t *match)
{
zbx_free(match);
}
static char *decode_pcre2_compile_error(int error_code, PCRE2_SIZE error_offset, uint32_t flags)
{
/* 120 code units buffer is recommended in "man pcre2api" */
int ret;
char buf[BUF_SIZE];
if (0 > (ret = pcre2_get_error_message(error_code, (PCRE2_UCHAR *)buf, sizeof(buf))))
return zbx_dsprintf(NULL, "pcre2_get_error_message(%d, ...) failed with error %d", error_code, ret);
return zbx_dsprintf(NULL, "%s, position %zu, flags:0x%x", buf, (size_t)error_offset, (unsigned int)flags);
}
/******************************************************************************
* *
* Purpose: compiles a regular expression *
* *
* Parameters: *
* pattern - [IN] regular expression as a text string. Empty *
* string ("") is allowed, it will match everything. *
* NULL is not allowed. *
* flags - [IN] option bitmask as passed to pcre2_compile *
* regexp - [OUT] compiled regexp. Can be NULL if only regexp *
* compilation is checked, Cleanup in caller. *
* err_msg - [OUT] dynamically allocated error message. Can be NULL to *
* discard the error message. *
* *
* Return value: SUCCEED or FAIL *
* *
******************************************************************************/
static int regexp_compile(const char *pattern, uint32_t flags, zbx_regexp_t **regexp, char **err_msg)
{
pcre2_code *pcre2_regexp;
int error = 0;
PCRE2_SIZE error_offset = 0;