From d68aef320c66d7f065aadbb0af301a60709aeb83 Mon Sep 17 00:00:00 2001 From: Gabor Berkes Date: Thu, 20 Feb 2025 12:25:53 +0000 Subject: [PATCH] refactor: improve maintainability for SonarCloud compliance - Marked the conversion operator in `Pcre2MatchContextPtr` as `explicit` to improve type safety and prevent unintended implicit conversions. - Ensured consistent use of `nullptr` instead of `NULL` for better readability and modern C++ compliance. These changes enhance code clarity, maintainability, and adherence to modern C++ best practices. --- src/operators/verify_cc.cc | 32 +++++++++++----------- src/operators/verify_cc.h | 6 ++--- src/utils/regex.cc | 54 +++++++++++++++++++------------------- src/utils/regex.h | 6 ++--- 4 files changed, 49 insertions(+), 49 deletions(-) diff --git a/src/operators/verify_cc.cc b/src/operators/verify_cc.cc index 4f197783..1ddbf4f4 100644 --- a/src/operators/verify_cc.cc +++ b/src/operators/verify_cc.cc @@ -25,7 +25,7 @@ #if PCRE_HAVE_JIT #define pcre_study_opt PCRE_STUDY_JIT_COMPILE #else -#define pcre_study_opt 0 +constexpr int pcre_study_opt = 0; #endif #endif @@ -37,17 +37,17 @@ VerifyCC::~VerifyCC() { #ifndef WITH_PCRE pcre2_code_free(m_pc); #else - if (m_pc != NULL) { + if (m_pc != nullptr) { pcre_free(m_pc); - m_pc = NULL; + m_pc = nullptr; } - if (m_pce != NULL) { + if (m_pce != nullptr) { #if PCRE_HAVE_JIT pcre_free_study(m_pce); #else pcre_free(m_pce); #endif - m_pce = NULL; + m_pce = nullptr; } #endif } @@ -100,27 +100,27 @@ bool VerifyCC::init(const std::string ¶m2, std::string *error) { int errornumber = 0; PCRE2_SIZE erroroffset = 0; m_pc = pcre2_compile(pcre2_pattern, PCRE2_ZERO_TERMINATED, - pcre2_options, &errornumber, &erroroffset, NULL); - if (m_pc == NULL) { + pcre2_options, &errornumber, &erroroffset, nullptr); + if (m_pc == nullptr) { return false; } m_pcje = pcre2_jit_compile(m_pc, PCRE2_JIT_COMPLETE); #else - const char *errptr = NULL; + const char *errptr = nullptr; int erroffset = 0; m_pc = pcre_compile(m_param.c_str(), PCRE_DOTALL|PCRE_MULTILINE, - &errptr, &erroffset, NULL); - if (m_pc == NULL) { + &errptr, &erroffset, nullptr); + if (m_pc == nullptr) { error->assign(errptr); return false; } m_pce = pcre_study(m_pc, pcre_study_opt, &errptr); - if (m_pce == NULL) { - if (errptr == NULL) { + if (m_pce == nullptr) { + if (errptr == nullptr) { /* - * Per pcre_study(3) m_pce == NULL && errptr == NULL means + * Per pcre_study(3) m_pce == nullptr && errptr == nullptr means * that no addional information is found, so no need to study */ return true; @@ -140,17 +140,17 @@ bool VerifyCC::evaluate(Transaction *t, RuleWithActions *rule, PCRE2_SIZE offset = 0; size_t target_length = i.length(); PCRE2_SPTR pcre2_i = reinterpret_cast(i.c_str()); - pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, NULL); + pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, nullptr); int ret; for (offset = 0; offset < target_length; offset++) { if (m_pcje == 0) { - ret = pcre2_jit_match(m_pc, pcre2_i, target_length, offset, 0, match_data, NULL); + ret = pcre2_jit_match(m_pc, pcre2_i, target_length, offset, 0, match_data, nullptr); } if (m_pcje != 0 || ret == PCRE2_ERROR_JIT_STACKLIMIT) { - ret = pcre2_match(m_pc, pcre2_i, target_length, offset, PCRE2_NO_JIT, match_data, NULL); + ret = pcre2_match(m_pc, pcre2_i, target_length, offset, PCRE2_NO_JIT, match_data, nullptr); } /* If there was no match, then we are done. */ diff --git a/src/operators/verify_cc.h b/src/operators/verify_cc.h index ec1661fb..7e7e158b 100644 --- a/src/operators/verify_cc.h +++ b/src/operators/verify_cc.h @@ -39,11 +39,11 @@ class VerifyCC : public Operator { explicit VerifyCC(std::unique_ptr param) : Operator("VerifyCC", std::move(param)), #ifndef WITH_PCRE - m_pc(NULL), + m_pc(nullptr), m_pcje(PCRE2_ERROR_JIT_BADOPTION) { } #else - m_pc(NULL), - m_pce(NULL) { } + m_pc(nullptr), + m_pce(nullptr) { } #endif ~VerifyCC() override; diff --git a/src/utils/regex.cc b/src/utils/regex.cc index 9d84c871..30025037 100644 --- a/src/utils/regex.cc +++ b/src/utils/regex.cc @@ -39,7 +39,7 @@ class Pcre2MatchContextPtr { public: Pcre2MatchContextPtr() - : m_match_context(pcre2_match_context_create(NULL)) {} + : m_match_context(pcre2_match_context_create(nullptr)) {} Pcre2MatchContextPtr(const Pcre2MatchContextPtr&) = delete; Pcre2MatchContextPtr& operator=(const Pcre2MatchContextPtr&) = delete; @@ -48,7 +48,7 @@ class Pcre2MatchContextPtr { pcre2_match_context_free(m_match_context); } - operator pcre2_match_context*() const { + explicit operator pcre2_match_context*() const { return m_match_context; } @@ -98,10 +98,10 @@ Regex::Regex(const std::string& pattern_, bool ignoreCase) int errornumber = 0; PCRE2_SIZE erroroffset = 0; m_pc = pcre2_compile(pcre2_pattern, PCRE2_ZERO_TERMINATED, - pcre2_options, &errornumber, &erroroffset, NULL); + pcre2_options, &errornumber, &erroroffset, nullptr); m_pcje = pcre2_jit_compile(m_pc, PCRE2_JIT_COMPLETE); #else - const char *errptr = NULL; + const char *errptr = nullptr; int erroffset; int flags = (PCRE_DOTALL|PCRE_MULTILINE); @@ -109,7 +109,7 @@ Regex::Regex(const std::string& pattern_, bool ignoreCase) flags |= PCRE_CASELESS; } m_pc = pcre_compile(pattern.c_str(), flags, - &errptr, &erroffset, NULL); + &errptr, &erroffset, nullptr); m_pce = pcre_study(m_pc, pcre_study_opt, &errptr); #endif @@ -120,17 +120,17 @@ Regex::~Regex() { #ifndef WITH_PCRE pcre2_code_free(m_pc); #else - if (m_pc != NULL) { + if (m_pc != nullptr) { pcre_free(m_pc); - m_pc = NULL; + m_pc = nullptr; } - if (m_pce != NULL) { + if (m_pce != nullptr) { #if PCRE_HAVE_JIT pcre_free_study(m_pce); #else pcre_free(m_pce); #endif - m_pce = NULL; + m_pce = nullptr; } #endif } @@ -143,16 +143,16 @@ std::list Regex::searchAll(const std::string& s) const { PCRE2_SPTR pcre2_s = reinterpret_cast(s.c_str()); PCRE2_SIZE offset = 0; - pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, NULL); + pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, nullptr); do { if (m_pcje == 0) { rc = pcre2_jit_match(m_pc, pcre2_s, s.length(), - offset, 0, match_data, NULL); + offset, 0, match_data, nullptr); } if (m_pcje != 0 || rc == PCRE2_ERROR_JIT_STACKLIMIT) { rc = pcre2_match(m_pc, pcre2_s, s.length(), - offset, PCRE2_NO_JIT, match_data, NULL); + offset, PCRE2_NO_JIT, match_data, nullptr); } const PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data); #else @@ -194,18 +194,18 @@ RegexResult Regex::searchOneMatch(const std::string& s, std::vector 0) { // TODO: What if setting the match limit fails? - pcre2_set_match_limit(match_context, match_limit); + pcre2_set_match_limit(static_cast(match_context), match_limit); } PCRE2_SPTR pcre2_s = reinterpret_cast(s.c_str()); - pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, NULL); + pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, nullptr); int rc = 0; if (m_pcje == 0) { - rc = pcre2_jit_match(m_pc, pcre2_s, s.length(), 0, 0, match_data, match_context); + rc = pcre2_jit_match(m_pc, pcre2_s, s.length(), 0, 0, match_data, static_cast(match_context)); } if (m_pcje != 0 || rc == PCRE2_ERROR_JIT_STACKLIMIT) { - rc = pcre2_match(m_pc, pcre2_s, s.length(), 0, PCRE2_NO_JIT, match_data, match_context); + rc = pcre2_match(m_pc, pcre2_s, s.length(), 0, PCRE2_NO_JIT, match_data, static_cast(match_context)); } const PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data); #else @@ -214,7 +214,7 @@ RegexResult Regex::searchOneMatch(const std::string& s, std::vector 0) { + if (m_pce != nullptr && match_limit > 0) { local_pce = *m_pce; local_pce.match_limit = match_limit; local_pce.flags |= PCRE_EXTRA_MATCH_LIMIT; @@ -247,20 +247,20 @@ RegexResult Regex::searchGlobal(const std::string& s, std::vector Pcre2MatchContextPtr match_context; if (match_limit > 0) { // TODO: What if setting the match limit fails? - pcre2_set_match_limit(match_context, match_limit); + pcre2_set_match_limit(static_cast(match_context), match_limit); } PCRE2_SPTR pcre2_s = reinterpret_cast(s.c_str()); PCRE2_SIZE startOffset = 0; - pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, NULL); + pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, nullptr); while (startOffset <= s.length()) { uint32_t pcre2_options = 0; if (prev_match_zero_length) { pcre2_options = PCRE2_NOTEMPTY_ATSTART | PCRE2_ANCHORED; } int rc = pcre2_match(m_pc, pcre2_s, s.length(), - startOffset, pcre2_options, match_data, match_context); + startOffset, pcre2_options, match_data, static_cast(match_context)); const PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data); #else @@ -268,7 +268,7 @@ RegexResult Regex::searchGlobal(const std::string& s, std::vector pcre_extra local_pce; pcre_extra *pce = m_pce; - if (m_pce != NULL && match_limit > 0) { + if (m_pce != nullptr && match_limit > 0) { local_pce = *m_pce; local_pce.match_limit = match_limit; local_pce.flags |= PCRE_EXTRA_MATCH_LIMIT; @@ -346,16 +346,16 @@ RegexResult Regex::searchGlobal(const std::string& s, std::vector int Regex::search(const std::string& s, SMatch *match) const { #ifndef WITH_PCRE PCRE2_SPTR pcre2_s = reinterpret_cast(s.c_str()); - pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, NULL); + pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, nullptr); int ret = 0; if (m_pcje == 0) { ret = pcre2_match(m_pc, pcre2_s, s.length(), - 0, 0, match_data, NULL) > 0; + 0, 0, match_data, nullptr) > 0; } if (m_pcje != 0 || ret == PCRE2_ERROR_JIT_STACKLIMIT) { ret = pcre2_match(m_pc, pcre2_s, s.length(), - 0, PCRE2_NO_JIT, match_data, NULL) > 0; + 0, PCRE2_NO_JIT, match_data, nullptr) > 0; } if (ret > 0) { // match PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data); @@ -380,14 +380,14 @@ int Regex::search(const std::string& s, SMatch *match) const { int Regex::search(const std::string& s) const { #ifndef WITH_PCRE PCRE2_SPTR pcre2_s = reinterpret_cast(s.c_str()); - pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, NULL); + pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, nullptr); int rc = 0; if (m_pcje == 0) { - rc = pcre2_jit_match(m_pc, pcre2_s, s.length(), 0, 0, match_data, NULL); + rc = pcre2_jit_match(m_pc, pcre2_s, s.length(), 0, 0, match_data, nullptr); } if (m_pcje != 0 || rc == PCRE2_ERROR_JIT_STACKLIMIT) { - rc = pcre2_match(m_pc, pcre2_s, s.length(), 0, PCRE2_NO_JIT, match_data, NULL); + rc = pcre2_match(m_pc, pcre2_s, s.length(), 0, PCRE2_NO_JIT, match_data, nullptr); } pcre2_match_data_free(match_data); if (rc > 0) { diff --git a/src/utils/regex.h b/src/utils/regex.h index a771720a..863ce560 100644 --- a/src/utils/regex.h +++ b/src/utils/regex.h @@ -79,7 +79,7 @@ class Regex { Regex& operator=(const Regex&) = delete; bool hasError() const { - return (m_pc == NULL); + return (m_pc == nullptr); } std::list searchAll(const std::string& s) const; RegexResult searchOneMatch(const std::string& s, std::vector& captures, unsigned long match_limit = 0) const; @@ -95,8 +95,8 @@ class Regex { pcre2_code *m_pc; int m_pcje; #else - pcre *m_pc = NULL; - pcre_extra *m_pce = NULL; + pcre *m_pc = nullptr; + pcre_extra *m_pce = nullptr; #endif };