mirror of
https://github.com/owasp-modsecurity/ModSecurity.git
synced 2025-08-14 05:45:59 +03:00
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:
parent
b97b61b711
commit
d68aef320c
@ -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<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. */
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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<SMatch> Regex::searchAll(const std::string& s) const {
|
||||
PCRE2_SPTR pcre2_s = reinterpret_cast<PCRE2_SPTR>(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<SMatchCaptur
|
||||
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<pcre2_match_context*>(match_context), match_limit);
|
||||
}
|
||||
|
||||
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;
|
||||
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) {
|
||||
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);
|
||||
#else
|
||||
@ -214,7 +214,7 @@ RegexResult Regex::searchOneMatch(const std::string& s, std::vector<SMatchCaptur
|
||||
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;
|
||||
@ -247,20 +247,20 @@ RegexResult Regex::searchGlobal(const std::string& s, std::vector<SMatchCapture>
|
||||
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<pcre2_match_context*>(match_context), match_limit);
|
||||
}
|
||||
|
||||
PCRE2_SPTR pcre2_s = reinterpret_cast<PCRE2_SPTR>(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<pcre2_match_context*>(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<SMatchCapture>
|
||||
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<SMatchCapture>
|
||||
int Regex::search(const std::string& s, SMatch *match) const {
|
||||
#ifndef WITH_PCRE
|
||||
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;
|
||||
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<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;
|
||||
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) {
|
||||
|
@ -79,7 +79,7 @@ class Regex {
|
||||
Regex& operator=(const Regex&) = delete;
|
||||
|
||||
bool hasError() const {
|
||||
return (m_pc == NULL);
|
||||
return (m_pc == nullptr);
|
||||
}
|
||||
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;
|
||||
@ -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
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user