Skip to content

Commit c6433df

Browse files
author
Gabor Berkes
committed
Refactor build system to use libpcre2 as the default
Updated the build system and related source files to use libpcre2 as the default regex library instead of the deprecated libpcre. This change ensures future compatibility and aligns with the library's maintenance status. To build with the old libpcre, the `--with-pcre` configuration parameter can be specified.
1 parent 4fb2246 commit c6433df

File tree

8 files changed

+51
-54
lines changed

8 files changed

+51
-54
lines changed

Diff for: .github/workflows/ci.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
- {label: "wo geoip", opt: "--without-geoip" }
2727
- {label: "wo ssdeep", opt: "--without-ssdeep" }
2828
- {label: "with lmdb", opt: "--with-lmdb" }
29-
- {label: "with pcre2", opt: "--with-pcre2" }
29+
- {label: "with pcre", opt: "--with-pcre" }
3030
exclude:
3131
- platform: {label: "x32"}
3232
configure: {label: "wo geoip"}
@@ -88,7 +88,7 @@ jobs:
8888
- {label: "wo geoip", opt: "--without-geoip" }
8989
- {label: "wo ssdeep", opt: "--without-ssdeep" }
9090
- {label: "with lmdb", opt: "--with-lmdb" }
91-
- {label: "with pcre2", opt: "--with-pcre2" }
91+
- {label: "with pcre", opt: "--with-pcre" }
9292
steps:
9393
- name: Setup Dependencies
9494
# curl, pcre2 not installed because they're already

Diff for: build/pcre.m4

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ AC_ARG_WITH(
2121
[test_paths="${with_pcre}"],
2222
[test_paths="/usr/local/libpcre /usr/local/pcre /usr/local /opt/libpcre /opt/pcre /opt /usr /opt/local"])
2323
24-
if test "x${with_pcre2}" != "x" && test "x${with_pcre2}" != "xno"; then
25-
AC_MSG_NOTICE([pcre2 specified; omitting check for pcre])
24+
if test "x${with_pcre}" == "x" && test "x${with_pcre}" != "xno"; then
25+
AC_MSG_NOTICE([Support for pcre not requested; omitting check for pcre])
2626
else
2727
2828
AC_MSG_CHECKING([for libpcre config script])
@@ -106,6 +106,7 @@ else
106106
LIBS=$save_LIBS
107107
fi
108108
109+
PCRE_CFLAGS="-DWITH_PCRE$ {PCRE_CFLAGS}"
109110
AC_SUBST(PCRE_CONFIG)
110111
AC_SUBST(PCRE_VERSION)
111112
AC_SUBST(PCRE_CPPFLAGS)

Diff for: build/pcre2.m4

+10-15
Original file line numberDiff line numberDiff line change
@@ -91,21 +91,16 @@ if test -z "${PCRE2_LDADD}"; then
9191
PCRE2_FOUND=-1
9292
fi
9393
else
94-
if test -z "${PCRE2_MANDATORY}"; then
95-
PCRE2_FOUND=2
96-
AC_MSG_NOTICE([PCRE2 is disabled by default.])
97-
else
98-
PCRE2_FOUND=1
99-
AC_MSG_NOTICE([using PCRE2 v${PCRE2_VERSION}])
100-
PCRE2_CFLAGS="-DWITH_PCRE2 ${PCRE2_CFLAGS}"
101-
PCRE2_DISPLAY="${PCRE2_LDADD}, ${PCRE2_CFLAGS}"
102-
AC_SUBST(PCRE2_VERSION)
103-
AC_SUBST(PCRE2_LDADD)
104-
AC_SUBST(PCRE2_LIBS)
105-
AC_SUBST(PCRE2_LDFLAGS)
106-
AC_SUBST(PCRE2_CFLAGS)
107-
AC_SUBST(PCRE2_DISPLAY)
108-
fi
94+
PCRE2_FOUND=1
95+
AC_MSG_NOTICE([using PCRE2 v${PCRE2_VERSION}])
96+
PCRE2_CFLAGS="${PCRE2_CFLAGS}"
97+
PCRE2_DISPLAY="${PCRE2_LDADD}, ${PCRE2_CFLAGS}"
98+
AC_SUBST(PCRE2_VERSION)
99+
AC_SUBST(PCRE2_LDADD)
100+
AC_SUBST(PCRE2_LIBS)
101+
AC_SUBST(PCRE2_LDFLAGS)
102+
AC_SUBST(PCRE2_CFLAGS)
103+
AC_SUBST(PCRE2_DISPLAY)
109104
fi
110105
111106

