Skip to content

Commit d68aef3

Browse files
author
Gabor Berkes
committed
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.
1 parent b97b61b commit d68aef3

File tree

4 files changed

+49
-49
lines changed

4 files changed

+49
-49
lines changed

src/operators/verify_cc.cc

+16-16
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#if PCRE_HAVE_JIT
2626
#define pcre_study_opt PCRE_STUDY_JIT_COMPILE
2727
#else
28-
#define pcre_study_opt 0
28+
constexpr int pcre_study_opt = 0;
2929
#endif
3030
#endif
3131

@@ -37,17 +37,17 @@ VerifyCC::~VerifyCC() {
3737
#ifndef WITH_PCRE
3838
pcre2_code_free(m_pc);
3939
#else
40-
if (m_pc != NULL) {
40+
if (m_pc != nullptr) {
4141
pcre_free(m_pc);
42-
m_pc = NULL;
42+
m_pc = nullptr;
4343
}
44-
if (m_pce != NULL) {
44+
if (m_pce != nullptr) {
4545
#if PCRE_HAVE_JIT
4646
pcre_free_study(m_pce);
4747
#else
4848
pcre_free(m_pce);
4949
#endif
50-
m_pce = NULL;
50+
m_pce = nullptr;
5151
}
5252
#endif
5353
}
@@ -100,27 +100,27 @@ bool VerifyCC::init(const std::string &param2, std::string *error) {
100100
int errornumber = 0;
101101
PCRE2_SIZE erroroffset = 0;
102102
m_pc = pcre2_compile(pcre2_pattern, PCRE2_ZERO_TERMINATED,
103-
pcre2_options, &errornumber, &erroroffset, NULL);
104-
if (m_pc == NULL) {
103+
pcre2_options, &errornumber, &erroroffset, nullptr);
104+
if (m_pc == nullptr) {
105105
return false;
106106
}
107107
m_pcje = pcre2_jit_compile(m_pc, PCRE2_JIT_COMPLETE);
108108
#else
109-
const char *errptr = NULL;
109+
const char *errptr = nullptr;
110110
int erroffset = 0;
111111

112112
m_pc = pcre_compile(m_param.c_str(), PCRE_DOTALL|PCRE_MULTILINE,
113-
&errptr, &erroffset, NULL);
114-
if (m_pc == NULL) {
113+
&errptr, &erroffset, nullptr);
114+
if (m_pc == nullptr) {
115115
error->assign(errptr);
116116
return false;
117117
}
118118

119119
m_pce = pcre_study(m_pc, pcre_study_opt, &errptr);
120-
if (m_pce == NULL) {
121-
if (errptr == NULL) {
120+
if (m_pce == nullptr) {
121+
if (errptr == nullptr) {
122122
/*
123-
* Per pcre_study(3) m_pce == NULL && errptr == NULL means
123+
* Per pcre_study(3) m_pce == nullptr && errptr == nullptr means
124124
* that no addional information is found, so no need to study
125125
*/
126126
return true;
@@ -140,17 +140,17 @@ bool VerifyCC::evaluate(Transaction *t, RuleWithActions *rule,
140140
PCRE2_SIZE offset = 0;
141141
size_t target_length = i.length();
142142
PCRE2_SPTR pcre2_i = reinterpret_cast<PCRE2_SPTR>(i.c_str());
143-
pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, NULL);
143+
pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, nullptr);
144144

145145
int ret;
146146
for (offset = 0; offset < target_length; offset++) {
147147

148148
if (m_pcje == 0) {
149-
ret = pcre2_jit_match(m_pc, pcre2_i, target_length, offset, 0, match_data, NULL);
149+
ret = pcre2_jit_match(m_pc, pcre2_i, target_length, offset, 0, match_data, nullptr);
150150
}
151151

152152
if (m_pcje != 0 || ret == PCRE2_ERROR_JIT_STACKLIMIT) {
153-
ret = pcre2_match(m_pc, pcre2_i, target_length, offset, PCRE2_NO_JIT, match_data, NULL);
153+
ret = pcre2_match(m_pc, pcre2_i, target_length, offset, PCRE2_NO_JIT, match_data, nullptr);
154154
}
155155

156156
/* If there was no match, then we are done. */

src/operators/verify_cc.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ class VerifyCC : public Operator {
3939
explicit VerifyCC(std::unique_ptr<RunTimeString> param)
4040
: Operator("VerifyCC", std::move(param)),
4141
#ifndef WITH_PCRE
42-
m_pc(NULL),
42+
m_pc(nullptr),
4343
m_pcje(PCRE2_ERROR_JIT_BADOPTION) { }
4444
#else
45-
m_pc(NULL),
46-
m_pce(NULL) { }
45+
m_pc(nullptr),
46+
m_pce(nullptr) { }
4747
#endif
4848
~VerifyCC() override;
4949

src/utils/regex.cc

+27-27
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
class Pcre2MatchContextPtr {
4040
public:
4141
Pcre2MatchContextPtr()
42-
: m_match_context(pcre2_match_context_create(NULL)) {}
42+
: m_match_context(pcre2_match_context_create(nullptr)) {}
4343

4444
Pcre2MatchContextPtr(const Pcre2MatchContextPtr&) = delete;
4545
Pcre2MatchContextPtr& operator=(const Pcre2MatchContextPtr&) = delete;
@@ -48,7 +48,7 @@ class Pcre2MatchContextPtr {
4848
pcre2_match_context_free(m_match_context);
4949
}
5050

51-
operator pcre2_match_context*() const {
51+
explicit operator pcre2_match_context*() const {
5252
return m_match_context;
5353
}
5454

@@ -98,18 +98,18 @@ Regex::Regex(const std::string& pattern_, bool ignoreCase)
9898
int errornumber = 0;
9999
PCRE2_SIZE erroroffset = 0;
100100
m_pc = pcre2_compile(pcre2_pattern, PCRE2_ZERO_TERMINATED,
101-
pcre2_options, &errornumber, &erroroffset, NULL);
101+
pcre2_options, &errornumber, &erroroffset, nullptr);
102102
m_pcje = pcre2_jit_compile(m_pc, PCRE2_JIT_COMPLETE);
103103
#else
104-
const char *errptr = NULL;
104+
const char *errptr = nullptr;
105105
int erroffset;
106106
int flags = (PCRE_DOTALL|PCRE_MULTILINE);
107107

108108
if (ignoreCase == true) {
109109
flags |= PCRE_CASELESS;
110110
}
111111
m_pc = pcre_compile(pattern.c_str(), flags,
112-
&errptr, &erroffset, NULL);
112+
&errptr, &erroffset, nullptr);
113113

114114
m_pce = pcre_study(m_pc, pcre_study_opt, &errptr);
115115
#endif
@@ -120,17 +120,17 @@ Regex::~Regex() {
120120
#ifndef WITH_PCRE
121121
pcre2_code_free(m_pc);
122122
#else
123-
if (m_pc != NULL) {
123+
if (m_pc != nullptr) {
124124
pcre_free(m_pc);
125-
m_pc = NULL;
125+
m_pc = nullptr;
126126
}
127-
if (m_pce != NULL) {
127+
if (m_pce != nullptr) {
128128
#if PCRE_HAVE_JIT
129129
pcre_free_study(m_pce);
130130
#else
131131
pcre_free(m_pce);
132132
#endif
133-
m_pce = NULL;
133+
m_pce = nullptr;
134134
}
135135
#endif
136136
}
@@ -143,16 +143,16 @@ std::list<SMatch> Regex::searchAll(const std::string& s) const {
143143
PCRE2_SPTR pcre2_s = reinterpret_cast<PCRE2_SPTR>(s.c_str());
144144
PCRE2_SIZE offset = 0;
145145

146-
pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, NULL);
146+
pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, nullptr);
147147
do {
148148
if (m_pcje == 0) {
149149
rc = pcre2_jit_match(m_pc, pcre2_s, s.length(),
150-
offset, 0, match_data, NULL);
150+
offset, 0, match_data, nullptr);
151151
}
152152

153153
if (m_pcje != 0 || rc == PCRE2_ERROR_JIT_STACKLIMIT) {
154154
rc = pcre2_match(m_pc, pcre2_s, s.length(),
155-
offset, PCRE2_NO_JIT, match_data, NULL);
155+
offset, PCRE2_NO_JIT, match_data, nullptr);
156156
}
157157
const PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
158158
#else
@@ -194,18 +194,18 @@ RegexResult Regex::searchOneMatch(const std::string& s, std::vector<SMatchCaptur
194194
Pcre2MatchContextPtr match_context;
195195
if (match_limit > 0) {
196196
// TODO: What if setting the match limit fails?
197-
pcre2_set_match_limit(match_context, match_limit);
197+
pcre2_set_match_limit(static_cast<pcre2_match_context*>(match_context), match_limit);
198198
}
199199

200200
PCRE2_SPTR pcre2_s = reinterpret_cast<PCRE2_SPTR>(s.c_str());
201-
pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, NULL);
201+
pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, nullptr);
202202
int rc = 0;
203203
if (m_pcje == 0) {
204-
rc = pcre2_jit_match(m_pc, pcre2_s, s.length(), 0, 0, match_data, match_context);
204+
rc = pcre2_jit_match(m_pc, pcre2_s, s.length(), 0, 0, match_data, static_cast<pcre2_match_context*>(match_context));
205205
}
206206

207207
if (m_pcje != 0 || rc == PCRE2_ERROR_JIT_STACKLIMIT) {
208-
rc = pcre2_match(m_pc, pcre2_s, s.length(), 0, PCRE2_NO_JIT, match_data, match_context);
208+
rc = pcre2_match(m_pc, pcre2_s, s.length(), 0, PCRE2_NO_JIT, match_data, static_cast<pcre2_match_context*>(match_context));
209209
}
210210
const PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
211211
#else
@@ -214,7 +214,7 @@ RegexResult Regex::searchOneMatch(const std::string& s, std::vector<SMatchCaptur
214214
pcre_extra local_pce;
215215
pcre_extra *pce = m_pce;
216216

217-
if (m_pce != NULL && match_limit > 0) {
217+
if (m_pce != nullptr && match_limit > 0) {
218218
local_pce = *m_pce;
219219
local_pce.match_limit = match_limit;
220220
local_pce.flags |= PCRE_EXTRA_MATCH_LIMIT;
@@ -247,28 +247,28 @@ RegexResult Regex::searchGlobal(const std::string& s, std::vector<SMatchCapture>
247247
Pcre2MatchContextPtr match_context;
248248
if (match_limit > 0) {
249249
// TODO: What if setting the match limit fails?
250-
pcre2_set_match_limit(match_context, match_limit);
250+
pcre2_set_match_limit(static_cast<pcre2_match_context*>(match_context), match_limit);
251251
}
252252

253253
PCRE2_SPTR pcre2_s = reinterpret_cast<PCRE2_SPTR>(s.c_str());
254254
PCRE2_SIZE startOffset = 0;
255255

256-
pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, NULL);
256+
pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, nullptr);
257257
while (startOffset <= s.length()) {
258258
uint32_t pcre2_options = 0;
259259
if (prev_match_zero_length) {
260260
pcre2_options = PCRE2_NOTEMPTY_ATSTART | PCRE2_ANCHORED;
261261
}
262262
int rc = pcre2_match(m_pc, pcre2_s, s.length(),
263-
startOffset, pcre2_options, match_data, match_context);
263+
startOffset, pcre2_options, match_data, static_cast<pcre2_match_context*>(match_context));
264264
const PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
265265

266266
#else
267267
const char *subject = s.c_str();
268268
pcre_extra local_pce;
269269
pcre_extra *pce = m_pce;
270270

271-
if (m_pce != NULL && match_limit > 0) {
271+
if (m_pce != nullptr && match_limit > 0) {
272272
local_pce = *m_pce;
273273
local_pce.match_limit = match_limit;
274274
local_pce.flags |= PCRE_EXTRA_MATCH_LIMIT;
@@ -346,16 +346,16 @@ RegexResult Regex::searchGlobal(const std::string& s, std::vector<SMatchCapture>
346346
int Regex::search(const std::string& s, SMatch *match) const {
347347
#ifndef WITH_PCRE
348348
PCRE2_SPTR pcre2_s = reinterpret_cast<PCRE2_SPTR>(s.c_str());
349-
pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, NULL);
349+
pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, nullptr);
350350
int ret = 0;
351351
if (m_pcje == 0) {
352352
ret = pcre2_match(m_pc, pcre2_s, s.length(),
353-
0, 0, match_data, NULL) > 0;
353+
0, 0, match_data, nullptr) > 0;
354354
}
355355

356356
if (m_pcje != 0 || ret == PCRE2_ERROR_JIT_STACKLIMIT) {
357357
ret = pcre2_match(m_pc, pcre2_s, s.length(),
358-
0, PCRE2_NO_JIT, match_data, NULL) > 0;
358+
0, PCRE2_NO_JIT, match_data, nullptr) > 0;
359359
}
360360
if (ret > 0) { // match
361361
PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
@@ -380,14 +380,14 @@ int Regex::search(const std::string& s, SMatch *match) const {
380380
int Regex::search(const std::string& s) const {
381381
#ifndef WITH_PCRE
382382
PCRE2_SPTR pcre2_s = reinterpret_cast<PCRE2_SPTR>(s.c_str());
383-
pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, NULL);
383+
pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, nullptr);
384384
int rc = 0;
385385
if (m_pcje == 0) {
386-
rc = pcre2_jit_match(m_pc, pcre2_s, s.length(), 0, 0, match_data, NULL);
386+
rc = pcre2_jit_match(m_pc, pcre2_s, s.length(), 0, 0, match_data, nullptr);
387387
}
388388

389389
if (m_pcje != 0 || rc == PCRE2_ERROR_JIT_STACKLIMIT) {
390-
rc = pcre2_match(m_pc, pcre2_s, s.length(), 0, PCRE2_NO_JIT, match_data, NULL);
390+
rc = pcre2_match(m_pc, pcre2_s, s.length(), 0, PCRE2_NO_JIT, match_data, nullptr);
391391
}
392392
pcre2_match_data_free(match_data);
393393
if (rc > 0) {

src/utils/regex.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class Regex {
7979
Regex& operator=(const Regex&) = delete;
8080

8181
bool hasError() const {
82-
return (m_pc == NULL);
82+
return (m_pc == nullptr);
8383
}
8484
std::list<SMatch> searchAll(const std::string& s) const;
8585
RegexResult searchOneMatch(const std::string& s, std::vector<SMatchCapture>& captures, unsigned long match_limit = 0) const;
@@ -95,8 +95,8 @@ class Regex {
9595
pcre2_code *m_pc;
9696
int m_pcje;
9797
#else
98-
pcre *m_pc = NULL;
99-
pcre_extra *m_pce = NULL;
98+
pcre *m_pc = nullptr;
99+
pcre_extra *m_pce = nullptr;
100100
#endif
101101
};
102102

0 commit comments

Comments
 (0)