7272
7373#include " xml.h"
7474
75- #ifdef HAVE_RULES
76- #ifdef _WIN32
77- #define PCRE_STATIC
78- #endif
79- #include < pcre.h>
80- #endif
81-
8275class SymbolDatabase ;
8376
8477static constexpr char Version[] = CPPCHECK_VERSION_STRING;
@@ -1162,135 +1155,6 @@ bool CppCheck::hasRule(const std::string &tokenlist) const
11621155 });
11631156}
11641157
1165- static const char * pcreErrorCodeToString (const int pcreExecRet)
1166- {
1167- switch (pcreExecRet) {
1168- case PCRE_ERROR_NULL:
1169- return " Either code or subject was passed as NULL, or ovector was NULL "
1170- " and ovecsize was not zero (PCRE_ERROR_NULL)" ;
1171- case PCRE_ERROR_BADOPTION:
1172- return " An unrecognized bit was set in the options argument (PCRE_ERROR_BADOPTION)" ;
1173- case PCRE_ERROR_BADMAGIC:
1174- return " PCRE stores a 4-byte \" magic number\" at the start of the compiled code, "
1175- " to catch the case when it is passed a junk pointer and to detect when a "
1176- " pattern that was compiled in an environment of one endianness is run in "
1177- " an environment with the other endianness. This is the error that PCRE "
1178- " gives when the magic number is not present (PCRE_ERROR_BADMAGIC)" ;
1179- case PCRE_ERROR_UNKNOWN_NODE:
1180- return " While running the pattern match, an unknown item was encountered in the "
1181- " compiled pattern. This error could be caused by a bug in PCRE or by "
1182- " overwriting of the compiled pattern (PCRE_ERROR_UNKNOWN_NODE)" ;
1183- case PCRE_ERROR_NOMEMORY:
1184- return " If a pattern contains back references, but the ovector that is passed "
1185- " to pcre_exec() is not big enough to remember the referenced substrings, "
1186- " PCRE gets a block of memory at the start of matching to use for this purpose. "
1187- " If the call via pcre_malloc() fails, this error is given. The memory is "
1188- " automatically freed at the end of matching. This error is also given if "
1189- " pcre_stack_malloc() fails in pcre_exec(). "
1190- " This can happen only when PCRE has been compiled with "
1191- " --disable-stack-for-recursion (PCRE_ERROR_NOMEMORY)" ;
1192- case PCRE_ERROR_NOSUBSTRING:
1193- return " This error is used by the pcre_copy_substring(), pcre_get_substring(), "
1194- " and pcre_get_substring_list() functions (see below). "
1195- " It is never returned by pcre_exec() (PCRE_ERROR_NOSUBSTRING)" ;
1196- case PCRE_ERROR_MATCHLIMIT:
1197- return " The backtracking limit, as specified by the match_limit field in a pcre_extra "
1198- " structure (or defaulted) was reached. "
1199- " See the description above (PCRE_ERROR_MATCHLIMIT)" ;
1200- case PCRE_ERROR_CALLOUT:
1201- return " This error is never generated by pcre_exec() itself. "
1202- " It is provided for use by callout functions that want to yield a distinctive "
1203- " error code. See the pcrecallout documentation for details (PCRE_ERROR_CALLOUT)" ;
1204- case PCRE_ERROR_BADUTF8:
1205- return " A string that contains an invalid UTF-8 byte sequence was passed as a subject, "
1206- " and the PCRE_NO_UTF8_CHECK option was not set. If the size of the output vector "
1207- " (ovecsize) is at least 2, the byte offset to the start of the the invalid UTF-8 "
1208- " character is placed in the first element, and a reason code is placed in the "
1209- " second element. The reason codes are listed in the following section. For "
1210- " backward compatibility, if PCRE_PARTIAL_HARD is set and the problem is a truncated "
1211- " UTF-8 character at the end of the subject (reason codes 1 to 5), "
1212- " PCRE_ERROR_SHORTUTF8 is returned instead of PCRE_ERROR_BADUTF8" ;
1213- case PCRE_ERROR_BADUTF8_OFFSET:
1214- return " The UTF-8 byte sequence that was passed as a subject was checked and found to "
1215- " be valid (the PCRE_NO_UTF8_CHECK option was not set), but the value of "
1216- " startoffset did not point to the beginning of a UTF-8 character or the end of "
1217- " the subject (PCRE_ERROR_BADUTF8_OFFSET)" ;
1218- case PCRE_ERROR_PARTIAL:
1219- return " The subject string did not match, but it did match partially. See the "
1220- " pcrepartial documentation for details of partial matching (PCRE_ERROR_PARTIAL)" ;
1221- case PCRE_ERROR_BADPARTIAL:
1222- return " This code is no longer in use. It was formerly returned when the PCRE_PARTIAL "
1223- " option was used with a compiled pattern containing items that were not supported "
1224- " for partial matching. From release 8.00 onwards, there are no restrictions on "
1225- " partial matching (PCRE_ERROR_BADPARTIAL)" ;
1226- case PCRE_ERROR_INTERNAL:
1227- return " An unexpected internal error has occurred. This error could be caused by a bug "
1228- " in PCRE or by overwriting of the compiled pattern (PCRE_ERROR_INTERNAL)" ;
1229- case PCRE_ERROR_BADCOUNT:
1230- return " This error is given if the value of the ovecsize argument is negative "
1231- " (PCRE_ERROR_BADCOUNT)" ;
1232- case PCRE_ERROR_RECURSIONLIMIT:
1233- return " The internal recursion limit, as specified by the match_limit_recursion "
1234- " field in a pcre_extra structure (or defaulted) was reached. "
1235- " See the description above (PCRE_ERROR_RECURSIONLIMIT)" ;
1236- case PCRE_ERROR_DFA_UITEM:
1237- return " PCRE_ERROR_DFA_UITEM" ;
1238- case PCRE_ERROR_DFA_UCOND:
1239- return " PCRE_ERROR_DFA_UCOND" ;
1240- case PCRE_ERROR_DFA_WSSIZE:
1241- return " PCRE_ERROR_DFA_WSSIZE" ;
1242- case PCRE_ERROR_DFA_RECURSE:
1243- return " PCRE_ERROR_DFA_RECURSE" ;
1244- case PCRE_ERROR_NULLWSLIMIT:
1245- return " PCRE_ERROR_NULLWSLIMIT" ;
1246- case PCRE_ERROR_BADNEWLINE:
1247- return " An invalid combination of PCRE_NEWLINE_xxx options was "
1248- " given (PCRE_ERROR_BADNEWLINE)" ;
1249- case PCRE_ERROR_BADOFFSET:
1250- return " The value of startoffset was negative or greater than the length "
1251- " of the subject, that is, the value in length (PCRE_ERROR_BADOFFSET)" ;
1252- case PCRE_ERROR_SHORTUTF8:
1253- return " This error is returned instead of PCRE_ERROR_BADUTF8 when the subject "
1254- " string ends with a truncated UTF-8 character and the PCRE_PARTIAL_HARD option is set. "
1255- " Information about the failure is returned as for PCRE_ERROR_BADUTF8. "
1256- " It is in fact sufficient to detect this case, but this special error code for "
1257- " PCRE_PARTIAL_HARD precedes the implementation of returned information; "
1258- " it is retained for backwards compatibility (PCRE_ERROR_SHORTUTF8)" ;
1259- case PCRE_ERROR_RECURSELOOP:
1260- return " This error is returned when pcre_exec() detects a recursion loop "
1261- " within the pattern. Specifically, it means that either the whole pattern "
1262- " or a subpattern has been called recursively for the second time at the same "
1263- " position in the subject string. Some simple patterns that might do this "
1264- " are detected and faulted at compile time, but more complicated cases, "
1265- " in particular mutual recursions between two different subpatterns, "
1266- " cannot be detected until run time (PCRE_ERROR_RECURSELOOP)" ;
1267- case PCRE_ERROR_JIT_STACKLIMIT:
1268- return " This error is returned when a pattern that was successfully studied "
1269- " using a JIT compile option is being matched, but the memory available "
1270- " for the just-in-time processing stack is not large enough. See the pcrejit "
1271- " documentation for more details (PCRE_ERROR_JIT_STACKLIMIT)" ;
1272- case PCRE_ERROR_BADMODE:
1273- return " This error is given if a pattern that was compiled by the 8-bit library "
1274- " is passed to a 16-bit or 32-bit library function, or vice versa (PCRE_ERROR_BADMODE)" ;
1275- case PCRE_ERROR_BADENDIANNESS:
1276- return " This error is given if a pattern that was compiled and saved is reloaded on a "
1277- " host with different endianness. The utility function pcre_pattern_to_host_byte_order() "
1278- " can be used to convert such a pattern so that it runs on the new host (PCRE_ERROR_BADENDIANNESS)" ;
1279- case PCRE_ERROR_DFA_BADRESTART:
1280- return " PCRE_ERROR_DFA_BADRESTART" ;
1281- #if PCRE_MAJOR >= 8 && PCRE_MINOR >= 32
1282- case PCRE_ERROR_BADLENGTH:
1283- return " This error is given if pcre_exec() is called with a negative value for the length argument (PCRE_ERROR_BADLENGTH)" ;
1284- case PCRE_ERROR_JIT_BADOPTION:
1285- return " This error is returned when a pattern that was successfully studied using a JIT compile "
1286- " option is being matched, but the matching mode (partial or complete match) does not correspond "
1287- " to any JIT compilation mode. When the JIT fast path function is used, this error may be "
1288- " also given for invalid options. See the pcrejit documentation for more details (PCRE_ERROR_JIT_BADOPTION)" ;
1289- #endif
1290- }
1291- return " " ;
1292- }
1293-
12941158void CppCheck::executeRules (const std::string &tokenlist, const TokenList &list)
12951159{
12961160 // There is no rule to execute
@@ -1312,73 +1176,7 @@ void CppCheck::executeRules(const std::string &tokenlist, const TokenList &list)
13121176 reportOut (" Processing rule: " + rule.pattern , Color::FgGreen);
13131177 }
13141178
1315- const char *pcreCompileErrorStr = nullptr ;
1316- int erroffset = 0 ;
1317- pcre * const re = pcre_compile (rule.pattern .c_str (),0 ,&pcreCompileErrorStr,&erroffset,nullptr );
1318- if (!re) {
1319- if (pcreCompileErrorStr) {
1320- const std::string msg = " pcre_compile failed: " + std::string (pcreCompileErrorStr);
1321- const ErrorMessage errmsg (std::list<ErrorMessage::FileLocation>(),
1322- emptyString,
1323- Severity::error,
1324- msg,
1325- " pcre_compile" ,
1326- Certainty::normal);
1327-
1328- reportErr (errmsg);
1329- }
1330- continue ;
1331- }
1332-
1333- // Optimize the regex, but only if PCRE_CONFIG_JIT is available
1334- #ifdef PCRE_CONFIG_JIT
1335- const char *pcreStudyErrorStr = nullptr ;
1336- pcre_extra * const pcreExtra = pcre_study (re, PCRE_STUDY_JIT_COMPILE, &pcreStudyErrorStr);
1337- // pcre_study() returns NULL for both errors and when it can not optimize the regex.
1338- // The last argument is how one checks for errors.
1339- // It is NULL if everything works, and points to an error string otherwise.
1340- if (pcreStudyErrorStr) {
1341- const std::string msg = " pcre_study failed: " + std::string (pcreStudyErrorStr);
1342- const ErrorMessage errmsg (std::list<ErrorMessage::FileLocation>(),
1343- emptyString,
1344- Severity::error,
1345- msg,
1346- " pcre_study" ,
1347- Certainty::normal);
1348-
1349- reportErr (errmsg);
1350- // pcre_compile() worked, but pcre_study() returned an error. Free the resources allocated by pcre_compile().
1351- pcre_free (re);
1352- continue ;
1353- }
1354- #else
1355- const pcre_extra * const pcreExtra = nullptr ;
1356- #endif
1357-
1358- int pos = 0 ;
1359- int ovector[30 ]= {0 };
1360- while (pos < (int )str.size ()) {
1361- const int pcreExecRet = pcre_exec (re, pcreExtra, str.c_str (), (int )str.size (), pos, 0 , ovector, 30 );
1362- if (pcreExecRet < 0 ) {
1363- const std::string errorMessage = pcreErrorCodeToString (pcreExecRet);
1364- if (!errorMessage.empty ()) {
1365- const ErrorMessage errmsg (std::list<ErrorMessage::FileLocation>(),
1366- emptyString,
1367- Severity::error,
1368- std::string (" pcre_exec failed: " ) + errorMessage,
1369- " pcre_exec" ,
1370- Certainty::normal);
1371-
1372- reportErr (errmsg);
1373- }
1374- break ;
1375- }
1376- const auto pos1 = (unsigned int )ovector[0 ];
1377- const auto pos2 = (unsigned int )ovector[1 ];
1378-
1379- // jump to the end of the match for the next pcre_exec
1380- pos = (int )pos2;
1381-
1179+ auto f = [&](int pos1, int pos2) {
13821180 // determine location..
13831181 int fileIndex = 0 ;
13841182 int line = 0 ;
@@ -1407,15 +1205,21 @@ void CppCheck::executeRules(const std::string &tokenlist, const TokenList &list)
14071205
14081206 // Report error
14091207 reportErr (errmsg);
1410- }
1208+ };
1209+
1210+ assert (rule.regex );
14111211
1412- pcre_free (re);
1413- #ifdef PCRE_CONFIG_JIT
1414- // Free up the EXTRA PCRE value (may be NULL at this point)
1415- if (pcreExtra) {
1416- pcre_free_study (pcreExtra);
1212+ const std::string err = rule.regex ->match (str, f);
1213+ if (!err.empty ()) {
1214+ const ErrorMessage errmsg (std::list<ErrorMessage::FileLocation>(),
1215+ emptyString,
1216+ Severity::error,
1217+ err,
1218+ " pcre_exec" ,
1219+ Certainty::normal);
1220+
1221+ reportErr (errmsg);
14171222 }
1418- #endif
14191223 }
14201224}
14211225#endif
0 commit comments