6666
6767#include " xml.h"
6868
69- #ifdef HAVE_RULES
70- #ifdef _WIN32
71- #define PCRE_STATIC
72- #endif
73- #include < pcre.h>
74- #endif
75-
7669class SymbolDatabase ;
7770
7871static constexpr char Version[] = CPPCHECK_VERSION_STRING;
@@ -1121,135 +1114,6 @@ bool CppCheck::hasRule(const std::string &tokenlist) const
11211114 });
11221115}
11231116
1124- static const char * pcreErrorCodeToString (const int pcreExecRet)
1125- {
1126- switch (pcreExecRet) {
1127- case PCRE_ERROR_NULL:
1128- return " Either code or subject was passed as NULL, or ovector was NULL "
1129- " and ovecsize was not zero (PCRE_ERROR_NULL)" ;
1130- case PCRE_ERROR_BADOPTION:
1131- return " An unrecognized bit was set in the options argument (PCRE_ERROR_BADOPTION)" ;
1132- case PCRE_ERROR_BADMAGIC:
1133- return " PCRE stores a 4-byte \" magic number\" at the start of the compiled code, "
1134- " to catch the case when it is passed a junk pointer and to detect when a "
1135- " pattern that was compiled in an environment of one endianness is run in "
1136- " an environment with the other endianness. This is the error that PCRE "
1137- " gives when the magic number is not present (PCRE_ERROR_BADMAGIC)" ;
1138- case PCRE_ERROR_UNKNOWN_NODE:
1139- return " While running the pattern match, an unknown item was encountered in the "
1140- " compiled pattern. This error could be caused by a bug in PCRE or by "
1141- " overwriting of the compiled pattern (PCRE_ERROR_UNKNOWN_NODE)" ;
1142- case PCRE_ERROR_NOMEMORY:
1143- return " If a pattern contains back references, but the ovector that is passed "
1144- " to pcre_exec() is not big enough to remember the referenced substrings, "
1145- " PCRE gets a block of memory at the start of matching to use for this purpose. "
1146- " If the call via pcre_malloc() fails, this error is given. The memory is "
1147- " automatically freed at the end of matching. This error is also given if "
1148- " pcre_stack_malloc() fails in pcre_exec(). "
1149- " This can happen only when PCRE has been compiled with "
1150- " --disable-stack-for-recursion (PCRE_ERROR_NOMEMORY)" ;
1151- case PCRE_ERROR_NOSUBSTRING:
1152- return " This error is used by the pcre_copy_substring(), pcre_get_substring(), "
1153- " and pcre_get_substring_list() functions (see below). "
1154- " It is never returned by pcre_exec() (PCRE_ERROR_NOSUBSTRING)" ;
1155- case PCRE_ERROR_MATCHLIMIT:
1156- return " The backtracking limit, as specified by the match_limit field in a pcre_extra "
1157- " structure (or defaulted) was reached. "
1158- " See the description above (PCRE_ERROR_MATCHLIMIT)" ;
1159- case PCRE_ERROR_CALLOUT:
1160- return " This error is never generated by pcre_exec() itself. "
1161- " It is provided for use by callout functions that want to yield a distinctive "
1162- " error code. See the pcrecallout documentation for details (PCRE_ERROR_CALLOUT)" ;
1163- case PCRE_ERROR_BADUTF8:
1164- return " A string that contains an invalid UTF-8 byte sequence was passed as a subject, "
1165- " and the PCRE_NO_UTF8_CHECK option was not set. If the size of the output vector "
1166- " (ovecsize) is at least 2, the byte offset to the start of the the invalid UTF-8 "
1167- " character is placed in the first element, and a reason code is placed in the "
1168- " second element. The reason codes are listed in the following section. For "
1169- " backward compatibility, if PCRE_PARTIAL_HARD is set and the problem is a truncated "
1170- " UTF-8 character at the end of the subject (reason codes 1 to 5), "
1171- " PCRE_ERROR_SHORTUTF8 is returned instead of PCRE_ERROR_BADUTF8" ;
1172- case PCRE_ERROR_BADUTF8_OFFSET:
1173- return " The UTF-8 byte sequence that was passed as a subject was checked and found to "
1174- " be valid (the PCRE_NO_UTF8_CHECK option was not set), but the value of "
1175- " startoffset did not point to the beginning of a UTF-8 character or the end of "
1176- " the subject (PCRE_ERROR_BADUTF8_OFFSET)" ;
1177- case PCRE_ERROR_PARTIAL:
1178- return " The subject string did not match, but it did match partially. See the "
1179- " pcrepartial documentation for details of partial matching (PCRE_ERROR_PARTIAL)" ;
1180- case PCRE_ERROR_BADPARTIAL:
1181- return " This code is no longer in use. It was formerly returned when the PCRE_PARTIAL "
1182- " option was used with a compiled pattern containing items that were not supported "
1183- " for partial matching. From release 8.00 onwards, there are no restrictions on "
1184- " partial matching (PCRE_ERROR_BADPARTIAL)" ;
1185- case PCRE_ERROR_INTERNAL:
1186- return " An unexpected internal error has occurred. This error could be caused by a bug "
1187- " in PCRE or by overwriting of the compiled pattern (PCRE_ERROR_INTERNAL)" ;
1188- case PCRE_ERROR_BADCOUNT:
1189- return " This error is given if the value of the ovecsize argument is negative "
1190- " (PCRE_ERROR_BADCOUNT)" ;
1191- case PCRE_ERROR_RECURSIONLIMIT:
1192- return " The internal recursion limit, as specified by the match_limit_recursion "
1193- " field in a pcre_extra structure (or defaulted) was reached. "
1194- " See the description above (PCRE_ERROR_RECURSIONLIMIT)" ;
1195- case PCRE_ERROR_DFA_UITEM:
1196- return " PCRE_ERROR_DFA_UITEM" ;
1197- case PCRE_ERROR_DFA_UCOND:
1198- return " PCRE_ERROR_DFA_UCOND" ;
1199- case PCRE_ERROR_DFA_WSSIZE:
1200- return " PCRE_ERROR_DFA_WSSIZE" ;
1201- case PCRE_ERROR_DFA_RECURSE:
1202- return " PCRE_ERROR_DFA_RECURSE" ;
1203- case PCRE_ERROR_NULLWSLIMIT:
1204- return " PCRE_ERROR_NULLWSLIMIT" ;
1205- case PCRE_ERROR_BADNEWLINE:
1206- return " An invalid combination of PCRE_NEWLINE_xxx options was "
1207- " given (PCRE_ERROR_BADNEWLINE)" ;
1208- case PCRE_ERROR_BADOFFSET:
1209- return " The value of startoffset was negative or greater than the length "
1210- " of the subject, that is, the value in length (PCRE_ERROR_BADOFFSET)" ;
1211- case PCRE_ERROR_SHORTUTF8:
1212- return " This error is returned instead of PCRE_ERROR_BADUTF8 when the subject "
1213- " string ends with a truncated UTF-8 character and the PCRE_PARTIAL_HARD option is set. "
1214- " Information about the failure is returned as for PCRE_ERROR_BADUTF8. "
1215- " It is in fact sufficient to detect this case, but this special error code for "
1216- " PCRE_PARTIAL_HARD precedes the implementation of returned information; "
1217- " it is retained for backwards compatibility (PCRE_ERROR_SHORTUTF8)" ;
1218- case PCRE_ERROR_RECURSELOOP:
1219- return " This error is returned when pcre_exec() detects a recursion loop "
1220- " within the pattern. Specifically, it means that either the whole pattern "
1221- " or a subpattern has been called recursively for the second time at the same "
1222- " position in the subject string. Some simple patterns that might do this "
1223- " are detected and faulted at compile time, but more complicated cases, "
1224- " in particular mutual recursions between two different subpatterns, "
1225- " cannot be detected until run time (PCRE_ERROR_RECURSELOOP)" ;
1226- case PCRE_ERROR_JIT_STACKLIMIT:
1227- return " This error is returned when a pattern that was successfully studied "
1228- " using a JIT compile option is being matched, but the memory available "
1229- " for the just-in-time processing stack is not large enough. See the pcrejit "
1230- " documentation for more details (PCRE_ERROR_JIT_STACKLIMIT)" ;
1231- case PCRE_ERROR_BADMODE:
1232- return " This error is given if a pattern that was compiled by the 8-bit library "
1233- " is passed to a 16-bit or 32-bit library function, or vice versa (PCRE_ERROR_BADMODE)" ;
1234- case PCRE_ERROR_BADENDIANNESS:
1235- return " This error is given if a pattern that was compiled and saved is reloaded on a "
1236- " host with different endianness. The utility function pcre_pattern_to_host_byte_order() "
1237- " can be used to convert such a pattern so that it runs on the new host (PCRE_ERROR_BADENDIANNESS)" ;
1238- case PCRE_ERROR_DFA_BADRESTART:
1239- return " PCRE_ERROR_DFA_BADRESTART" ;
1240- #if PCRE_MAJOR >= 8 && PCRE_MINOR >= 32
1241- case PCRE_ERROR_BADLENGTH:
1242- return " This error is given if pcre_exec() is called with a negative value for the length argument (PCRE_ERROR_BADLENGTH)" ;
1243- case PCRE_ERROR_JIT_BADOPTION:
1244- return " This error is returned when a pattern that was successfully studied using a JIT compile "
1245- " option is being matched, but the matching mode (partial or complete match) does not correspond "
1246- " to any JIT compilation mode. When the JIT fast path function is used, this error may be "
1247- " also given for invalid options. See the pcrejit documentation for more details (PCRE_ERROR_JIT_BADOPTION)" ;
1248- #endif
1249- }
1250- return " " ;
1251- }
1252-
12531117void CppCheck::executeRules (const std::string &tokenlist, const TokenList &list)
12541118{
12551119 // There is no rule to execute
@@ -1271,73 +1135,7 @@ void CppCheck::executeRules(const std::string &tokenlist, const TokenList &list)
12711135 reportOut (" Processing rule: " + rule.pattern , Color::FgGreen);
12721136 }
12731137
1274- const char *pcreCompileErrorStr = nullptr ;
1275- int erroffset = 0 ;
1276- pcre * const re = pcre_compile (rule.pattern .c_str (),0 ,&pcreCompileErrorStr,&erroffset,nullptr );
1277- if (!re) {
1278- if (pcreCompileErrorStr) {
1279- const std::string msg = " pcre_compile failed: " + std::string (pcreCompileErrorStr);
1280- const ErrorMessage errmsg (std::list<ErrorMessage::FileLocation>(),
1281- emptyString,
1282- Severity::error,
1283- msg,
1284- " pcre_compile" ,
1285- Certainty::normal);
1286-
1287- reportErr (errmsg);
1288- }
1289- continue ;
1290- }
1291-
1292- // Optimize the regex, but only if PCRE_CONFIG_JIT is available
1293- #ifdef PCRE_CONFIG_JIT
1294- const char *pcreStudyErrorStr = nullptr ;
1295- pcre_extra * const pcreExtra = pcre_study (re, PCRE_STUDY_JIT_COMPILE, &pcreStudyErrorStr);
1296- // pcre_study() returns NULL for both errors and when it can not optimize the regex.
1297- // The last argument is how one checks for errors.
1298- // It is NULL if everything works, and points to an error string otherwise.
1299- if (pcreStudyErrorStr) {
1300- const std::string msg = " pcre_study failed: " + std::string (pcreStudyErrorStr);
1301- const ErrorMessage errmsg (std::list<ErrorMessage::FileLocation>(),
1302- emptyString,
1303- Severity::error,
1304- msg,
1305- " pcre_study" ,
1306- Certainty::normal);
1307-
1308- reportErr (errmsg);
1309- // pcre_compile() worked, but pcre_study() returned an error. Free the resources allocated by pcre_compile().
1310- pcre_free (re);
1311- continue ;
1312- }
1313- #else
1314- const pcre_extra * const pcreExtra = nullptr ;
1315- #endif
1316-
1317- int pos = 0 ;
1318- int ovector[30 ]= {0 };
1319- while (pos < (int )str.size ()) {
1320- const int pcreExecRet = pcre_exec (re, pcreExtra, str.c_str (), (int )str.size (), pos, 0 , ovector, 30 );
1321- if (pcreExecRet < 0 ) {
1322- const std::string errorMessage = pcreErrorCodeToString (pcreExecRet);
1323- if (!errorMessage.empty ()) {
1324- const ErrorMessage errmsg (std::list<ErrorMessage::FileLocation>(),
1325- emptyString,
1326- Severity::error,
1327- std::string (" pcre_exec failed: " ) + errorMessage,
1328- " pcre_exec" ,
1329- Certainty::normal);
1330-
1331- reportErr (errmsg);
1332- }
1333- break ;
1334- }
1335- const auto pos1 = (unsigned int )ovector[0 ];
1336- const auto pos2 = (unsigned int )ovector[1 ];
1337-
1338- // jump to the end of the match for the next pcre_exec
1339- pos = (int )pos2;
1340-
1138+ auto f = [&](int pos1, int pos2) {
13411139 // determine location..
13421140 int fileIndex = 0 ;
13431141 int line = 0 ;
@@ -1366,15 +1164,21 @@ void CppCheck::executeRules(const std::string &tokenlist, const TokenList &list)
13661164
13671165 // Report error
13681166 reportErr (errmsg);
1369- }
1167+ };
1168+
1169+ assert (rule.regex );
13701170
1371- pcre_free (re);
1372- #ifdef PCRE_CONFIG_JIT
1373- // Free up the EXTRA PCRE value (may be NULL at this point)
1374- if (pcreExtra) {
1375- pcre_free_study (pcreExtra);
1171+ const std::string err = rule.regex ->match (str, f);
1172+ if (!err.empty ()) {
1173+ const ErrorMessage errmsg (std::list<ErrorMessage::FileLocation>(),
1174+ emptyString,
1175+ Severity::error,
1176+ err,
1177+ " pcre_exec" ,
1178+ Certainty::normal);
1179+
1180+ reportErr (errmsg);
13761181 }
1377- #endif
13781182 }
13791183}
13801184#endif
0 commit comments