Diff for: configure.ac

+9-8
Original file line numberDiff line numberDiff line change
@@ -141,15 +141,16 @@ CHECK_LIBXML2
141141

142142

143143
#
144-
# Check for libpcre
144+
# Check for libpcre only if explicitly requested
145145
#
146-
CHECK_PCRE
147-
148-
149-
#
150-
# Check for pcre2
151-
#
152-
PROG_PCRE2
146+
if test "x${with_pcre}" != "x" && test "x${with_pcre}" != "xno"; then
147+
CHECK_PCRE
148+
else
149+
#
150+
# Check for pcre2
151+
#
152+
PROG_PCRE2
153+
fi
153154

154155

155156
# Checks for header files.

Diff for: src/operators/verify_cc.cc

+6-6
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
#include "src/operators/operator.h"
2323

24-
#ifndef WITH_PCRE2
24+
#ifdef WITH_PCRE
2525
#if PCRE_HAVE_JIT
2626
#define pcre_study_opt PCRE_STUDY_JIT_COMPILE
2727
#else
@@ -34,7 +34,7 @@ namespace modsecurity {
3434
namespace operators {
3535

3636
VerifyCC::~VerifyCC() {
37-
#if WITH_PCRE2
37+
#ifndef WITH_PCRE
3838
pcre2_code_free(m_pc);
3939
#else
4040
if (m_pc != NULL) {
@@ -94,7 +94,7 @@ int VerifyCC::luhnVerify(const char *ccnumber, int len) {
9494

9595

9696
bool VerifyCC::init(const std::string &param2, std::string *error) {
97-
#ifdef WITH_PCRE2
97+
#ifndef WITH_PCRE
9898
PCRE2_SPTR pcre2_pattern = reinterpret_cast<PCRE2_SPTR>(m_param.c_str());
9999
uint32_t pcre2_options = (PCRE2_DOTALL|PCRE2_MULTILINE);
100100
int errornumber = 0;
@@ -136,7 +136,7 @@ bool VerifyCC::init(const std::string &param2, std::string *error) {
136136

137137
bool VerifyCC::evaluate(Transaction *t, RuleWithActions *rule,
138138
const std::string& i, RuleMessage &ruleMessage) {
139-
#ifdef WITH_PCRE2
139+
#ifndef WITH_PCRE
140140
PCRE2_SIZE offset = 0;
141141
size_t target_length = i.length();
142142
PCRE2_SPTR pcre2_i = reinterpret_cast<PCRE2_SPTR>(i.c_str());
@@ -192,15 +192,15 @@ bool VerifyCC::evaluate(Transaction *t, RuleWithActions *rule,
192192
"\" at " + i + ". [offset " +
193193
std::to_string(offset) + "]");
194194
}
195-
#ifdef WITH_PCRE2
195+
#ifndef WITH_PCRE
196196
pcre2_match_data_free(match_data);
197197
#endif
198198
return true;
199199
}
200200
}
201201
}
202202

203-
#ifdef WITH_PCRE2
203+
#ifndef WITH_PCRE
204204
pcre2_match_data_free(match_data);
205205
#endif
206206

Diff for: src/operators/verify_cc.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#ifndef SRC_OPERATORS_VERIFY_CC_H_
1717
#define SRC_OPERATORS_VERIFY_CC_H_
1818

19-
#if WITH_PCRE2
19+
#ifndef WITH_PCRE
2020
#define PCRE2_CODE_UNIT_WIDTH 8
2121
#include <pcre2.h>
2222
#else
@@ -38,7 +38,7 @@ class VerifyCC : public Operator {
3838
/** @ingroup ModSecurity_Operator */
3939
explicit VerifyCC(std::unique_ptr<RunTimeString> param)
4040
: Operator("VerifyCC", std::move(param)),
41-
#if WITH_PCRE2
41+
#ifndef WITH_PCRE
4242
m_pc(NULL),
4343
m_pcje(PCRE2_ERROR_JIT_BADOPTION) { }
4444
#else
@@ -52,7 +52,7 @@ class VerifyCC : public Operator {
5252
RuleMessage &ruleMessage) override;
5353
bool init(const std::string &param, std::string *error) override;
5454
private:
55-
#if WITH_PCRE2
55+
#ifndef WITH_PCRE
5656
pcre2_code *m_pc;
5757
int m_pcje;
5858
#else

Diff for: src/utils/regex.cc

+16-16
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
#include "src/utils/geo_lookup.h"
2525

26-
#ifndef WITH_PCRE2
26+
#ifdef WITH_PCRE
2727
#if PCRE_HAVE_JIT
2828
// NOTE: Add PCRE_STUDY_EXTRA_NEEDED so studying always yields a pcre_extra strucure
2929
// and we can selectively override match limits using a copy of that structure at runtime.
@@ -35,7 +35,7 @@
3535
#endif
3636
#endif
3737

38-
#ifdef WITH_PCRE2
38+
#ifndef WITH_PCRE
3939
class Pcre2MatchContextPtr {
4040
public:
4141
Pcre2MatchContextPtr()
@@ -62,7 +62,7 @@ namespace Utils {
6262

6363
// Helper function to tell us if the current config indicates CRLF is a valid newline sequence
6464
bool crlfIsNewline() {
65-
#if WITH_PCRE2
65+
#ifndef WITH_PCRE
6666
uint32_t newline = 0;
6767
pcre2_config(PCRE2_CONFIG_NEWLINE, &newline);
6868
bool crlf_is_newline =
@@ -89,7 +89,7 @@ bool crlfIsNewline() {
8989

9090
Regex::Regex(const std::string& pattern_, bool ignoreCase)
9191
: pattern(pattern_.empty() ? ".*" : pattern_) {
92-
#if WITH_PCRE2
92+
#ifndef WITH_PCRE
9393
PCRE2_SPTR pcre2_pattern = reinterpret_cast<PCRE2_SPTR>(pattern.c_str());
9494
uint32_t pcre2_options = (PCRE2_DOTALL|PCRE2_MULTILINE);
9595
if (ignoreCase) {
@@ -117,7 +117,7 @@ Regex::Regex(const std::string& pattern_, bool ignoreCase)
117117

118118

119119
Regex::~Regex() {
120-
#if WITH_PCRE2
120+
#ifndef WITH_PCRE
121121
pcre2_code_free(m_pc);
122122
#else
123123
if (m_pc != NULL) {
@@ -139,7 +139,7 @@ Regex::~Regex() {
139139
std::list<SMatch> Regex::searchAll(const std::string& s) const {
140140
std::list<SMatch> retList;
141141
int rc = 0;
142-
#ifdef WITH_PCRE2
142+
#ifndef WITH_PCRE
143143
PCRE2_SPTR pcre2_s = reinterpret_cast<PCRE2_SPTR>(s.c_str());
144144
PCRE2_SIZE offset = 0;
145145

@@ -183,14 +183,14 @@ std::list<SMatch> Regex::searchAll(const std::string& s) const {
183183
}
184184
} while (rc > 0);
185185

186-
#ifdef WITH_PCRE2
186+
#ifndef WITH_PCRE
187187
pcre2_match_data_free(match_data);
188188
#endif
189189
return retList;
190190
}
191191

192192
RegexResult Regex::searchOneMatch(const std::string& s, std::vector<SMatchCapture>& captures, unsigned long match_limit) const {
193-
#ifdef WITH_PCRE2
193+
#ifndef WITH_PCRE
194194
Pcre2MatchContextPtr match_context;
195195
if (match_limit > 0) {
196196
// TODO: What if setting the match limit fails?
@@ -235,15 +235,15 @@ RegexResult Regex::searchOneMatch(const std::string& s, std::vector<SMatchCaptur
235235
captures.push_back(capture);
236236
}
237237

238-
#ifdef WITH_PCRE2
238+
#ifndef WITH_PCRE
239239
pcre2_match_data_free(match_data);
240240
#endif
241241
return to_regex_result(rc);
242242
}
243243

244244
RegexResult Regex::searchGlobal(const std::string& s, std::vector<SMatchCapture>& captures, unsigned long match_limit) const {
245245
bool prev_match_zero_length = false;
246-
#ifdef WITH_PCRE2
246+
#ifndef WITH_PCRE
247247
Pcre2MatchContextPtr match_context;
248248
if (match_limit > 0) {
249249
// TODO: What if setting the match limit fails?
@@ -337,14 +337,14 @@ RegexResult Regex::searchGlobal(const std::string& s, std::vector<SMatchCapture>
337337
}
338338
}
339339

340-
#ifdef WITH_PCRE2
340+
#ifndef WITH_PCRE
341341
pcre2_match_data_free(match_data);
342342
#endif
343343
return RegexResult::Ok;
344344
}
345345

346346
int Regex::search(const std::string& s, SMatch *match) const {
347-
#ifdef WITH_PCRE2
347+
#ifndef WITH_PCRE
348348
PCRE2_SPTR pcre2_s = reinterpret_cast<PCRE2_SPTR>(s.c_str());
349349
pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, NULL);
350350
int ret = 0;
@@ -371,14 +371,14 @@ int Regex::search(const std::string& s, SMatch *match) const {
371371
0);
372372
}
373373

374-
#ifdef WITH_PCRE2
374+
#ifndef WITH_PCRE
375375
pcre2_match_data_free(match_data);
376376
#endif
377377
return ret;
378378
}
379379

380380
int Regex::search(const std::string& s) const {
381-
#ifdef WITH_PCRE2
381+
#ifndef WITH_PCRE
382382
PCRE2_SPTR pcre2_s = reinterpret_cast<PCRE2_SPTR>(s.c_str());
383383
pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, NULL);
384384
int rc = 0;
@@ -405,15 +405,15 @@ int Regex::search(const std::string& s) const {
405405
RegexResult Regex::to_regex_result(int pcre_exec_result) const {
406406
if (
407407
pcre_exec_result > 0 ||
408-
#ifdef WITH_PCRE2
408+
#ifndef WITH_PCRE
409409
pcre_exec_result == PCRE2_ERROR_NOMATCH
410410
#else
411411
pcre_exec_result == PCRE_ERROR_NOMATCH
412412
#endif
413413
) {
414414
return RegexResult::Ok;
415415
} else if(
416-
#ifdef WITH_PCRE2
416+
#ifndef WITH_PCRE
417417
pcre_exec_result == PCRE2_ERROR_MATCHLIMIT
418418
#else
419419
pcre_exec_result == PCRE_ERROR_MATCHLIMIT

Diff for: src/utils/regex.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* directly using the email address [email protected].
1313
*
1414
*/
15-
#if WITH_PCRE2
15+
#ifndef WITH_PCRE
1616
#define PCRE2_CODE_UNIT_WIDTH 8
1717
#include <pcre2.h>
1818
#else
@@ -91,7 +91,7 @@ class Regex {
9191
private:
9292
RegexResult to_regex_result(int pcre_exec_result) const;
9393

94-
#if WITH_PCRE2
94+
#ifndef WITH_PCRE
9595
pcre2_code *m_pc;
9696
int m_pcje;
9797
#else

0 commit comments

Comments
 (0)