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