Skip to content

Commit a23e88f

Browse files
authored
Merge pull request #3114 from airween/v3/sonarmemleakfix
fix: Sonarcloud memleak fixes
2 parents a519c65 + 6cffa8f commit a23e88f

File tree

2 files changed

+20
-33
lines changed

2 files changed

+20
-33
lines changed

test/regression/regression.cc

+7-2
Original file line numberDiff line numberDiff line change
@@ -480,9 +480,14 @@ int main(int argc, char **argv) {
480480
ModSecurityTest<RegressionTest> test;
481481

482482
std::string ver(MODSECURITY_VERSION);
483-
std::string envvar("MODSECURITY=ModSecurity " + ver + " regression tests");
483+
std::string envvar("ModSecurity " + ver + " regression tests");
484+
485+
#ifndef WIN32
486+
setenv("MODSECURITY", envvar.c_str(), 1);
487+
#else
488+
_putenv_s("MODSECURITY", envvar.c_str());
489+
#endif
484490

485-
putenv(strdup(envvar.c_str()));
486491
#ifndef NO_LOGS
487492
int test_number = 0;
488493
#endif

tools/rules-check/rules-check.cc

+13-31
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@ void print_help(const char *name) {
3636

3737

3838
int main(int argc, char **argv) {
39-
modsecurity::RulesSet *rules;
39+
auto rules = std::make_unique<modsecurity::RulesSet>();
4040
char **args = argv;
41-
rules = new modsecurity::RulesSet();
4241
int ret = 0;
4342

4443
args++;
@@ -50,41 +49,26 @@ int main(int argc, char **argv) {
5049

5150
while (*args != NULL) {
5251
struct stat buffer;
53-
std::string argFull("");
54-
const char *arg = *args;
52+
std::string arg = *args;
5553
std::string err;
5654
int r;
5755

58-
if (argFull.empty() == false) {
59-
if (arg[strlen(arg)-1] == '\"') {
60-
argFull.append(arg, strlen(arg)-1);
61-
goto next;
62-
} else {
63-
argFull.append(arg);
64-
goto next;
65-
}
66-
}
67-
68-
if (arg[0] == '\"' && argFull.empty() == true) {
69-
if (arg[strlen(arg)-1] == '\"') {
70-
argFull.append(arg+1, strlen(arg) - 2);
71-
} else {
72-
argFull.append(arg+1);
73-
goto next;
74-
}
75-
}
56+
// strip arg from leading and trailing '"' chars
57+
arg.erase(arg.find_last_not_of('\"')+1);
58+
arg.erase(0, arg.find_first_not_of('\"'));
7659

77-
if (argFull.empty() == false) {
78-
arg = strdup(argFull.c_str());
79-
argFull.clear();
60+
if (arg.empty() == true) {
61+
args++;
62+
continue;
8063
}
8164

8265
std::cout << " : " << arg << " -- ";
83-
if (stat(arg, &buffer) == 0) {
84-
r = rules->loadFromUri(arg);
66+
if (stat(arg.c_str(), &buffer) == 0) {
67+
r = rules->loadFromUri(arg.c_str());
8568
} else {
86-
r = rules->load(arg);
69+
r = rules->load(arg.c_str());
8770
}
71+
8872
if (r < 0) {
8973
err.assign(rules->m_parserError.str());
9074
rules->m_parserError.str("");
@@ -95,12 +79,10 @@ int main(int argc, char **argv) {
9579
if (err.empty() == false) {
9680
std::cerr << " " << err << std::endl;
9781
}
98-
next:
82+
9983
args++;
10084
}
10185

102-
delete rules;
103-
10486
if (ret < 0) {
10587
std::cout << "Test failed." << std::endl;
10688
} else {

0 commit comments

Comments
 (0)