Skip to content

Commit ef932b5

Browse files
committed
compile --rule-file pattern only once / extracted regular expressions code to separate file
1 parent 3967c33 commit ef932b5

11 files changed

Lines changed: 766 additions & 389 deletions

File tree

Makefile

Lines changed: 137 additions & 129 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
@@ -57,6 +57,8 @@
5757
#include <utility>
5858

5959
#ifdef HAVE_RULES
60+
#include "regex.h"
61+
6062
// xml is used for rules
6163
#include "xml.h"
6264
#endif
@@ -1270,6 +1272,13 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
12701272
return Result::Fail;
12711273
}
12721274

1275+
Regex regex;
1276+
const std::string regex_err = regex.compile(rule.pattern);
1277+
if (!regex_err.empty()) {
1278+
mLogger.printError("failed to compile rule pattern '" + rule.pattern + "' (" + regex_err + ").");
1279+
return Result::Fail;
1280+
}
1281+
rule.regex = std::move(regex);
12731282
mSettings.rules.emplace_back(std::move(rule));
12741283
#else
12751284
mLogger.printError("Option --rule cannot be used as Cppcheck has not been built with rules support.");
@@ -1347,6 +1356,12 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
13471356
return Result::Fail;
13481357
}
13491358

1359+
const std::string regex_err = rule.regex.compile(rule.pattern);
1360+
if (!regex_err.empty()) {
1361+
mLogger.printError("unable to load rule-file '" + ruleFile + "' - pattern '" + rule.pattern + "' failed to compile (" + regex_err + ").");
1362+
return Result::Fail;
1363+
}
1364+
13501365
if (rule.severity == Severity::none) {
13511366
mLogger.printError("unable to load rule-file '" + ruleFile + "' - a rule has an invalid severity.");
13521367
return Result::Fail;

lib/cppcheck.cpp

Lines changed: 17 additions & 212 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@
4444
#include "valueflow.h"
4545
#include "version.h"
4646

47+
#ifdef HAVE_RULES
48+
#include "regex.h"
49+
#endif
50+
4751
#include <algorithm>
4852
#include <cassert>
4953
#include <cstdio>
@@ -66,17 +70,9 @@
6670
#include <vector>
6771

6872
#include "json.h"
69-
70-
#include <simplecpp.h>
71-
7273
#include "xml.h"
7374

74-
#ifdef HAVE_RULES
75-
#ifdef _WIN32
76-
#define PCRE_STATIC
77-
#endif
78-
#include <pcre.h>
79-
#endif
75+
#include <simplecpp.h>
8076

8177
class SymbolDatabase;
8278

@@ -1442,135 +1438,6 @@ bool CppCheck::hasRule(const std::string &tokenlist) const
14421438
});
14431439
}
14441440

