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>
6670
6771#include " xml.h"
6872
69- #ifdef HAVE_RULES
70- #ifdef _WIN32
71- #define PCRE_STATIC
72- #endif
73- #include < pcre.h>
74- #endif
75-
7673class SymbolDatabase ;
7774
7875static constexpr char Version[] = CPPCHECK_VERSION_STRING;
@@ -1143,135 +1140,6 @@ bool CppCheck::hasRule(const std::string &tokenlist) const
11431140 });
11441141}
11451142
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-
12751143void CppCheck::executeRules (const std::string &tokenlist, const TokenList &list)
12761144{
12771145 // There is no rule to execute
@@ -1293,73 +1161,7 @@ void CppCheck::executeRules(const std::string &tokenlist, const TokenList &list)
12931161 reportOut (" Processing rule: " + rule.pattern , Color::FgGreen);
12941162 }
12951163
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-
1164+ auto f = [&](int pos1, int pos2) {
13631165 // determine location..
13641166 int fileIndex = 0 ;
13651167 int line = 0 ;
@@ -1388,15 +1190,21 @@ void CppCheck::executeRules(const std::string &tokenlist, const TokenList &list)
13881190
13891191 // Report error
13901192 reportErr (errmsg);
1391- }
1193+ };
1194+
1195+ assert (rule.regex );
1196+
1197+ const std::string err = rule.regex ->match (str, f);
1198+ if (!err.empty ()) {
1199+ const ErrorMessage errmsg (std::list<ErrorMessage::FileLocation>(),
1200+ emptyString,
1201+ Severity::error,
1202+ err,
1203+ " pcre_exec" ,
1204+ Certainty::normal);
13921205
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);
1206+ reportErr (errmsg);
13981207 }
1399- #endif
14001208 }
14011209}
14021210#endif
0 commit comments