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.
This commit is contained in:
Gabor Berkes 2025-02-20 12:25:53 +00:00
parent b97b61b711
commit d68aef320c
4 changed files with 49 additions and 49 deletions

View File

@ -25,7 +25,7 @@
#if PCRE_HAVE_JIT #if PCRE_HAVE_JIT
#define pcre_study_opt PCRE_STUDY_JIT_COMPILE #define pcre_study_opt PCRE_STUDY_JIT_COMPILE
#else #else
#define pcre_study_opt 0 constexpr int pcre_study_opt = 0;
#endif #endif
#endif #endif
@ -37,17 +37,17 @@ VerifyCC::~VerifyCC() {
#ifndef WITH_PCRE #ifndef WITH_PCRE
pcre2_code_free(m_pc); pcre2_code_free(m_pc);
#else #else
if (m_pc != NULL) { if (m_pc != nullptr) {
pcre_free(m_pc); pcre_free(m_pc);
m_pc = NULL; m_pc = nullptr;
} }
if (m_pce != NULL) { if (m_pce != nullptr) {
#if PCRE_HAVE_JIT #if PCRE_HAVE_JIT
pcre_free_study(m_pce); pcre_free_study(m_pce);
#else #else
pcre_free(m_pce); pcre_free(m_pce);
#endif #endif
m_pce = NULL; m_pce = nullptr;
} }
#endif #endif
} }
@ -100,27 +100,27 @@ bool VerifyCC::init(const std::string &param2, std::string *error) {
int errornumber = 0; int errornumber = 0;
PCRE2_SIZE erroroffset = 0; PCRE2_SIZE erroroffset = 0;
m_pc = pcre2_compile(pcre2_pattern, PCRE2_ZERO_TERMINATED, m_pc = pcre2_compile(pcre2_pattern, PCRE2_ZERO_TERMINATED,
pcre2_options, &errornumber, &erroroffset, NULL); pcre2_options, &errornumber, &erroroffset, nullptr);
if (m_pc == NULL) { if (m_pc == nullptr) {
return false; return false;
} }
m_pcje = pcre2_jit_compile(m_pc, PCRE2_JIT_COMPLETE); m_pcje = pcre2_jit_compile(m_pc, PCRE2_JIT_COMPLETE);
#else #else
const char *errptr = NULL; const char *errptr = nullptr;
int erroffset = 0; int erroffset = 0;
m_pc = pcre_compile(m_param.c_str(), PCRE_DOTALL|PCRE_MULTILINE, m_pc = pcre_compile(m_param.c_str(), PCRE_DOTALL|PCRE_MULTILINE,
&errptr, &erroffset, NULL); &errptr, &erroffset, nullptr);
if (m_pc == NULL) { if (m_pc == nullptr) {
error->assign(errptr); error->assign(errptr);
return false; return false;
} }
m_pce = pcre_study(m_pc, pcre_study_opt, &errptr); m_pce = pcre_study(m_pc, pcre_study_opt, &errptr);
if (m_pce == NULL) { if (m_pce == nullptr) {
if (errptr == NULL) { 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 * that no addional information is found, so no need to study
*/ */
return true; return true;
@ -140,17 +140,17 @@ bool VerifyCC::evaluate(Transaction *t, RuleWithActions *rule,
PCRE2_SIZE offset = 0; PCRE2_SIZE offset = 0;
size_t target_length = i.length(); size_t target_length = i.length();
PCRE2_SPTR pcre2_i = reinterpret_cast<PCRE2_SPTR>(i.c_str()); PCRE2_SPTR pcre2_i = reinterpret_cast<PCRE2_SPTR>(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; int ret;
for (offset = 0; offset < target_length; offset++) { for (offset = 0; offset < target_length; offset++) {
if (m_pcje == 0) { 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) { 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. */ /* If there was no match, then we are done. */

View File

@ -39,11 +39,11 @@ class VerifyCC : public Operator {
explicit VerifyCC(std::unique_ptr<RunTimeString> param) explicit VerifyCC(std::unique_ptr<RunTimeString> param)
: Operator("VerifyCC", std::move(param)), : Operator("VerifyCC", std::move(param)),
#ifndef WITH_PCRE #ifndef WITH_PCRE
m_pc(NULL), m_pc(nullptr),
m_pcje(PCRE2_ERROR_JIT_BADOPTION) { } m_pcje(PCRE2_ERROR_JIT_BADOPTION) { }
#else #else
m_pc(NULL), m_pc(nullptr),
m_pce(NULL) { } m_pce(nullptr) { }
#endif #endif
~VerifyCC() override; ~VerifyCC() override;

View File

@ -39,7 +39,7 @@
class Pcre2MatchContextPtr { class Pcre2MatchContextPtr {
public: public:
Pcre2MatchContextPtr() Pcre2MatchContextPtr()
: m_match_context(pcre2_match_context_create(NULL)) {} : m_match_context(pcre2_match_context_create(nullptr)) {}
Pcre2MatchContextPtr(const Pcre2MatchContextPtr&) = delete; Pcre2MatchContextPtr(const Pcre2MatchContextPtr&) = delete;
Pcre2MatchContextPtr& operator=(const Pcre2MatchContextPtr&) = delete; Pcre2MatchContextPtr& operator=(const Pcre2MatchContextPtr&) = delete;
@ -48,7 +48,7 @@ class Pcre2MatchContextPtr {
pcre2_match_context_free(m_match_context); pcre2_match_context_free(m_match_context);
} }
operator pcre2_match_context*() const { explicit operator pcre2_match_context*() const {
return m_match_context; return m_match_context;
} }
@ -98,10 +98,10 @@ Regex::Regex(const std::string& pattern_, bool ignoreCase)
int errornumber = 0; int errornumber = 0;
PCRE2_SIZE erroroffset = 0; PCRE2_SIZE erroroffset = 0;
m_pc = pcre2_compile(pcre2_pattern, PCRE2_ZERO_TERMINATED, 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); m_pcje = pcre2_jit_compile(m_pc, PCRE2_JIT_COMPLETE);
#else #else
const char *errptr = NULL; const char *errptr = nullptr;
int erroffset; int erroffset;
int flags = (PCRE_DOTALL|PCRE_MULTILINE); int flags = (PCRE_DOTALL|PCRE_MULTILINE);
@ -109,7 +109,7 @@ Regex::Regex(const std::string& pattern_, bool ignoreCase)
flags |= PCRE_CASELESS; flags |= PCRE_CASELESS;
} }
m_pc = pcre_compile(pattern.c_str(), flags, m_pc = pcre_compile(pattern.c_str(), flags,
&errptr, &erroffset, NULL); &errptr, &erroffset, nullptr);
m_pce = pcre_study(m_pc, pcre_study_opt, &errptr); m_pce = pcre_study(m_pc, pcre_study_opt, &errptr);
#endif #endif
@ -120,17 +120,17 @@ Regex::~Regex() {
#ifndef WITH_PCRE #ifndef WITH_PCRE
pcre2_code_free(m_pc); pcre2_code_free(m_pc);
#else #else
if (m_pc != NULL) { if (m_pc != nullptr) {
pcre_free(m_pc); pcre_free(m_pc);
m_pc = NULL; m_pc = nullptr;
} }
if (m_pce != NULL) { if (m_pce != nullptr) {
#if PCRE_HAVE_JIT #if PCRE_HAVE_JIT
pcre_free_study(m_pce); pcre_free_study(m_pce);
#else #else
pcre_free(m_pce); pcre_free(m_pce);
#endif #endif
m_pce = NULL; m_pce = nullptr;
} }
#endif #endif
} }
@ -143,16 +143,16 @@ std::list<SMatch> Regex::searchAll(const std::string& s) const {
PCRE2_SPTR pcre2_s = reinterpret_cast<PCRE2_SPTR>(s.c_str()); PCRE2_SPTR pcre2_s = reinterpret_cast<PCRE2_SPTR>(s.c_str());
PCRE2_SIZE offset = 0; 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 { do {
if (m_pcje == 0) { if (m_pcje == 0) {
rc = pcre2_jit_match(m_pc, pcre2_s, s.length(), 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) { if (m_pcje != 0 || rc == PCRE2_ERROR_JIT_STACKLIMIT) {
rc = pcre2_match(m_pc, pcre2_s, s.length(), 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); const PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
#else #else
@ -194,18 +194,18 @@ RegexResult Regex::searchOneMatch(const std::string& s, std::vector<SMatchCaptur
Pcre2MatchContextPtr match_context; Pcre2MatchContextPtr match_context;
if (match_limit > 0) { if (match_limit > 0) {
// TODO: What if setting the match limit fails? // TODO: What if setting the match limit fails?
pcre2_set_match_limit(match_context, match_limit); pcre2_set_match_limit(static_cast<pcre2_match_context*>(match_context), match_limit);
} }
PCRE2_SPTR pcre2_s = reinterpret_cast<PCRE2_SPTR>(s.c_str()); PCRE2_SPTR pcre2_s = reinterpret_cast<PCRE2_SPTR>(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; int rc = 0;
if (m_pcje == 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<pcre2_match_context*>(match_context));
} }
if (m_pcje != 0 || rc == PCRE2_ERROR_JIT_STACKLIMIT) { 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<pcre2_match_context*>(match_context));
} }
const PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data); const PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
#else #else
@ -214,7 +214,7 @@ RegexResult Regex::searchOneMatch(const std::string& s, std::vector<SMatchCaptur
pcre_extra local_pce; pcre_extra local_pce;
pcre_extra *pce = m_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 = *m_pce;
local_pce.match_limit = match_limit; local_pce.match_limit = match_limit;
local_pce.flags |= PCRE_EXTRA_MATCH_LIMIT; local_pce.flags |= PCRE_EXTRA_MATCH_LIMIT;
@ -247,20 +247,20 @@ RegexResult Regex::searchGlobal(const std::string& s, std::vector<SMatchCapture>
Pcre2MatchContextPtr match_context; Pcre2MatchContextPtr match_context;
if (match_limit > 0) { if (match_limit > 0) {
// TODO: What if setting the match limit fails? // TODO: What if setting the match limit fails?
pcre2_set_match_limit(match_context, match_limit); pcre2_set_match_limit(static_cast<pcre2_match_context*>(match_context), match_limit);
} }
PCRE2_SPTR pcre2_s = reinterpret_cast<PCRE2_SPTR>(s.c_str()); PCRE2_SPTR pcre2_s = reinterpret_cast<PCRE2_SPTR>(s.c_str());
PCRE2_SIZE startOffset = 0; 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()) { while (startOffset <= s.length()) {
uint32_t pcre2_options = 0; uint32_t pcre2_options = 0;
if (prev_match_zero_length) { if (prev_match_zero_length) {
pcre2_options = PCRE2_NOTEMPTY_ATSTART | PCRE2_ANCHORED; pcre2_options = PCRE2_NOTEMPTY_ATSTART | PCRE2_ANCHORED;
} }
int rc = pcre2_match(m_pc, pcre2_s, s.length(), int rc = pcre2_match(m_pc, pcre2_s, s.length(),
startOffset, pcre2_options, match_data, match_context); startOffset, pcre2_options, match_data, static_cast<pcre2_match_context*>(match_context));
const PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data); const PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
#else #else
@ -268,7 +268,7 @@ RegexResult Regex::searchGlobal(const std::string& s, std::vector<SMatchCapture>
pcre_extra local_pce; pcre_extra local_pce;
pcre_extra *pce = m_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 = *m_pce;
local_pce.match_limit = match_limit; local_pce.match_limit = match_limit;
local_pce.flags |= PCRE_EXTRA_MATCH_LIMIT; local_pce.flags |= PCRE_EXTRA_MATCH_LIMIT;
@ -346,16 +346,16 @@ RegexResult Regex::searchGlobal(const std::string& s, std::vector<SMatchCapture>
int Regex::search(const std::string& s, SMatch *match) const { int Regex::search(const std::string& s, SMatch *match) const {
#ifndef WITH_PCRE #ifndef WITH_PCRE
PCRE2_SPTR pcre2_s = reinterpret_cast<PCRE2_SPTR>(s.c_str()); PCRE2_SPTR pcre2_s = reinterpret_cast<PCRE2_SPTR>(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; int ret = 0;
if (m_pcje == 0) { if (m_pcje == 0) {
ret = pcre2_match(m_pc, pcre2_s, s.length(), 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) { if (m_pcje != 0 || ret == PCRE2_ERROR_JIT_STACKLIMIT) {
ret = pcre2_match(m_pc, pcre2_s, s.length(), 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 if (ret > 0) { // match
PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data); 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 { int Regex::search(const std::string& s) const {
#ifndef WITH_PCRE #ifndef WITH_PCRE
PCRE2_SPTR pcre2_s = reinterpret_cast<PCRE2_SPTR>(s.c_str()); PCRE2_SPTR pcre2_s = reinterpret_cast<PCRE2_SPTR>(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; int rc = 0;
if (m_pcje == 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) { 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); pcre2_match_data_free(match_data);
if (rc > 0) { if (rc > 0) {

View File

@ -79,7 +79,7 @@ class Regex {
Regex& operator=(const Regex&) = delete; Regex& operator=(const Regex&) = delete;
bool hasError() const { bool hasError() const {
return (m_pc == NULL); return (m_pc == nullptr);
} }
std::list<SMatch> searchAll(const std::string& s) const; std::list<SMatch> searchAll(const std::string& s) const;
RegexResult searchOneMatch(const std::string& s, std::vector<SMatchCapture>& captures, unsigned long match_limit = 0) const; RegexResult searchOneMatch(const std::string& s, std::vector<SMatchCapture>& captures, unsigned long match_limit = 0) const;
@ -95,8 +95,8 @@ class Regex {
pcre2_code *m_pc; pcre2_code *m_pc;
int m_pcje; int m_pcje;
#else #else
pcre *m_pc = NULL; pcre *m_pc = nullptr;
pcre_extra *m_pce = NULL; pcre_extra *m_pce = nullptr;
#endif #endif
}; };