1445-
static const char * pcreErrorCodeToString(const int pcreExecRet)
1446-
{
1447-
switch (pcreExecRet) {
1448-
case PCRE_ERROR_NULL:
1449-
return "Either code or subject was passed as NULL, or ovector was NULL "
1450-
"and ovecsize was not zero (PCRE_ERROR_NULL)";
1451-
case PCRE_ERROR_BADOPTION:
1452-
return "An unrecognized bit was set in the options argument (PCRE_ERROR_BADOPTION)";
1453-
case PCRE_ERROR_BADMAGIC:
1454-
return "PCRE stores a 4-byte \"magic number\" at the start of the compiled code, "
1455-
"to catch the case when it is passed a junk pointer and to detect when a "
1456-
"pattern that was compiled in an environment of one endianness is run in "
1457-
"an environment with the other endianness. This is the error that PCRE "
1458-
"gives when the magic number is not present (PCRE_ERROR_BADMAGIC)";
1459-
case PCRE_ERROR_UNKNOWN_NODE:
1460-
return "While running the pattern match, an unknown item was encountered in the "
1461-
"compiled pattern. This error could be caused by a bug in PCRE or by "
1462-
"overwriting of the compiled pattern (PCRE_ERROR_UNKNOWN_NODE)";
1463-
case PCRE_ERROR_NOMEMORY:
1464-
return "If a pattern contains back references, but the ovector that is passed "
1465-
"to pcre_exec() is not big enough to remember the referenced substrings, "
1466-
"PCRE gets a block of memory at the start of matching to use for this purpose. "
1467-
"If the call via pcre_malloc() fails, this error is given. The memory is "
1468-
"automatically freed at the end of matching. This error is also given if "
1469-
"pcre_stack_malloc() fails in pcre_exec(). "
1470-
"This can happen only when PCRE has been compiled with "
1471-
"--disable-stack-for-recursion (PCRE_ERROR_NOMEMORY)";
1472-
case PCRE_ERROR_NOSUBSTRING:
1473-
return "This error is used by the pcre_copy_substring(), pcre_get_substring(), "
1474-
"and pcre_get_substring_list() functions (see below). "
1475-
"It is never returned by pcre_exec() (PCRE_ERROR_NOSUBSTRING)";
1476-
case PCRE_ERROR_MATCHLIMIT:
1477-
return "The backtracking limit, as specified by the match_limit field in a pcre_extra "
1478-
"structure (or defaulted) was reached. "
1479-
"See the description above (PCRE_ERROR_MATCHLIMIT)";
1480-
case PCRE_ERROR_CALLOUT:
1481-
return "This error is never generated by pcre_exec() itself. "
1482-
"It is provided for use by callout functions that want to yield a distinctive "
1483-
"error code. See the pcrecallout documentation for details (PCRE_ERROR_CALLOUT)";
1484-
case PCRE_ERROR_BADUTF8:
1485-
return "A string that contains an invalid UTF-8 byte sequence was passed as a subject, "
1486-
"and the PCRE_NO_UTF8_CHECK option was not set. If the size of the output vector "
1487-
"(ovecsize) is at least 2, the byte offset to the start of the the invalid UTF-8 "
1488-
"character is placed in the first element, and a reason code is placed in the "
1489-
"second element. The reason codes are listed in the following section. For "
1490-
"backward compatibility, if PCRE_PARTIAL_HARD is set and the problem is a truncated "
1491-
"UTF-8 character at the end of the subject (reason codes 1 to 5), "
1492-
"PCRE_ERROR_SHORTUTF8 is returned instead of PCRE_ERROR_BADUTF8";
1493-
case PCRE_ERROR_BADUTF8_OFFSET:
1494-
return "The UTF-8 byte sequence that was passed as a subject was checked and found to "
1495-
"be valid (the PCRE_NO_UTF8_CHECK option was not set), but the value of "
1496-
"startoffset did not point to the beginning of a UTF-8 character or the end of "
1497-
"the subject (PCRE_ERROR_BADUTF8_OFFSET)";
1498-
case PCRE_ERROR_PARTIAL:
1499-
return "The subject string did not match, but it did match partially. See the "
1500-
"pcrepartial documentation for details of partial matching (PCRE_ERROR_PARTIAL)";
1501-
case PCRE_ERROR_BADPARTIAL:
1502-
return "This code is no longer in use. It was formerly returned when the PCRE_PARTIAL "
1503-
"option was used with a compiled pattern containing items that were not supported "
1504-
"for partial matching. From release 8.00 onwards, there are no restrictions on "
1505-
"partial matching (PCRE_ERROR_BADPARTIAL)";
1506-
case PCRE_ERROR_INTERNAL:
1507-
return "An unexpected internal error has occurred. This error could be caused by a bug "
1508-
"in PCRE or by overwriting of the compiled pattern (PCRE_ERROR_INTERNAL)";
1509-
case PCRE_ERROR_BADCOUNT:
1510-
return "This error is given if the value of the ovecsize argument is negative "
1511-
"(PCRE_ERROR_BADCOUNT)";
1512-
case PCRE_ERROR_RECURSIONLIMIT:
1513-
return "The internal recursion limit, as specified by the match_limit_recursion "
1514-
"field in a pcre_extra structure (or defaulted) was reached. "
1515-
"See the description above (PCRE_ERROR_RECURSIONLIMIT)";
1516-
case PCRE_ERROR_DFA_UITEM:
1517-
return "PCRE_ERROR_DFA_UITEM";
1518-
case PCRE_ERROR_DFA_UCOND:
1519-
return "PCRE_ERROR_DFA_UCOND";
1520-
case PCRE_ERROR_DFA_WSSIZE:
1521-
return "PCRE_ERROR_DFA_WSSIZE";
1522-
case PCRE_ERROR_DFA_RECURSE:
1523-
return "PCRE_ERROR_DFA_RECURSE";
1524-
case PCRE_ERROR_NULLWSLIMIT:
1525-
return "PCRE_ERROR_NULLWSLIMIT";
1526-
case PCRE_ERROR_BADNEWLINE:
1527-
return "An invalid combination of PCRE_NEWLINE_xxx options was "
1528-
"given (PCRE_ERROR_BADNEWLINE)";
1529-
case PCRE_ERROR_BADOFFSET:
1530-
return "The value of startoffset was negative or greater than the length "
1531-
"of the subject, that is, the value in length (PCRE_ERROR_BADOFFSET)";
1532-
case PCRE_ERROR_SHORTUTF8:
1533-
return "This error is returned instead of PCRE_ERROR_BADUTF8 when the subject "
1534-
"string ends with a truncated UTF-8 character and the PCRE_PARTIAL_HARD option is set. "
1535-
"Information about the failure is returned as for PCRE_ERROR_BADUTF8. "
1536-
"It is in fact sufficient to detect this case, but this special error code for "
1537-
"PCRE_PARTIAL_HARD precedes the implementation of returned information; "
1538-
"it is retained for backwards compatibility (PCRE_ERROR_SHORTUTF8)";
1539-
case PCRE_ERROR_RECURSELOOP:
1540-
return "This error is returned when pcre_exec() detects a recursion loop "
1541-
"within the pattern. Specifically, it means that either the whole pattern "
1542-
"or a subpattern has been called recursively for the second time at the same "
1543-
"position in the subject string. Some simple patterns that might do this "
1544-
"are detected and faulted at compile time, but more complicated cases, "
1545-
"in particular mutual recursions between two different subpatterns, "
1546-
"cannot be detected until run time (PCRE_ERROR_RECURSELOOP)";
1547-
case PCRE_ERROR_JIT_STACKLIMIT:
1548-
return "This error is returned when a pattern that was successfully studied "
1549-
"using a JIT compile option is being matched, but the memory available "
1550-
"for the just-in-time processing stack is not large enough. See the pcrejit "
1551-
"documentation for more details (PCRE_ERROR_JIT_STACKLIMIT)";
1552-
case PCRE_ERROR_BADMODE:
1553-
return "This error is given if a pattern that was compiled by the 8-bit library "
1554-
"is passed to a 16-bit or 32-bit library function, or vice versa (PCRE_ERROR_BADMODE)";
1555-
case PCRE_ERROR_BADENDIANNESS:
1556-
return "This error is given if a pattern that was compiled and saved is reloaded on a "
1557-
"host with different endianness. The utility function pcre_pattern_to_host_byte_order() "
1558-
"can be used to convert such a pattern so that it runs on the new host (PCRE_ERROR_BADENDIANNESS)";
1559-
case PCRE_ERROR_DFA_BADRESTART:
1560-
return "PCRE_ERROR_DFA_BADRESTART";
1561-
#if PCRE_MAJOR >= 8 && PCRE_MINOR >= 32
1562-
case PCRE_ERROR_BADLENGTH:
1563-
return "This error is given if pcre_exec() is called with a negative value for the length argument (PCRE_ERROR_BADLENGTH)";
1564-
case PCRE_ERROR_JIT_BADOPTION:
1565-
return "This error is returned when a pattern that was successfully studied using a JIT compile "
1566-
"option is being matched, but the matching mode (partial or complete match) does not correspond "
1567-
"to any JIT compilation mode. When the JIT fast path function is used, this error may be "
1568-
"also given for invalid options. See the pcrejit documentation for more details (PCRE_ERROR_JIT_BADOPTION)";
1569-
#endif
1570-
}
1571-
return "";
1572-
}
1573-
15741441
void CppCheck::executeRules(const std::string &tokenlist, const TokenList &list)
15751442
{
15761443
// There is no rule to execute
@@ -1592,73 +1459,7 @@ void CppCheck::executeRules(const std::string &tokenlist, const TokenList &list)
15921459
mErrorLogger.reportOut("Processing rule: " + rule.pattern, Color::FgGreen);
15931460
}
15941461

