Conversation
|
I was not sure if there was any restriction not to use const, thats why i asked. I could extend the PR with more positions for "const auto&" if you like. |
Sure, go! If the tests will be passed, I'll be happy :). Btw you could review the tests too, may be you will find some lack there too (for eg. you're checking the code...) |
with changes from e9bf9ad nothing is modified anymore
|
|
Hi @edding3000, sorry for the long delay, it seems like this PR is ready to merge. Just one question: could you merge the current v3/master into this one? |
|
also please take a look at the few request changes. |
|
@airween I merged v3/master. cppcheck fails now. Can you see the reason? |
Yes: I think So what you can try now:
Do you have |
2539f80 to
0f8c668
Compare
|
|
Thx for your hint. I build cppcheck from source, but it did not find this - perhaps I did not use correct settings. Anyway, warning is fixed now. |
There was a problem hiding this comment.
Pull request overview
This PR makes small performance-oriented C++ adjustments across the ModSecurity codebase, primarily by avoiding unnecessary copies in range-based for loops and tightening const-correctness in a few helper APIs.
Changes:
- Convert multiple range-based loops to iterate by
const auto&(or equivalent) to avoid copies. - Adjust a few APIs/usages for const-correctness (e.g.,
RunTimeString::evaluate,RulesExceptions::merge,RuleWithActions::containsTag). - Replace C-style casts with
reinterpret_castfor request/response body buffer pointers in regression tests.
Reviewed changes
Copilot reviewed 15 out of 15 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| test/unit/unit.cc | Avoids copies in iteration and uses auto* for pointer-returning factory calls. |
| test/regression/regression.cc | Minor cleanup + safer casts to const unsigned char* for body buffers. |
| src/variables/xml.cc | Iteration tweaks while registering XML namespaces from actions. |
| src/variables/variable.h | Avoids string copy by passing toOmit argument by const reference. |
| src/transaction.cc | Avoids copies when iterating over rule messages and tags during JSON generation. |
| src/run_time_string.h | Makes evaluate overloads const (and adjusts inline evaluate()). |
| src/run_time_string.cc | Implements RunTimeString::evaluate as const and iterates elements as const. |
| src/rules_exceptions.cc | Iteration by const reference + merge now takes a const pointer. |
| src/rule_with_operator.cc | Iterates variables by const reference to avoid pointer copies. |
| src/rule_with_actions.cc | Iteration tweaks + makes containsTag const. |
| src/engine/lua.cc | Iterates variable-value pointers without copying when building Lua tables. |
| src/collection/backend/in_memory-per_process.cc | Avoids copying map entries during iteration. |
| src/actions/block.cc | Avoids copying default action smart pointers during iteration. |
| headers/modsecurity/rules_exceptions.h | Public header signature updated for merge (const pointer). |
| headers/modsecurity/rule_with_actions.h | Public header signature updated for containsTag (const method). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| @@ -51,7 +51,7 @@ class RulesExceptions { | |||
| bool addRange(int a, int b); | |||
| bool addNumber(int a); | |||
| bool contains(int a); | |||
There was a problem hiding this comment.
Changing RulesExceptions::merge to take a const RulesExceptions* alters the exported C++ symbol in an installed public header, which is an ABI-breaking change for downstream binaries linked against an older libmodsecurity. To keep backward compatibility, preserve the original signature and add a const overload (or keep the old signature and treat the parameter as const internally).
| bool contains(int a); | |
| bool contains(int a); | |
| bool merge(RulesExceptions *from) { | |
| return merge(static_cast<const RulesExceptions *>(from)); | |
| } |
| @@ -80,7 +80,7 @@ class RuleWithActions : public Rule { | |||
|
|
|||
| std::vector<actions::Action *> getActionsByName(const std::string& name, | |||
| const Transaction *t); | |||
There was a problem hiding this comment.
Making RuleWithActions::containsTag const changes the mangled name in an installed public header, which can break ABI for external connectors compiled against a previous version of the library. Consider keeping the existing non-const method for compatibility and adding a const overload (with the non-const delegating to the const implementation).
| const Transaction *t); | |
| const Transaction *t); | |
| bool containsTag(const std::string& name, Transaction *t) | |
| { | |
| return static_cast<const RuleWithActions *>(this)->containsTag(name, t); | |
| } |
| std::string evaluate(Transaction *t) const; | ||
| std::string evaluate(Transaction *t, Rule *r) const; | ||
| std::string evaluate() const{ | ||
| return evaluate(NULL); | ||
| } |
There was a problem hiding this comment.
Minor formatting/style: evaluate() const{ is missing a space before {, and this still uses NULL in C++ code. Prefer consistent spacing (const {) and consider nullptr to avoid integral null pointer constants.
airween
left a comment
There was a problem hiding this comment.
@edding3000 thanks, please take a look at Copilot reviews and my suggestions below.
| @@ -51,7 +51,7 @@ class RulesExceptions { | |||
| bool addRange(int a, int b); | |||
| bool addNumber(int a); | |||
| bool contains(int a); | |||
| std::string evaluate(Transaction *t) const; | ||
| std::string evaluate(Transaction *t, Rule *r) const; | ||
| std::string evaluate() const{ | ||
| return evaluate(NULL); | ||
| } |
| @@ -80,7 +80,7 @@ class RuleWithActions : public Rule { | |||
|
|
|||
| std::vector<actions::Action *> getActionsByName(const std::string& name, | |||
| const Transaction *t); | |||



what
Range based for loops with references are already used in this project, but in a few places not.
I am not sure why. I changed this code locations and executed unit tests.
Also use '*' for pointers when using auto keyword, makes it more readable.
why
Improve performance a little bit.
questions
"const auto&" could also be used in many places. Is there a reason why it is not used?