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
#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 &param2, 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<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;
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. */

View File

@@ -39,11 +39,11 @@ class VerifyCC : public Operator {
explicit VerifyCC(std::unique_ptr<RunTimeString> 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;