Skip to content

Use local memory pool inside update_rule_target_ex() to reduce memory footprint #2177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 19 additions & 34 deletions apache2/re.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,18 +245,24 @@ char *update_rule_target_ex(modsec_rec *msr, msre_ruleset *ruleset, msre_rule *r
char *opt = NULL, *param = NULL;
char *target_list = NULL, *replace = NULL;
int i, rc, match = 0, var_appended = 0;
apr_pool_t *local_pool = NULL;

if(rule != NULL) {
apr_status_t status = apr_pool_create(&local_pool, NULL);
if (status < 0) {
return apr_psprintf(ruleset->mp, "Error creating memory pool: %d", status);
}

target_list = strdup(p2);
if(target_list == NULL)
target_list = apr_pstrdup(local_pool, p2);
if(target_list == NULL) {
apr_pool_destroy(local_pool);
return apr_psprintf(ruleset->mp, "Error to update target - memory allocation");;
}

if(p3 != NULL) {
replace = strdup(p3);
replace = apr_pstrdup(local_pool, p3);
if(replace == NULL) {
free(target_list);
target_list = NULL;
apr_pool_destroy(local_pool);
return apr_psprintf(ruleset->mp, "Error to update target - memory allocation");;
}
}
Expand Down Expand Up @@ -288,10 +294,7 @@ char *update_rule_target_ex(modsec_rec *msr, msre_ruleset *ruleset, msre_rule *r
}

if(apr_table_get(ruleset->engine->variables, name) == NULL) {
if(target_list != NULL)
free(target_list);
if(replace != NULL)
free(replace);
apr_pool_destroy(local_pool);
if(msr) {
msr_log(msr, 9, "Error to update target - [%s] is not valid target", name);
}
Expand Down Expand Up @@ -380,10 +383,11 @@ char *update_rule_target_ex(modsec_rec *msr, msre_ruleset *ruleset, msre_rule *r
goto end;
}
} else {

target = strdup(p);
if(target == NULL)
target = apr_pstrdup(local_pool, p);
if(target == NULL) {
apr_pool_destroy(local_pool);
return NULL;
}

is_negated = is_counting = 0;
param = name = value = NULL;
Expand Down Expand Up @@ -413,10 +417,7 @@ char *update_rule_target_ex(modsec_rec *msr, msre_ruleset *ruleset, msre_rule *r
}

if(apr_table_get(ruleset->engine->variables, name) == NULL) {
if(target_list != NULL)
free(target_list);
if(replace != NULL)
free(replace);
apr_pool_destroy(local_pool);
if(msr) {
msr_log(msr, 9, "Error to update target - [%s] is not valid target", name);
}
Expand Down Expand Up @@ -458,11 +459,6 @@ char *update_rule_target_ex(modsec_rec *msr, msre_ruleset *ruleset, msre_rule *r
}
}

if(target != NULL) {
free(target);
target = NULL;
}

if(match == 0 ) {
rc = msre_parse_targets(ruleset, p, rule->targets, &my_error_msg);
if (rc < 0) {
Expand Down Expand Up @@ -493,7 +489,7 @@ char *update_rule_target_ex(modsec_rec *msr, msre_ruleset *ruleset, msre_rule *r
}

if(var_appended == 1) {
current_targets = msre_generate_target_string(ruleset->mp, rule);
current_targets = msre_generate_target_string(local_pool, rule);
rule->unparsed = msre_rule_generate_unparsed(ruleset->mp, rule, current_targets, NULL, NULL);
rule->p1 = apr_pstrdup(ruleset->mp, current_targets);
if(msr) {
Expand All @@ -508,18 +504,7 @@ char *update_rule_target_ex(modsec_rec *msr, msre_ruleset *ruleset, msre_rule *r
}

end:
if(target_list != NULL) {
free(target_list);
target_list = NULL;
}
if(replace != NULL) {
free(replace);
replace = NULL;
}
if(target != NULL) {
free(target);
target = NULL;
}
apr_pool_destroy(local_pool);
return NULL;
}

Expand Down