From ca7c0ae0b94b0daad932aef25fa0fc0e99ef64dd Mon Sep 17 00:00:00 2001 From: Ervin Hegedus Date: Wed, 27 Mar 2024 16:19:38 +0100 Subject: [PATCH] Code refactorization --- tools/rules-check/rules-check.cc | 50 +++++++++----------------------- 1 file changed, 13 insertions(+), 37 deletions(-) diff --git a/tools/rules-check/rules-check.cc b/tools/rules-check/rules-check.cc index b1b974a4db..9061f9f117 100644 --- a/tools/rules-check/rules-check.cc +++ b/tools/rules-check/rules-check.cc @@ -32,61 +32,39 @@ void print_help(const char *name) { int main(int argc, char **argv) { - modsecurity::RulesSet *rules; + std::unique_ptr rules (new modsecurity::RulesSet()); char **args = argv; - rules = new modsecurity::RulesSet(); int ret = 0; args++; if (*args == NULL) { print_help(argv[0]); - delete rules; return 0; } while (*args != NULL) { struct stat buffer; - std::string argFull(""); - const char *arg = *args; + std::string arg = (*args); std::string err; int r; - bool need_free = false; - - if (argFull.empty() == false) { - if (arg[strlen(arg)-1] == '\"') { - argFull.append(arg, strlen(arg)-1); - goto next; - } else { - argFull.append(arg); - goto next; - } - } - if (arg[0] == '\"' && argFull.empty() == true) { - if (arg[strlen(arg)-1] == '\"') { - argFull.append(arg+1, strlen(arg) - 2); - } else { - argFull.append(arg+1); - goto next; - } - } + // strip arg from leading and trailing '"' chars + arg.erase(arg.find_last_not_of('\"')+1); + arg.erase(0, arg.find_first_not_of('\"')); - if (argFull.empty() == false) { - arg = strdup(argFull.c_str()); - need_free = true; - argFull.clear(); + if (arg.empty() == true) { + args++; + continue; } std::cout << " : " << arg << " -- "; - if (stat(arg, &buffer) == 0) { - r = rules->loadFromUri(arg); + if (stat(arg.c_str(), &buffer) == 0) { + r = rules->loadFromUri(arg.c_str()); } else { - r = rules->load(arg); - } - if(need_free == true && arg != nullptr) { - free((void*)arg); + r = rules->load(arg.c_str()); } + if (r < 0) { err.assign(rules->m_parserError.str()); rules->m_parserError.str(""); @@ -97,12 +75,10 @@ int main(int argc, char **argv) { if (err.empty() == false) { std::cerr << " " << err << std::endl; } -next: + args++; } - delete rules; - if (ret < 0) { std::cout << "Test failed." << std::endl; } else {