Skip to content

Commit b465995

Browse files
author
gberkes
committed
Refactor: Ensure safe error handling by removing isolated throw; statements.
- SonarCloud analysis identified standalone `throw;` calls without accompanying `try-catch` blocks, used inconsistently as placeholders or for premature termination under specific conditions. - Removed these `throw;` instances to prevent potential runtime issues in future development phases, where such configurations might inadvertently be created. - Introduced `assert` statements as a more appropriate mechanism for asserting preconditions in the affected class member functions, ensuring clearer intent and safer code behavior during development. - Refactor action_kind processing to use switch() instead of if-else chains; add assertion in default case. - Fix SonarCloud issue: Make this variable a const reference. https://sonarcloud.io/project/issues?resolved=false&pullRequest=3104&id=owasp-modsecurity_ModSecurity&open=AY8Vpgy4f6U6E7VKL4Cn
1 parent b6d218f commit b465995

File tree

2 files changed

+48
-43
lines changed

2 files changed

+48
-43
lines changed

headers/modsecurity/transaction.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515

1616
#ifdef __cplusplus
17+
#include <cassert>
1718
#include <ctime>
1819
#include <fstream>
1920
#include <iomanip>
@@ -307,11 +308,8 @@ class TransactionSecMarkerManagement {
307308
}
308309

309310
std::shared_ptr<std::string> getCurrentMarker() const {
310-
if (m_marker) {
311-
return m_marker;
312-
} else {
313-
throw; // cppcheck-suppress rethrowNoCurrentException
314-
}
311+
assert((m_marker != nullptr) && "You might have forgotten to call and evaluate isInsideAMarker() before calling getCurrentMarker().");
312+
return m_marker;
315313
}
316314

317315
void removeMarker() {

src/rule_with_actions.cc

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include <stdio.h>
1919

20+
#include <cassert>
2021
#include <algorithm>
2122
#include <iostream>
2223
#include <string>
@@ -86,45 +87,51 @@ RuleWithActions::RuleWithActions(
8687

8788
if (actions) {
8889
for (Action *a : *actions) {
89-
if (a->action_kind == Action::ConfigurationKind) {
90-
a->evaluate(this, NULL);
91-
delete a;
92-
93-
} else if (a->action_kind == Action::RunTimeOnlyIfMatchKind) {
94-
if (dynamic_cast<actions::Capture *>(a)) {
95-
m_containsCaptureAction = true;
96-
delete a;
97-
} else if (dynamic_cast<actions::MultiMatch *>(a)) {
98-
m_containsMultiMatchAction = true;
90+
switch (a->action_kind) {
91+
case Action::ConfigurationKind:
92+
a->evaluate(this, NULL);
9993
delete a;
100-
} else if (dynamic_cast<actions::Severity *>(a)) {
101-
m_severity = dynamic_cast<actions::Severity *>(a);
102-
} else if (dynamic_cast<actions::LogData *>(a)) {
103-
m_logData = dynamic_cast<actions::LogData*>(a);
104-
} else if (dynamic_cast<actions::Msg *>(a)) {
105-
m_msg = dynamic_cast<actions::Msg*>(a);
106-
} else if (dynamic_cast<actions::SetVar *>(a)) {
107-
m_actionsSetVar.push_back(
108-
dynamic_cast<actions::SetVar *>(a));
109-
} else if (dynamic_cast<actions::Tag *>(a)) {
110-
m_actionsTag.push_back(dynamic_cast<actions::Tag *>(a));
111-
} else if (dynamic_cast<actions::Block *>(a)) {
112-
m_actionsRuntimePos.push_back(a);
113-
m_containsStaticBlockAction = true;
114-
} else if (a->isDisruptive() == true) {
115-
if (m_disruptiveAction != nullptr) {
116-
delete m_disruptiveAction;
117-
m_disruptiveAction = nullptr;
94+
break;
95+
case Action::RunTimeOnlyIfMatchKind:
96+
if (dynamic_cast<actions::Capture *>(a)) {
97+
m_containsCaptureAction = true;
98+
delete a;
99+
} else if (dynamic_cast<actions::MultiMatch *>(a)) {
100+
m_containsMultiMatchAction = true;
101+
delete a;
102+
} else if (dynamic_cast<actions::Severity *>(a)) {
103+
m_severity = dynamic_cast<actions::Severity *>(a);
104+
} else if (dynamic_cast<actions::LogData *>(a)) {
105+
m_logData = dynamic_cast<actions::LogData*>(a);
106+
} else if (dynamic_cast<actions::Msg *>(a)) {
107+
m_msg = dynamic_cast<actions::Msg*>(a);
108+
} else if (dynamic_cast<actions::SetVar *>(a)) {
109+
m_actionsSetVar.push_back(
110+
dynamic_cast<actions::SetVar *>(a));
111+
} else if (dynamic_cast<actions::Tag *>(a)) {
112+
m_actionsTag.push_back(dynamic_cast<actions::Tag *>(a));
113+
} else if (dynamic_cast<actions::Block *>(a)) {
114+
m_actionsRuntimePos.push_back(a);
115+
m_containsStaticBlockAction = true;
116+
} else if (a->isDisruptive() == true) {
117+
if (m_disruptiveAction != nullptr) {
118+
delete m_disruptiveAction;
119+
m_disruptiveAction = nullptr;
120+
}
121+
m_disruptiveAction = a;
122+
} else {
123+
m_actionsRuntimePos.push_back(a);
118124
}
119-
m_disruptiveAction = a;
120-
} else {
121-
m_actionsRuntimePos.push_back(a);
122-
}
123-
} else {
124-
delete a;
125-
std::cout << "General failure, action: " << a->m_name;
126-
std::cout << " has an unknown type." << std::endl;
127-
throw; // cppcheck-suppress rethrowNoCurrentException
125+
break;
126+
default:
127+
std::cout << "General failure, action: " << a->m_name;
128+
std::cout << " has an unknown type." << std::endl;
129+
delete a;
130+
#ifdef NDEBUG
131+
break;
132+
#else
133+
assert(false);
134+
#endif
128135
}
129136
}
130137
delete actions;
@@ -239,7 +246,7 @@ void RuleWithActions::executeActionsAfterFullMatch(Transaction *trans,
239246
bool containsBlock, std::shared_ptr<RuleMessage> ruleMessage) {
240247
bool disruptiveAlreadyExecuted = false;
241248

242-
for (auto &a : trans->m_rules->m_defaultActions[getPhase()]) { // cppcheck-suppress ctunullpointer
249+
for (const auto &a : trans->m_rules->m_defaultActions[getPhase()]) { // cppcheck-suppress ctunullpointer
243250
if (a.get()->action_kind != actions::Action::RunTimeOnlyIfMatchKind) {
244251
continue;
245252
}

0 commit comments

Comments
 (0)