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