Skip to content

Commit 374608f

Browse files
author
Marc Stern
authored
Merge pull request #3004 from marcstern/v2/mst/optim4
Optimization: Avoid last loop and storing an empty value in case nothing after last %{..} macro
2 parents 8556d33 + 029fded commit 374608f

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

apache2/re_actions.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -228,18 +228,20 @@ int expand_macros(modsec_rec *msr, msc_string *var, msre_rule *rule, apr_pool_t
228228
msre_var *var_resolved = NULL;
229229

230230
/* Add the text part before the macro to the array. */
231+
if (p != text_start) {
231232
part = (msc_string *)apr_pcalloc(mptmp, sizeof(msc_string));
232233
if (part == NULL) return -1;
233234
part->value_len = p - text_start;
234235
part->value = apr_pstrmemdup(mptmp, text_start, part->value_len);
235236
*(msc_string **)apr_array_push(arr) = part;
237+
}
236238

237239
/* Resolve the macro and add that to the array. */
238240
var_resolved = msre_create_var_ex(mptmp, msr->modsecurity->msre, var_name, var_value,
239241
msr, &my_error_msg);
240242
if (var_resolved != NULL) {
241243
var_generated = generate_single_var(msr, var_resolved, NULL, rule, mptmp);
242-
if (var_generated != NULL) {
244+
if (var_generated != NULL && var_generated->value_len) {
243245
part = (msc_string *)apr_pcalloc(mptmp, sizeof(msc_string));
244246
if (part == NULL) return -1;
245247
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
280282
part->value_len = strlen(part->value);
281283
*(msc_string **)apr_array_push(arr) = part;
282284
}
283-
} while (p != NULL);
285+
} while (p != NULL && *next_text_start);
284286

285-
/* If there's more than one member of the array that
286-
* means there was at least one macro present. Combine
287-
* text parts into a single string now.
287+
/* Combine text parts into a single string now.
288+
* If no macro was present, we already returned
288289
*/
289-
if (arr->nelts > 1) {
290290
/* Figure out the required size for the string. */
291291
var->value_len = 0;
292292
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
306306
offset += part->value_len;
307307
}
308308
var->value[offset] = '\0';
309-
}
310309

311310
return 1;
312311
}

0 commit comments

Comments
 (0)