Merge pull request #3004 from marcstern/v2/mst/optim4

Optimization: Avoid last loop and storing an empty value in case nothing after last %{..} macro
This commit is contained in:
Marc Stern
2024-02-01 12:01:58 +01:00
committed by GitHub

View File

@@ -228,18 +228,20 @@ int expand_macros(modsec_rec *msr, msc_string *var, msre_rule *rule, apr_pool_t
msre_var *var_resolved = NULL; msre_var *var_resolved = NULL;
/* Add the text part before the macro to the array. */ /* Add the text part before the macro to the array. */
if (p != text_start) {
part = (msc_string *)apr_pcalloc(mptmp, sizeof(msc_string)); part = (msc_string *)apr_pcalloc(mptmp, sizeof(msc_string));
if (part == NULL) return -1; if (part == NULL) return -1;
part->value_len = p - text_start; part->value_len = p - text_start;
part->value = apr_pstrmemdup(mptmp, text_start, part->value_len); part->value = apr_pstrmemdup(mptmp, text_start, part->value_len);
*(msc_string **)apr_array_push(arr) = part; *(msc_string **)apr_array_push(arr) = part;
}
/* Resolve the macro and add that to the array. */ /* Resolve the macro and add that to the array. */
var_resolved = msre_create_var_ex(mptmp, msr->modsecurity->msre, var_name, var_value, var_resolved = msre_create_var_ex(mptmp, msr->modsecurity->msre, var_name, var_value,
msr, &my_error_msg); msr, &my_error_msg);
if (var_resolved != NULL) { if (var_resolved != NULL) {
var_generated = generate_single_var(msr, var_resolved, NULL, rule, mptmp); var_generated = generate_single_var(msr, var_resolved, NULL, rule, mptmp);
if (var_generated != NULL) { if (var_generated != NULL && var_generated->value_len) {
part = (msc_string *)apr_pcalloc(mptmp, sizeof(msc_string)); part = (msc_string *)apr_pcalloc(mptmp, sizeof(msc_string));
if (part == NULL) return -1; if (part == NULL) return -1;
part->value_len = var_generated->value_len; part->value_len = var_generated->value_len;
@@ -280,13 +282,11 @@ int expand_macros(modsec_rec *msr, msc_string *var, msre_rule *rule, apr_pool_t
part->value_len = strlen(part->value); part->value_len = strlen(part->value);
*(msc_string **)apr_array_push(arr) = part; *(msc_string **)apr_array_push(arr) = part;
} }
} while (p != NULL); } while (p != NULL && *next_text_start);
/* If there's more than one member of the array that /* Combine text parts into a single string now.
* means there was at least one macro present. Combine * If no macro was present, we already returned
* text parts into a single string now.
*/ */
if (arr->nelts > 1) {
/* Figure out the required size for the string. */ /* Figure out the required size for the string. */
var->value_len = 0; var->value_len = 0;
for(i = 0; i < arr->nelts; i++) { for(i = 0; i < arr->nelts; i++) {
@@ -306,7 +306,6 @@ int expand_macros(modsec_rec *msr, msc_string *var, msre_rule *rule, apr_pool_t
offset += part->value_len; offset += part->value_len;
} }
var->value[offset] = '\0'; var->value[offset] = '\0';
}
return 1; return 1;
} }