Skip to content

Commit 2ed6ba0

Browse files
committed
compile --rule-file pattern only once / extracted regular expressions code to separate file
1 parent c2a8e1f commit 2ed6ba0

File tree

12 files changed

+739
-400
lines changed

12 files changed

+739
-400
lines changed

Makefile

Lines changed: 141 additions & 133 deletions
Large diffs are not rendered by default.

cli/cmdlineparser.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
#include <utility>
5555

5656
#ifdef HAVE_RULES
57+
#include "regex.h"
58+
5759
// xml is used for rules
5860
#include "xml.h"
5961
#endif
@@ -1147,6 +1149,13 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
11471149
return Result::Fail;
11481150
}
11491151

1152+
Regex regex;
1153+
const std::string regex_err = regex.compile(rule.pattern);
1154+
if (!regex_err.empty()) {
1155+
mLogger.printError("failed to compile rule pattern '" + rule.pattern + "' (" + regex_err + ").");
1156+
return Result::Fail;
1157+
}
1158+
rule.regex = std::move(regex);
11501159
mSettings.rules.emplace_back(std::move(rule));
11511160
#else
11521161
mLogger.printError("Option --rule cannot be used as Cppcheck has not been built with rules support.");
@@ -1224,6 +1233,12 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
12241233
return Result::Fail;
12251234
}
12261235

1236+
const std::string regex_err = rule.regex.compile(rule.pattern);
1237+
if (!regex_err.empty()) {
1238+
mLogger.printError("unable to load rule-file '" + ruleFile + "' - pattern '" + rule.pattern + "' failed to compile (" + regex_err + ").");
1239+
return Result::Fail;
1240+
}
1241+
12271242
if (rule.severity == Severity::none) {
12281243
mLogger.printError("unable to load rule-file '" + ruleFile + "' - a rule has an invalid severity.");
12291244
return Result::Fail;

lib/cppcheck.cpp

Lines changed: 17 additions & 212 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@
4040
#include "valueflow.h"
4141
#include "version.h"
4242

43+
#ifdef HAVE_RULES
44+
#include "regex.h"
45+
#endif
46+
4347
#include <algorithm>
4448
#include <cassert>
4549
#include <cstdio>
@@ -61,17 +65,9 @@
6165
#include <vector>
6266

6367
#include "json.h"
64-
65-
#include <simplecpp.h>
66-
6768
#include "xml.h"
6869

69-
#ifdef HAVE_RULES
70-
#ifdef _WIN32
71-
#define PCRE_STATIC
72-
#endif
73-
#include <pcre.h>
74-
#endif
70+
#include <simplecpp.h>
7571

7672
class SymbolDatabase;
7773

@@ -1143,135 +1139,6 @@ bool CppCheck::hasRule(const std::string &tokenlist) const
11431139
});
11441140
}
11451141