1595-
const char *pcreCompileErrorStr = nullptr;
1596-
int erroffset = 0;
1597-
pcre * const re = pcre_compile(rule.pattern.c_str(),0,&pcreCompileErrorStr,&erroffset,nullptr);
1598-
if (!re) {
1599-
if (pcreCompileErrorStr) {
1600-
const std::string msg = "pcre_compile failed: " + std::string(pcreCompileErrorStr);
1601-
const ErrorMessage errmsg({},
1602-
"",
1603-
Severity::error,
1604-
msg,
1605-
"pcre_compile",
1606-
Certainty::normal);
1607-
1608-
mErrorLogger.reportErr(errmsg);
1609-
}
1610-
continue;
1611-
}
1612-
1613-
// Optimize the regex, but only if PCRE_CONFIG_JIT is available
1614-
#ifdef PCRE_CONFIG_JIT
1615-
const char *pcreStudyErrorStr = nullptr;
1616-
pcre_extra * const pcreExtra = pcre_study(re, PCRE_STUDY_JIT_COMPILE, &pcreStudyErrorStr);
1617-
// pcre_study() returns NULL for both errors and when it can not optimize the regex.
1618-
// The last argument is how one checks for errors.
1619-
// It is NULL if everything works, and points to an error string otherwise.
1620-
if (pcreStudyErrorStr) {
1621-
const std::string msg = "pcre_study failed: " + std::string(pcreStudyErrorStr);
1622-
const ErrorMessage errmsg({},
1623-
"",
1624-
Severity::error,
1625-
msg,
1626-
"pcre_study",
1627-
Certainty::normal);
1628-
1629-
mErrorLogger.reportErr(errmsg);
1630-
// pcre_compile() worked, but pcre_study() returned an error. Free the resources allocated by pcre_compile().
1631-
pcre_free(re);
1632-
continue;
1633-
}
1634-
#else
1635-
const pcre_extra * const pcreExtra = nullptr;
1636-
#endif
1637-
1638-
int pos = 0;
1639-
int ovector[30]= {0};
1640-
while (pos < static_cast<int>(str.size())) {
1641-
const int pcreExecRet = pcre_exec(re, pcreExtra, str.c_str(), static_cast<int>(str.size()), pos, 0, ovector, 30);
1642-
if (pcreExecRet < 0) {
1643-
const std::string errorMessage = pcreErrorCodeToString(pcreExecRet);
1644-
if (!errorMessage.empty()) {
1645-
const ErrorMessage errmsg({},
1646-
"",
1647-
Severity::error,
1648-
std::string("pcre_exec failed: ") + errorMessage,
1649-
"pcre_exec",
1650-
Certainty::normal);
1651-
1652-
mErrorLogger.reportErr(errmsg);
1653-
}
1654-
break;
1655-
}
1656-
const auto pos1 = static_cast<unsigned int>(ovector[0]);
1657-
const auto pos2 = static_cast<unsigned int>(ovector[1]);
1658-
1659-
// jump to the end of the match for the next pcre_exec
1660-
pos = static_cast<int>(pos2);
1661-
1462+
auto f = [&](int pos1, int pos2) {
16621463
// determine location..
16631464
int fileIndex = 0;
16641465
int line = 0;
@@ -1687,15 +1488,19 @@ void CppCheck::executeRules(const std::string &tokenlist, const TokenList &list)
16871488

16881489
// Report error
16891490
mErrorLogger.reportErr(errmsg);
1690-
}
1491+
};
16911492

