From c06ea4e44caee74486e2931154021a0d0e6c8066 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Wed, 10 Sep 2025 11:08:52 +0200 Subject: check_curl: pre compile regex for string matching --- plugins/check_curl.c | 7 ++++--- plugins/check_curl.d/config.h | 8 ++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/plugins/check_curl.c b/plugins/check_curl.c index f28bcdfc..ba38854a 100644 --- a/plugins/check_curl.c +++ b/plugins/check_curl.c @@ -1261,9 +1261,8 @@ mp_state_enum check_http(const check_curl_config config, check_curl_working_stat } if (strlen(config.regexp)) { - regex_t preg; regmatch_t pmatch[REGS]; - int errcode = regexec(&preg, global_state.body_buf.buf, REGS, pmatch, 0); + int errcode = regexec(&config.compiled_regex, global_state.body_buf.buf, REGS, pmatch, 0); if ((errcode == 0 && !config.invert_regex) || (errcode == REG_NOMATCH && config.invert_regex)) { /* OK - No-op to avoid changing the logic around it */ @@ -1284,7 +1283,7 @@ mp_state_enum check_http(const check_curl_config config, check_curl_working_stat } result = config.state_regex; } else { - regerror(errcode, &preg, errbuf, MAX_INPUT_BUFFER); + regerror(errcode, &config.compiled_regex, errbuf, MAX_INPUT_BUFFER); char tmp[DEFAULT_BUFFER_SIZE]; @@ -1969,6 +1968,8 @@ check_curl_config_wrapper process_arguments(int argc, char **argv) { result.errorcode = ERROR; return result; } + + result.config.compiled_regex = preg; break; case INVERT_REGEX: result.config.invert_regex = true; diff --git a/plugins/check_curl.d/config.h b/plugins/check_curl.d/config.h index 9de68713..9acd4048 100644 --- a/plugins/check_curl.d/config.h +++ b/plugins/check_curl.d/config.h @@ -8,6 +8,7 @@ #include #include #include "curl/curl.h" +#include "regex.h" enum { MAX_RE_SIZE = 1024, @@ -87,7 +88,13 @@ typedef struct { char *cookie_jar_file; int maximum_age; + + // the original regex string from the command line char regexp[MAX_RE_SIZE]; + + // the compiled regex for usage later + regex_t compiled_regex; + mp_state_enum state_regex; bool invert_regex; bool verify_peer_and_host; @@ -136,6 +143,7 @@ check_curl_config check_curl_config_init() { .maximum_age = -1, .regexp = {}, + .compiled_regex = {}, .state_regex = STATE_CRITICAL, .invert_regex = false, .verify_peer_and_host = false, -- cgit v1.2.3-74-g34f1