1146-
static const char * pcreErrorCodeToString(const int pcreExecRet)
1147-
{
1148-
switch (pcreExecRet) {
1149-
case PCRE_ERROR_NULL:
1150-
return "Either code or subject was passed as NULL, or ovector was NULL "
1151-
"and ovecsize was not zero (PCRE_ERROR_NULL)";
1152-
case PCRE_ERROR_BADOPTION:
1153-
return "An unrecognized bit was set in the options argument (PCRE_ERROR_BADOPTION)";
1154-
case PCRE_ERROR_BADMAGIC:
1155-
return "PCRE stores a 4-byte \"magic number\" at the start of the compiled code, "
1156-
"to catch the case when it is passed a junk pointer and to detect when a "
1157-
"pattern that was compiled in an environment of one endianness is run in "
1158-
"an environment with the other endianness. This is the error that PCRE "
1159-
"gives when the magic number is not present (PCRE_ERROR_BADMAGIC)";
1160-
case PCRE_ERROR_UNKNOWN_NODE:
1161-
return "While running the pattern match, an unknown item was encountered in the "
1162-
"compiled pattern. This error could be caused by a bug in PCRE or by "
1163-
"overwriting of the compiled pattern (PCRE_ERROR_UNKNOWN_NODE)";
1164-
case PCRE_ERROR_NOMEMORY:
1165-
return "If a pattern contains back references, but the ovector that is passed "
1166-
"to pcre_exec() is not big enough to remember the referenced substrings, "
1167-
"PCRE gets a block of memory at the start of matching to use for this purpose. "
1168-
"If the call via pcre_malloc() fails, this error is given. The memory is "
1169-
"automatically freed at the end of matching. This error is also given if "
1170-
"pcre_stack_malloc() fails in pcre_exec(). "
1171-
"This can happen only when PCRE has been compiled with "
1172-
"--disable-stack-for-recursion (PCRE_ERROR_NOMEMORY)";
1173-
case PCRE_ERROR_NOSUBSTRING:
1174-
return "This error is used by the pcre_copy_substring(), pcre_get_substring(), "
1175-
"and pcre_get_substring_list() functions (see below). "
1176-
"It is never returned by pcre_exec() (PCRE_ERROR_NOSUBSTRING)";
1177-
case PCRE_ERROR_MATCHLIMIT:
1178-
return "The backtracking limit, as specified by the match_limit field in a pcre_extra "
1179-
"structure (or defaulted) was reached. "
1180-
"See the description above (PCRE_ERROR_MATCHLIMIT)";
1181-
case PCRE_ERROR_CALLOUT:
1182-
return "This error is never generated by pcre_exec() itself. "
1183-
"It is provided for use by callout functions that want to yield a distinctive "
1184-
"error code. See the pcrecallout documentation for details (PCRE_ERROR_CALLOUT)";
1185-
case PCRE_ERROR_BADUTF8:
1186-
return "A string that contains an invalid UTF-8 byte sequence was passed as a subject, "
1187-
"and the PCRE_NO_UTF8_CHECK option was not set. If the size of the output vector "
1188-
"(ovecsize) is at least 2, the byte offset to the start of the the invalid UTF-8 "
1189-
"character is placed in the first element, and a reason code is placed in the "
1190-
"second element. The reason codes are listed in the following section. For "
1191-
"backward compatibility, if PCRE_PARTIAL_HARD is set and the problem is a truncated "
1192-
"UTF-8 character at the end of the subject (reason codes 1 to 5), "
1193-
"PCRE_ERROR_SHORTUTF8 is returned instead of PCRE_ERROR_BADUTF8";
1194-
case PCRE_ERROR_BADUTF8_OFFSET:
1195-
return "The UTF-8 byte sequence that was passed as a subject was checked and found to "
1196-
"be valid (the PCRE_NO_UTF8_CHECK option was not set), but the value of "
1197-
"startoffset did not point to the beginning of a UTF-8 character or the end of "
1198-
"the subject (PCRE_ERROR_BADUTF8_OFFSET)";
1199-
case PCRE_ERROR_PARTIAL:
1200-
return "The subject string did not match, but it did match partially. See the "
1201-
"pcrepartial documentation for details of partial matching (PCRE_ERROR_PARTIAL)";
1202-
case PCRE_ERROR_BADPARTIAL:
1203-
return "This code is no longer in use. It was formerly returned when the PCRE_PARTIAL "
1204-
"option was used with a compiled pattern containing items that were not supported "
1205-
"for partial matching. From release 8.00 onwards, there are no restrictions on "
1206-
"partial matching (PCRE_ERROR_BADPARTIAL)";
1207-
case PCRE_ERROR_INTERNAL:
1208-
return "An unexpected internal error has occurred. This error could be caused by a bug "
1209-
"in PCRE or by overwriting of the compiled pattern (PCRE_ERROR_INTERNAL)";
1210-
case PCRE_ERROR_BADCOUNT:
1211-
return "This error is given if the value of the ovecsize argument is negative "
1212-
"(PCRE_ERROR_BADCOUNT)";
1213-
case PCRE_ERROR_RECURSIONLIMIT:
1214-
return "The internal recursion limit, as specified by the match_limit_recursion "
1215-
"field in a pcre_extra structure (or defaulted) was reached. "
1216-
"See the description above (PCRE_ERROR_RECURSIONLIMIT)";
1217-
case PCRE_ERROR_DFA_UITEM:
1218-
return "PCRE_ERROR_DFA_UITEM";
1219-
case PCRE_ERROR_DFA_UCOND:
1220-
return "PCRE_ERROR_DFA_UCOND";
1221-
case PCRE_ERROR_DFA_WSSIZE:
1222-
return "PCRE_ERROR_DFA_WSSIZE";
1223-
case PCRE_ERROR_DFA_RECURSE:
1224-
return "PCRE_ERROR_DFA_RECURSE";
1225-
case PCRE_ERROR_NULLWSLIMIT:
1226-
return "PCRE_ERROR_NULLWSLIMIT";
1227-
case PCRE_ERROR_BADNEWLINE:
1228-
return "An invalid combination of PCRE_NEWLINE_xxx options was "
1229-
"given (PCRE_ERROR_BADNEWLINE)";
1230-
case PCRE_ERROR_BADOFFSET:
1231-
return "The value of startoffset was negative or greater than the length "
1232-
"of the subject, that is, the value in length (PCRE_ERROR_BADOFFSET)";
1233-
case PCRE_ERROR_SHORTUTF8:
1234-
return "This error is returned instead of PCRE_ERROR_BADUTF8 when the subject "
1235-
"string ends with a truncated UTF-8 character and the PCRE_PARTIAL_HARD option is set. "
1236-
"Information about the failure is returned as for PCRE_ERROR_BADUTF8. "
1237-
"It is in fact sufficient to detect this case, but this special error code for "
1238-
"PCRE_PARTIAL_HARD precedes the implementation of returned information; "
1239-
"it is retained for backwards compatibility (PCRE_ERROR_SHORTUTF8)";
1240-
case PCRE_ERROR_RECURSELOOP:
1241-
return "This error is returned when pcre_exec() detects a recursion loop "
1242-
"within the pattern. Specifically, it means that either the whole pattern "
1243-
"or a subpattern has been called recursively for the second time at the same "
1244-
"position in the subject string. Some simple patterns that might do this "
1245-
"are detected and faulted at compile time, but more complicated cases, "
1246-
"in particular mutual recursions between two different subpatterns, "
1247-
"cannot be detected until run time (PCRE_ERROR_RECURSELOOP)";
1248-
case PCRE_ERROR_JIT_STACKLIMIT:
1249-
return "This error is returned when a pattern that was successfully studied "
1250-
"using a JIT compile option is being matched, but the memory available "
1251-
"for the just-in-time processing stack is not large enough. See the pcrejit "
1252-
"documentation for more details (PCRE_ERROR_JIT_STACKLIMIT)";
1253-
case PCRE_ERROR_BADMODE:
1254-
return "This error is given if a pattern that was compiled by the 8-bit library "
1255-
"is passed to a 16-bit or 32-bit library function, or vice versa (PCRE_ERROR_BADMODE)";
1256-
case PCRE_ERROR_BADENDIANNESS:
1257-
return "This error is given if a pattern that was compiled and saved is reloaded on a "
1258-
"host with different endianness. The utility function pcre_pattern_to_host_byte_order() "
1259-
"can be used to convert such a pattern so that it runs on the new host (PCRE_ERROR_BADENDIANNESS)";
1260-
case PCRE_ERROR_DFA_BADRESTART:
1261-
return "PCRE_ERROR_DFA_BADRESTART";
1262-
#if PCRE_MAJOR >= 8 && PCRE_MINOR >= 32
1263-
case PCRE_ERROR_BADLENGTH:
1264-
return "This error is given if pcre_exec() is called with a negative value for the length argument (PCRE_ERROR_BADLENGTH)";
1265-
case PCRE_ERROR_JIT_BADOPTION:
1266-
return "This error is returned when a pattern that was successfully studied using a JIT compile "
1267-
"option is being matched, but the matching mode (partial or complete match) does not correspond "
1268-
"to any JIT compilation mode. When the JIT fast path function is used, this error may be "
1269-
"also given for invalid options. See the pcrejit documentation for more details (PCRE_ERROR_JIT_BADOPTION)";
1270-
#endif
1271-
}
1272-
return "";
1273-
}
1274-
12751142
void CppCheck::executeRules(const std::string &tokenlist, const TokenList &list)
12761143
{
12771144
// There is no rule to execute
@@ -1293,73 +1160,7 @@ void CppCheck::executeRules(const std::string &tokenlist, const TokenList &list)
12931160
reportOut("Processing rule: " + rule.pattern, Color::FgGreen);
12941161
}
12951162