1692-
pcre_free(re);
1693-
#ifdef PCRE_CONFIG_JIT
1694-
// Free up the EXTRA PCRE value (may be NULL at this point)
1695-
if (pcreExtra) {
1696-
pcre_free_study(pcreExtra);
1493+
const std::string err = rule.regex.match(str, f);
1494+
if (!err.empty()) {
1495+
const ErrorMessage errmsg(std::list<ErrorMessage::FileLocation>(),
1496+
emptyString,
1497+
Severity::error,
1498+
err,
1499+
"pcre_exec",
1500+
Certainty::normal);
1501+
1502+
mErrorLogger.reportErr(errmsg);
16971503
}
1698-
#endif
16991504
}
17001505
}
17011506
#endif

lib/cppcheck.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
<ClCompile Include="platform.cpp" />
8080
<ClCompile Include="preprocessor.cpp" />
8181
<ClCompile Include="programmemory.cpp" />
82+
<ClCompile Include="regex.cpp" />
8283
<ClCompile Include="reverseanalyzer.cpp" />
8384
<ClCompile Include="settings.cpp" />
8485
<ClCompile Include="standards.cpp" />
@@ -155,6 +156,7 @@
155156
<ClInclude Include="precompiled.h" />
156157
<ClInclude Include="preprocessor.h" />
157158
<ClInclude Include="programmemory.h" />
159+
<ClInclude Include="regex.h" />
158160
<ClInclude Include="reverseanalyzer.h" />
159161
<ClInclude Include="settings.h" />
160162
<ClInclude Include="smallvector.h" />

0 commit comments

Comments
 (0)