Refactor config_get_compiled_regex_capcount()
The function now returns the compiled code or null or error. Also, instead of capcount, it writes the ovector size. The ovector pointer can be null. Removed the other config_get_compiled_regex()-function and replaced it with the refactored function since they are almost identical.
This commit is contained in:
@ -355,32 +355,19 @@ int config_get_server_list(const MXS_CONFIG_PARAMETER *params, const char *key,
|
|||||||
struct server*** output);
|
struct server*** output);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a compiled regular expression. The returned @c pcre2_code should be freed
|
* Get a compiled regular expression and the ovector size of the pattern. The
|
||||||
* by the caller.
|
* return value should be freed by the caller.
|
||||||
*
|
*
|
||||||
* @param params List of configuration parameters
|
* @param params List of configuration parameters
|
||||||
* @param key Parameter name
|
* @param key Parameter name
|
||||||
* @param options PCRE2 compilation options
|
* @param options PCRE2 compilation options
|
||||||
* @return The compiled PCRE2 code, or NULL on error
|
* @param output_ovec_size Output for match data ovector size. On error,
|
||||||
|
* nothing is written. If NULL, the parameter is ignored.
|
||||||
|
* @return Compiled regex code on success, NULL otherwise
|
||||||
*/
|
*/
|
||||||
pcre2_code* config_get_compiled_regex(const MXS_CONFIG_PARAMETER *params, const char *key,
|
pcre2_code* config_get_compiled_regex(const MXS_CONFIG_PARAMETER *params,
|
||||||
uint32_t options);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a compiled regular expression and the capture count of the pattern. The
|
|
||||||
* @c pcre2_code should be freed by the caller.
|
|
||||||
*
|
|
||||||
* @param params List of configuration parameters
|
|
||||||
* @param key Parameter name
|
|
||||||
* @param options PCRE2 compilation options
|
|
||||||
* @param output_code Output for compilation result
|
|
||||||
* @param output_capcount Output for capture count
|
|
||||||
* @return True on success, false otherwise
|
|
||||||
*/
|
|
||||||
bool config_get_compiled_regex_capcount(const MXS_CONFIG_PARAMETER *params,
|
|
||||||
const char *key, uint32_t options,
|
const char *key, uint32_t options,
|
||||||
pcre2_code** output_code,
|
uint32_t* output_ovec_size);
|
||||||
uint32_t* output_capcount);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse a list of server names and write the results in an array of strings
|
* Parse a list of server names and write the results in an array of strings
|
||||||
|
@ -165,8 +165,8 @@ static int maxscale_getline(char** dest, int* size, FILE* file);
|
|||||||
static bool check_first_last_char(const char* string, char expected);
|
static bool check_first_last_char(const char* string, char expected);
|
||||||
static void remove_first_last_char(char* value);
|
static void remove_first_last_char(char* value);
|
||||||
static bool test_regex_string_validity(const char* regex_string, const char* key);
|
static bool test_regex_string_validity(const char* regex_string, const char* key);
|
||||||
static bool compile_regex_string(const char* regex_string, bool jit_enabled, uint32_t options,
|
static pcre2_code* compile_regex_string(const char* regex_string, bool jit_enabled,
|
||||||
pcre2_code** output_code, uint32_t* output_capcount);
|
uint32_t options, uint32_t* output_ovector_size);
|
||||||
|
|
||||||
int config_get_ifaddr(unsigned char *output);
|
int config_get_ifaddr(unsigned char *output);
|
||||||
static int config_get_release_string(char* release);
|
static int config_get_release_string(char* release);
|
||||||
@ -1240,24 +1240,13 @@ char* config_copy_string(const MXS_CONFIG_PARAMETER *params, const char *key)
|
|||||||
}
|
}
|
||||||
|
|
||||||
pcre2_code* config_get_compiled_regex(const MXS_CONFIG_PARAMETER *params,
|
pcre2_code* config_get_compiled_regex(const MXS_CONFIG_PARAMETER *params,
|
||||||
const char *key, uint32_t options)
|
|
||||||
{
|
|
||||||
pcre2_code* code = NULL;
|
|
||||||
uint32_t capcount = 0;
|
|
||||||
config_get_compiled_regex_capcount(params, key, options, &code, &capcount);
|
|
||||||
return code;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool config_get_compiled_regex_capcount(const MXS_CONFIG_PARAMETER *params,
|
|
||||||
const char *key, uint32_t options,
|
const char *key, uint32_t options,
|
||||||
pcre2_code** output_code,
|
uint32_t* output_ovec_size)
|
||||||
uint32_t* output_capcount)
|
|
||||||
{
|
{
|
||||||
const char* regex_string = config_get_string(params, key);
|
const char* regex_string = config_get_string(params, key);
|
||||||
uint32_t jit_available = 0;
|
uint32_t jit_available = 0;
|
||||||
pcre2_config(PCRE2_CONFIG_JIT, &jit_available);
|
pcre2_config(PCRE2_CONFIG_JIT, &jit_available);
|
||||||
return compile_regex_string(regex_string, jit_available, options,
|
return compile_regex_string(regex_string, jit_available, options, output_ovec_size);
|
||||||
output_code, output_capcount);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
MXS_CONFIG_PARAMETER* config_clone_param(const MXS_CONFIG_PARAMETER* param)
|
MXS_CONFIG_PARAMETER* config_clone_param(const MXS_CONFIG_PARAMETER* param)
|
||||||
@ -4075,20 +4064,20 @@ static void remove_first_last_char(char* value)
|
|||||||
* Compile a regex string using PCRE2 using the settings provided.
|
* Compile a regex string using PCRE2 using the settings provided.
|
||||||
*
|
*
|
||||||
* @param regex_string The string to compile
|
* @param regex_string The string to compile
|
||||||
* @param jit_enabled Enable JIT compilation. If not available, a notice is printed.
|
* @param jit_enabled Enable JIT compilation. If true but JIT is not available,
|
||||||
|
* a warning is printed.
|
||||||
* @param options PCRE2 compilation options
|
* @param options PCRE2 compilation options
|
||||||
* @param output_code Output for the regex machine code
|
* @param output_ovector_size Output for the match data ovector size. On error,
|
||||||
* @param output_capcount Output for the capture count of the regex. Add one to
|
* nothing is written. If NULL, the parameter is ignored.
|
||||||
* get the optimal ovector size.
|
* @return Compiled regex code on success, NULL otherwise
|
||||||
* @return True on success. On error, nothing is written to the outputs.
|
|
||||||
*/
|
*/
|
||||||
static bool compile_regex_string(const char* regex_string, bool jit_enabled,
|
static pcre2_code* compile_regex_string(const char* regex_string, bool jit_enabled,
|
||||||
uint32_t options, pcre2_code** output_code,
|
uint32_t options, uint32_t* output_ovector_size)
|
||||||
uint32_t* output_capcount)
|
|
||||||
{
|
{
|
||||||
bool success = true;
|
bool success = true;
|
||||||
int errorcode = -1;
|
int errorcode = -1;
|
||||||
PCRE2_SIZE error_offset = -1;
|
PCRE2_SIZE error_offset = -1;
|
||||||
|
uint32_t capcount = 0;
|
||||||
pcre2_code* machine =
|
pcre2_code* machine =
|
||||||
pcre2_compile((PCRE2_SPTR) regex_string, PCRE2_ZERO_TERMINATED, options,
|
pcre2_compile((PCRE2_SPTR) regex_string, PCRE2_ZERO_TERMINATED, options,
|
||||||
&errorcode, &error_offset, NULL);
|
&errorcode, &error_offset, NULL);
|
||||||
@ -4102,22 +4091,14 @@ static bool compile_regex_string(const char* regex_string, bool jit_enabled,
|
|||||||
MXS_WARNING("PCRE2 JIT compilation of pattern '%s' failed, "
|
MXS_WARNING("PCRE2 JIT compilation of pattern '%s' failed, "
|
||||||
"falling back to normal compilation.", regex_string);
|
"falling back to normal compilation.", regex_string);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
/* Check what is the required match_data size for this pattern.
|
/* Check what is the required match_data size for this pattern. */
|
||||||
*/
|
|
||||||
uint32_t capcount = 0;
|
|
||||||
int ret_info = pcre2_pattern_info(machine, PCRE2_INFO_CAPTURECOUNT, &capcount);
|
int ret_info = pcre2_pattern_info(machine, PCRE2_INFO_CAPTURECOUNT, &capcount);
|
||||||
if (ret_info != 0)
|
if (ret_info != 0)
|
||||||
{
|
{
|
||||||
MXS_PCRE2_PRINT_ERROR(ret_info);
|
MXS_PCRE2_PRINT_ERROR(ret_info);
|
||||||
success = false;
|
success = false;
|
||||||
}
|
}
|
||||||
if (success)
|
|
||||||
{
|
|
||||||
*output_code = machine;
|
|
||||||
*output_capcount = capcount;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -4127,11 +4108,16 @@ static bool compile_regex_string(const char* regex_string, bool jit_enabled,
|
|||||||
success = false;
|
success = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!success && machine)
|
if (!success)
|
||||||
{
|
{
|
||||||
pcre2_code_free(machine);
|
pcre2_code_free(machine);
|
||||||
|
machine = NULL;
|
||||||
}
|
}
|
||||||
return success;
|
else if (output_ovector_size)
|
||||||
|
{
|
||||||
|
*output_ovector_size = capcount + 1;
|
||||||
|
}
|
||||||
|
return machine;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -4161,12 +4147,8 @@ static bool test_regex_string_validity(const char* regex_string, const char* key
|
|||||||
remove_first_last_char(regex_copy);
|
remove_first_last_char(regex_copy);
|
||||||
}
|
}
|
||||||
|
|
||||||
pcre2_code* code;
|
pcre2_code* code = compile_regex_string(regex_copy, false, 0, NULL);
|
||||||
uint32_t capcount;
|
bool rval = (code != NULL);
|
||||||
if (compile_regex_string(regex_copy, false, 0, &code, &capcount))
|
|
||||||
{
|
|
||||||
pcre2_code_free(code);
|
pcre2_code_free(code);
|
||||||
return true;
|
return rval;
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user