1296-
const char *pcreCompileErrorStr = nullptr;
1297-
int erroffset = 0;
1298-
pcre * const re = pcre_compile(rule.pattern.c_str(),0,&pcreCompileErrorStr,&erroffset,nullptr);
1299-
if (!re) {
1300-
if (pcreCompileErrorStr) {
1301-
const std::string msg = "pcre_compile failed: " + std::string(pcreCompileErrorStr);
1302-
const ErrorMessage errmsg(std::list<ErrorMessage::FileLocation>(),
1303-
emptyString,
1304-
Severity::error,
1305-
msg,
1306-
"pcre_compile",
1307-
Certainty::normal);
1308-
1309-
reportErr(errmsg);
1310-
}
1311-
continue;
1312-
}
1313-
1314-
// Optimize the regex, but only if PCRE_CONFIG_JIT is available
1315-
#ifdef PCRE_CONFIG_JIT
1316-
const char *pcreStudyErrorStr = nullptr;
1317-
pcre_extra * const pcreExtra = pcre_study(re, PCRE_STUDY_JIT_COMPILE, &pcreStudyErrorStr);
1318-
// pcre_study() returns NULL for both errors and when it can not optimize the regex.
1319-
// The last argument is how one checks for errors.
1320-
// It is NULL if everything works, and points to an error string otherwise.
1321-
if (pcreStudyErrorStr) {
1322-
const std::string msg = "pcre_study failed: " + std::string(pcreStudyErrorStr);
1323-
const ErrorMessage errmsg(std::list<ErrorMessage::FileLocation>(),
1324-
emptyString,
1325-
Severity::error,
1326-
msg,
1327-
"pcre_study",
1328-
Certainty::normal);
1329-
1330-
reportErr(errmsg);
1331-
// pcre_compile() worked, but pcre_study() returned an error. Free the resources allocated by pcre_compile().
1332-
pcre_free(re);
1333-
continue;
1334-
}
1335-
#else
1336-
const pcre_extra * const pcreExtra = nullptr;
1337-
#endif
1338-
1339-
int pos = 0;
1340-
int ovector[30]= {0};
1341-
while (pos < (int)str.size()) {
1342-
const int pcreExecRet = pcre_exec(re, pcreExtra, str.c_str(), (int)str.size(), pos, 0, ovector, 30);
1343-
if (pcreExecRet < 0) {
1344-
const std::string errorMessage = pcreErrorCodeToString(pcreExecRet);
1345-
if (!errorMessage.empty()) {
1346-
const ErrorMessage errmsg(std::list<ErrorMessage::FileLocation>(),
1347-
emptyString,
1348-
Severity::error,
1349-
std::string("pcre_exec failed: ") + errorMessage,
1350-
"pcre_exec",
1351-
Certainty::normal);
1352-
1353-
reportErr(errmsg);
1354-
}
1355-
break;
1356-
}
1357-
const auto pos1 = (unsigned int)ovector[0];
1358-
const auto pos2 = (unsigned int)ovector[1];
1359-
1360-
// jump to the end of the match for the next pcre_exec
1361-
pos = (int)pos2;
1362-
1163+
auto f = [&](int pos1, int pos2) {
13631164
// determine location..
13641165
int fileIndex = 0;
13651166
int line = 0;
@@ -1388,15 +1189,19 @@ void CppCheck::executeRules(const std::string &tokenlist, const TokenList &list)
13881189

13891190
// Report error
13901191
reportErr(errmsg);
1391-
}
1192+
};
1193+
1194+
const std::string err = rule.regex.match(str, f);
1195+
if (!err.empty()) {
1196+
const ErrorMessage errmsg(std::list<ErrorMessage::FileLocation>(),
1197+
emptyString,
1198+
Severity::error,
1199+
err,
1200+
"pcre_exec",
1201+
Certainty::normal);
13921202

1393-
pcre_free(re);
1394-
#ifdef PCRE_CONFIG_JIT
1395-
// Free up the EXTRA PCRE value (may be NULL at this point)
1396-
if (pcreExtra) {
1397-
pcre_free_study(pcreExtra);
1203+
reportErr(errmsg);
13981204
}
1399-
#endif
14001205
}
14011206
}
14021207
#endif

