Skip to content

Commit f04dcc0

Browse files
authored
Merge pull request #3207 from gberkes/v3/remove_this_throw_call_transaction_h_mk2
V3/remove this throw call transaction h mk2
2 parents b6d218f + b4cb243 commit f04dcc0

File tree

5 files changed

+82
-47
lines changed

5 files changed

+82
-47
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ jobs:
1212
matrix:
1313
os: [ubuntu-22.04]
1414
platform:
15-
- {label: "x64", arch: "amd64", configure: ""}
16-
- {label: "x32", arch: "i386", configure: "PKG_CONFIG_PATH=/usr/lib/i386-linux-gnu/pkgconfig CFLAGS=-m32 CXXFLAGS=-m32 LDFLAGS=-m32"}
15+
- {label: "x64", arch: "amd64", configure: "--enable-assertions=yes"}
16+
- {label: "x32", arch: "i386", configure: "PKG_CONFIG_PATH=/usr/lib/i386-linux-gnu/pkgconfig CFLAGS=-m32 CXXFLAGS=-m32 LDFLAGS=-m32 --enable-assertions=yes"}
1717
compiler:
1818
- {label: "gcc", cc: "gcc", cxx: "g++"}
1919
- {label: "clang", cc: "clang", cxx: "clang++"}
@@ -112,7 +112,7 @@ jobs:
112112
- name: build.sh
113113
run: ./build.sh
114114
- name: configure
115-
run: ./configure ${{ matrix.configure.opt }}
115+
run: ./configure ${{ matrix.configure.opt }} --enable-assertions=yes
116116
- uses: ammaraskar/gcc-problem-matcher@master
117117
- name: make
118118
run: make -j `sysctl -n hw.logicalcpu`

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,16 @@ CFLAGS to disable the compilation optimization parameters:
236236
```shell
237237
$ export CFLAGS="-g -O0"
238238
$ ./build.sh
239-
$ ./configure
239+
$ ./configure --enable-assertions=yes
240240
$ make
241241
$ sudo make install
242242
```
243+
"Assertions allow us to document assumptions and to spot violations early in the
244+
development process. What is more, assertions allow us to spot violations with a
245+
minimum of effort." https://dl.acm.org/doi/pdf/10.1145/240964.240969
246+
247+
It is recommended to use assertions where applicable, and to enable them with
248+
'--enable-assertions=yes' during the testing and debugging workflow.
243249

244250
### Benchmarking
245251

configure.ac

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,17 @@ AC_SUBST([MSC_VERSION])
248248
MSC_GIT_VERSION=msc_version_git
249249
AC_SUBST([MSC_GIT_VERSION])
250250

251+
AC_ARG_ENABLE(assertions,
252+
[AS_HELP_STRING([--enable-assertions],[Turn on assertions feature: undefine NDEBUG])],
253+
254+
[case "${enableval}" in
255+
yes) assertions=true ;;
256+
no) assertions=false ;;
257+
*) AC_MSG_ERROR(bad value ${enableval} for --enable-assertions) ;;
258+
esac],
259+
260+
[assertions=false]
261+
)
251262

252263
AC_ARG_ENABLE(debug-logs,
253264
[AS_HELP_STRING([--disable-debug-logs],[Turn off the SecDebugLog feature])],
@@ -377,6 +388,14 @@ if test "$aflFuzzer" == "true"; then
377388
GLOBAL_CPPFLAGS="$GLOBAL_CPPFLAGS $FUZZ_CPPCFLAGS"
378389
$buildExamples = false
379390
fi
391+
392+
case $assertions in
393+
false) ASSERTIONS_CPPCFLAGS="-DNDEBUG" ;;
394+
true) ASSERTIONS_CPPCFLAGS="-UNDEBUG" ;;
395+
*) AC_MSG_ERROR(bad value ${assertions} for assertions) ;;
396+
esac
397+
GLOBAL_CPPFLAGS="$GLOBAL_CPPFLAGS $ASSERTIONS_CPPCFLAGS"
398+
380399
AC_SUBST(GLOBAL_LDADD)
381400
AC_SUBST(GLOBAL_CPPFLAGS)
382401

@@ -613,6 +632,11 @@ if test $buildTestUtilities = true; then
613632
else
614633
echo " + Test Utilities ....disabled"
615634
fi
635+
if test $assertions = true; then
636+
echo " + Assertions ....enabled"
637+
else
638+
echo " + Assertions ....disabled"
639+
fi
616640
if test $debugLogs = true; then
617641
echo " + SecDebugLog ....enabled"
618642
else

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)