lib/cppcheck.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
<ClCompile Include="platform.cpp" />
7979
<ClCompile Include="preprocessor.cpp" />
8080
<ClCompile Include="programmemory.cpp" />
81+
<ClCompile Include="regex.cpp" />
8182
<ClCompile Include="reverseanalyzer.cpp" />
8283
<ClCompile Include="settings.cpp" />
8384
<ClCompile Include="summaries.cpp" />
@@ -178,6 +179,7 @@
178179
<ClInclude Include="precompiled.h" />
179180
<ClInclude Include="preprocessor.h" />
180181
<ClInclude Include="programmemory.h" />
182+
<ClInclude Include="regex.h" />
181183
<ClInclude Include="reverseanalyzer.h" />
182184
<ClInclude Include="settings.h" />
183185
<ClInclude Include="smallvector.h" />

lib/lib.pri

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ HEADERS += $${PWD}/addoninfo.h \
6161
$${PWD}/precompiled.h \
6262
$${PWD}/preprocessor.h \
6363
$${PWD}/programmemory.h \
64+
$${PWD}/regex.h \
6465
$${PWD}/reverseanalyzer.h \
6566
$${PWD}/settings.h \
6667
$${PWD}/smallvector.h \
@@ -164,6 +165,7 @@ SOURCES += $${PWD}/valueflow.cpp \
164165
$${PWD}/platform.cpp \
165166
$${PWD}/preprocessor.cpp \
166167
$${PWD}/programmemory.cpp \
168+
$${PWD}/regex.cpp \
167169
$${PWD}/reverseanalyzer.cpp \
168170
$${PWD}/settings.cpp \
169171
$${PWD}/summaries.cpp \

0 commit comments

Comments
 (0)