1 // Copyright 2003-2009 Google Inc. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // This is a variant of PCRE's pcrecpp.cc, originally written at Google.
6 // The main changes are the addition of the HitLimit method and
7 // compilation as PCRE in namespace re2.
8
9 #include <assert.h>
10 #include <ctype.h>
11 #include <errno.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <limits>
15 #include <string>
16 #include <utility>
17
18 #include "util/util.h"
19 #include "util/flags.h"
20 #include "util/logging.h"
21 #include "util/pcre.h"
22 #include "util/strutil.h"
23
24 // Silence warnings about the wacky formatting in the operator() functions.
25 // Note that we test for Clang first because it defines __GNUC__ as well.
26 #if defined(__clang__)
27 #elif defined(__GNUC__) && __GNUC__ >= 6
28 #pragma GCC diagnostic ignored "-Wmisleading-indentation"
29 #endif
30
31 #define PCREPORT(level) LOG(level)
32
33 // Default PCRE limits.
34 // Defaults chosen to allow a plausible amount of CPU and
35 // not exceed main thread stacks. Note that other threads
36 // often have smaller stacks, and therefore tightening
37 // regexp_stack_limit may frequently be necessary.
38 DEFINE_int32(regexp_stack_limit, 256<<10, "default PCRE stack limit (bytes)");
39 DEFINE_int32(regexp_match_limit, 1000000,
40 "default PCRE match limit (function calls)");
41
42 #ifndef USEPCRE
43
44 // Fake just enough of the PCRE API to allow this file to build. :)
45
46 struct pcre_extra {
47 int flags;
48 int match_limit;
49 int match_limit_recursion;
50 };
51
52 #define PCRE_EXTRA_MATCH_LIMIT 0
53 #define PCRE_EXTRA_MATCH_LIMIT_RECURSION 0
54 #define PCRE_ANCHORED 0
55 #define PCRE_NOTEMPTY 0
56 #define PCRE_ERROR_NOMATCH 1
57 #define PCRE_ERROR_MATCHLIMIT 2
58 #define PCRE_ERROR_RECURSIONLIMIT 3
59 #define PCRE_INFO_CAPTURECOUNT 0
60
pcre_free(void *)61 void pcre_free(void*) {
62 }
63
pcre_compile(const char *,int,const char **,int *,const unsigned char *)64 pcre* pcre_compile(const char*, int, const char**, int*, const unsigned char*) {
65 return NULL;
66 }
67
pcre_exec(const pcre *,const pcre_extra *,const char *,int,int,int,int *,int)68 int pcre_exec(const pcre*, const pcre_extra*, const char*, int, int, int, int*, int) {
69 return 0;
70 }
71
pcre_fullinfo(const pcre *,const pcre_extra *,int,void *)72 int pcre_fullinfo(const pcre*, const pcre_extra*, int, void*) {
73 return 0;
74 }
75
76 #endif
77
78 namespace re2 {
79
80 // Maximum number of args we can set
81 static const int kMaxArgs = 16;
82 static const int kVecSize = (1 + kMaxArgs) * 3; // results + PCRE workspace
83
84 // Approximate size of a recursive invocation of PCRE's
85 // internal "match()" frame. This varies depending on the
86 // compiler and architecture, of course, so the constant is
87 // just a conservative estimate. To find the exact number,
88 // run regexp_unittest with --regexp_stack_limit=0 under
89 // a debugger and look at the frames when it crashes.
90 // The exact frame size was 656 in production on 2008/02/03.
91 static const int kPCREFrameSize = 700;
92
93 // Special name for missing C++ arguments.
94 PCRE::Arg PCRE::no_more_args((void*)NULL);
95
96 const PCRE::PartialMatchFunctor PCRE::PartialMatch = { };
97 const PCRE::FullMatchFunctor PCRE::FullMatch = { } ;
98 const PCRE::ConsumeFunctor PCRE::Consume = { };
99 const PCRE::FindAndConsumeFunctor PCRE::FindAndConsume = { };
100
101 // If a regular expression has no error, its error_ field points here
102 static const string empty_string;
103
Init(const char * pattern,Option options,int match_limit,int stack_limit,bool report_errors)104 void PCRE::Init(const char* pattern, Option options, int match_limit,
105 int stack_limit, bool report_errors) {
106 pattern_ = pattern;
107 options_ = options;
108 match_limit_ = match_limit;
109 stack_limit_ = stack_limit;
110 hit_limit_ = false;
111 error_ = &empty_string;
112 report_errors_ = report_errors;
113 re_full_ = NULL;
114 re_partial_ = NULL;
115
116 if (options & ~(EnabledCompileOptions | EnabledExecOptions)) {
117 error_ = new string("illegal regexp option");
118 PCREPORT(ERROR)
119 << "Error compiling '" << pattern << "': illegal regexp option";
120 } else {
121 re_partial_ = Compile(UNANCHORED);
122 if (re_partial_ != NULL) {
123 re_full_ = Compile(ANCHOR_BOTH);
124 }
125 }
126 }
127
PCRE(const char * pattern)128 PCRE::PCRE(const char* pattern) {
129 Init(pattern, None, 0, 0, true);
130 }
PCRE(const char * pattern,Option option)131 PCRE::PCRE(const char* pattern, Option option) {
132 Init(pattern, option, 0, 0, true);
133 }
PCRE(const string & pattern)134 PCRE::PCRE(const string& pattern) {
135 Init(pattern.c_str(), None, 0, 0, true);
136 }
PCRE(const string & pattern,Option option)137 PCRE::PCRE(const string& pattern, Option option) {
138 Init(pattern.c_str(), option, 0, 0, true);
139 }
PCRE(const string & pattern,const PCRE_Options & re_option)140 PCRE::PCRE(const string& pattern, const PCRE_Options& re_option) {
141 Init(pattern.c_str(), re_option.option(), re_option.match_limit(),
142 re_option.stack_limit(), re_option.report_errors());
143 }
144
PCRE(const char * pattern,const PCRE_Options & re_option)145 PCRE::PCRE(const char *pattern, const PCRE_Options& re_option) {
146 Init(pattern, re_option.option(), re_option.match_limit(),
147 re_option.stack_limit(), re_option.report_errors());
148 }
149
~PCRE()150 PCRE::~PCRE() {
151 if (re_full_ != NULL) pcre_free(re_full_);
152 if (re_partial_ != NULL) pcre_free(re_partial_);
153 if (error_ != &empty_string) delete error_;
154 }
155
Compile(Anchor anchor)156 pcre* PCRE::Compile(Anchor anchor) {
157 // Special treatment for anchoring. This is needed because at
158 // runtime pcre only provides an option for anchoring at the
159 // beginning of a string.
160 //
161 // There are three types of anchoring we want:
162 // UNANCHORED Compile the original pattern, and use
163 // a pcre unanchored match.
164 // ANCHOR_START Compile the original pattern, and use
165 // a pcre anchored match.
166 // ANCHOR_BOTH Tack a "\z" to the end of the original pattern
167 // and use a pcre anchored match.
168
169 const char* error = "";
170 int eoffset;
171 pcre* re;
172 if (anchor != ANCHOR_BOTH) {
173 re = pcre_compile(pattern_.c_str(),
174 (options_ & EnabledCompileOptions),
175 &error, &eoffset, NULL);
176 } else {
177 // Tack a '\z' at the end of PCRE. Parenthesize it first so that
178 // the '\z' applies to all top-level alternatives in the regexp.
179 string wrapped = "(?:"; // A non-counting grouping operator
180 wrapped += pattern_;
181 wrapped += ")\\z";
182 re = pcre_compile(wrapped.c_str(),
183 (options_ & EnabledCompileOptions),
184 &error, &eoffset, NULL);
185 }
186 if (re == NULL) {
187 if (error_ == &empty_string) error_ = new string(error);
188 PCREPORT(ERROR) << "Error compiling '" << pattern_ << "': " << error;
189 }
190 return re;
191 }
192
193 /***** Convenience interfaces *****/
194
operator ()(const StringPiece & text,const PCRE & re,const Arg & a0,const Arg & a1,const Arg & a2,const Arg & a3,const Arg & a4,const Arg & a5,const Arg & a6,const Arg & a7,const Arg & a8,const Arg & a9,const Arg & a10,const Arg & a11,const Arg & a12,const Arg & a13,const Arg & a14,const Arg & a15) const195 bool PCRE::FullMatchFunctor::operator ()(const StringPiece& text,
196 const PCRE& re,
197 const Arg& a0,
198 const Arg& a1,
199 const Arg& a2,
200 const Arg& a3,
201 const Arg& a4,
202 const Arg& a5,
203 const Arg& a6,
204 const Arg& a7,
205 const Arg& a8,
206 const Arg& a9,
207 const Arg& a10,
208 const Arg& a11,
209 const Arg& a12,
210 const Arg& a13,
211 const Arg& a14,
212 const Arg& a15) const {
213 const Arg* args[kMaxArgs];
214 int n = 0;
215 if (&a0 == &no_more_args) goto done; args[n++] = &a0;
216 if (&a1 == &no_more_args) goto done; args[n++] = &a1;
217 if (&a2 == &no_more_args) goto done; args[n++] = &a2;
218 if (&a3 == &no_more_args) goto done; args[n++] = &a3;
219 if (&a4 == &no_more_args) goto done; args[n++] = &a4;
220 if (&a5 == &no_more_args) goto done; args[n++] = &a5;
221 if (&a6 == &no_more_args) goto done; args[n++] = &a6;
222 if (&a7 == &no_more_args) goto done; args[n++] = &a7;
223 if (&a8 == &no_more_args) goto done; args[n++] = &a8;
224 if (&a9 == &no_more_args) goto done; args[n++] = &a9;
225 if (&a10 == &no_more_args) goto done; args[n++] = &a10;
226 if (&a11 == &no_more_args) goto done; args[n++] = &a11;
227 if (&a12 == &no_more_args) goto done; args[n++] = &a12;
228 if (&a13 == &no_more_args) goto done; args[n++] = &a13;
229 if (&a14 == &no_more_args) goto done; args[n++] = &a14;
230 if (&a15 == &no_more_args) goto done; args[n++] = &a15;
231 done:
232
233 size_t consumed;
234 int vec[kVecSize] = {};
235 return re.DoMatchImpl(text, ANCHOR_BOTH, &consumed, args, n, vec, kVecSize);
236 }
237
operator ()(const StringPiece & text,const PCRE & re,const Arg & a0,const Arg & a1,const Arg & a2,const Arg & a3,const Arg & a4,const Arg & a5,const Arg & a6,const Arg & a7,const Arg & a8,const Arg & a9,const Arg & a10,const Arg & a11,const Arg & a12,const Arg & a13,const Arg & a14,const Arg & a15) const238 bool PCRE::PartialMatchFunctor::operator ()(const StringPiece& text,
239 const PCRE& re,
240 const Arg& a0,
241 const Arg& a1,
242 const Arg& a2,
243 const Arg& a3,
244 const Arg& a4,
245 const Arg& a5,
246 const Arg& a6,
247 const Arg& a7,
248 const Arg& a8,
249 const Arg& a9,
250 const Arg& a10,
251 const Arg& a11,
252 const Arg& a12,
253 const Arg& a13,
254 const Arg& a14,
255 const Arg& a15) const {
256 const Arg* args[kMaxArgs];
257 int n = 0;
258 if (&a0 == &no_more_args) goto done; args[n++] = &a0;
259 if (&a1 == &no_more_args) goto done; args[n++] = &a1;
260 if (&a2 == &no_more_args) goto done; args[n++] = &a2;
261 if (&a3 == &no_more_args) goto done; args[n++] = &a3;
262 if (&a4 == &no_more_args) goto done; args[n++] = &a4;
263 if (&a5 == &no_more_args) goto done; args[n++] = &a5;
264 if (&a6 == &no_more_args) goto done; args[n++] = &a6;
265 if (&a7 == &no_more_args) goto done; args[n++] = &a7;
266 if (&a8 == &no_more_args) goto done; args[n++] = &a8;
267 if (&a9 == &no_more_args) goto done; args[n++] = &a9;
268 if (&a10 == &no_more_args) goto done; args[n++] = &a10;
269 if (&a11 == &no_more_args) goto done; args[n++] = &a11;
270 if (&a12 == &no_more_args) goto done; args[n++] = &a12;
271 if (&a13 == &no_more_args) goto done; args[n++] = &a13;
272 if (&a14 == &no_more_args) goto done; args[n++] = &a14;
273 if (&a15 == &no_more_args) goto done; args[n++] = &a15;
274 done:
275
276 size_t consumed;
277 int vec[kVecSize] = {};
278 return re.DoMatchImpl(text, UNANCHORED, &consumed, args, n, vec, kVecSize);
279 }
280
operator ()(StringPiece * input,const PCRE & pattern,const Arg & a0,const Arg & a1,const Arg & a2,const Arg & a3,const Arg & a4,const Arg & a5,const Arg & a6,const Arg & a7,const Arg & a8,const Arg & a9,const Arg & a10,const Arg & a11,const Arg & a12,const Arg & a13,const Arg & a14,const Arg & a15) const281 bool PCRE::ConsumeFunctor::operator ()(StringPiece* input,
282 const PCRE& pattern,
283 const Arg& a0,
284 const Arg& a1,
285 const Arg& a2,
286 const Arg& a3,
287 const Arg& a4,
288 const Arg& a5,
289 const Arg& a6,
290 const Arg& a7,
291 const Arg& a8,
292 const Arg& a9,
293 const Arg& a10,
294 const Arg& a11,
295 const Arg& a12,
296 const Arg& a13,
297 const Arg& a14,
298 const Arg& a15) const {
299 const Arg* args[kMaxArgs];
300 int n = 0;
301 if (&a0 == &no_more_args) goto done; args[n++] = &a0;
302 if (&a1 == &no_more_args) goto done; args[n++] = &a1;
303 if (&a2 == &no_more_args) goto done; args[n++] = &a2;
304 if (&a3 == &no_more_args) goto done; args[n++] = &a3;
305 if (&a4 == &no_more_args) goto done; args[n++] = &a4;
306 if (&a5 == &no_more_args) goto done; args[n++] = &a5;
307 if (&a6 == &no_more_args) goto done; args[n++] = &a6;
308 if (&a7 == &no_more_args) goto done; args[n++] = &a7;
309 if (&a8 == &no_more_args) goto done; args[n++] = &a8;
310 if (&a9 == &no_more_args) goto done; args[n++] = &a9;
311 if (&a10 == &no_more_args) goto done; args[n++] = &a10;
312 if (&a11 == &no_more_args) goto done; args[n++] = &a11;
313 if (&a12 == &no_more_args) goto done; args[n++] = &a12;
314 if (&a13 == &no_more_args) goto done; args[n++] = &a13;
315 if (&a14 == &no_more_args) goto done; args[n++] = &a14;
316 if (&a15 == &no_more_args) goto done; args[n++] = &a15;
317 done:
318
319 size_t consumed;
320 int vec[kVecSize] = {};
321 if (pattern.DoMatchImpl(*input, ANCHOR_START, &consumed,
322 args, n, vec, kVecSize)) {
323 input->remove_prefix(consumed);
324 return true;
325 } else {
326 return false;
327 }
328 }
329
operator ()(StringPiece * input,const PCRE & pattern,const Arg & a0,const Arg & a1,const Arg & a2,const Arg & a3,const Arg & a4,const Arg & a5,const Arg & a6,const Arg & a7,const Arg & a8,const Arg & a9,const Arg & a10,const Arg & a11,const Arg & a12,const Arg & a13,const Arg & a14,const Arg & a15) const330 bool PCRE::FindAndConsumeFunctor::operator ()(StringPiece* input,
331 const PCRE& pattern,
332 const Arg& a0,
333 const Arg& a1,
334 const Arg& a2,
335 const Arg& a3,
336 const Arg& a4,
337 const Arg& a5,
338 const Arg& a6,
339 const Arg& a7,
340 const Arg& a8,
341 const Arg& a9,
342 const Arg& a10,
343 const Arg& a11,
344 const Arg& a12,
345 const Arg& a13,
346 const Arg& a14,
347 const Arg& a15) const {
348 const Arg* args[kMaxArgs];
349 int n = 0;
350 if (&a0 == &no_more_args) goto done; args[n++] = &a0;
351 if (&a1 == &no_more_args) goto done; args[n++] = &a1;
352 if (&a2 == &no_more_args) goto done; args[n++] = &a2;
353 if (&a3 == &no_more_args) goto done; args[n++] = &a3;
354 if (&a4 == &no_more_args) goto done; args[n++] = &a4;
355 if (&a5 == &no_more_args) goto done; args[n++] = &a5;
356 if (&a6 == &no_more_args) goto done; args[n++] = &a6;
357 if (&a7 == &no_more_args) goto done; args[n++] = &a7;
358 if (&a8 == &no_more_args) goto done; args[n++] = &a8;
359 if (&a9 == &no_more_args) goto done; args[n++] = &a9;
360 if (&a10 == &no_more_args) goto done; args[n++] = &a10;
361 if (&a11 == &no_more_args) goto done; args[n++] = &a11;
362 if (&a12 == &no_more_args) goto done; args[n++] = &a12;
363 if (&a13 == &no_more_args) goto done; args[n++] = &a13;
364 if (&a14 == &no_more_args) goto done; args[n++] = &a14;
365 if (&a15 == &no_more_args) goto done; args[n++] = &a15;
366 done:
367
368 size_t consumed;
369 int vec[kVecSize] = {};
370 if (pattern.DoMatchImpl(*input, UNANCHORED, &consumed,
371 args, n, vec, kVecSize)) {
372 input->remove_prefix(consumed);
373 return true;
374 } else {
375 return false;
376 }
377 }
378
Replace(string * str,const PCRE & pattern,const StringPiece & rewrite)379 bool PCRE::Replace(string *str,
380 const PCRE& pattern,
381 const StringPiece& rewrite) {
382 int vec[kVecSize] = {};
383 int matches = pattern.TryMatch(*str, 0, UNANCHORED, true, vec, kVecSize);
384 if (matches == 0)
385 return false;
386
387 string s;
388 if (!pattern.Rewrite(&s, rewrite, *str, vec, matches))
389 return false;
390
391 assert(vec[0] >= 0);
392 assert(vec[1] >= 0);
393 str->replace(vec[0], vec[1] - vec[0], s);
394 return true;
395 }
396
GlobalReplace(string * str,const PCRE & pattern,const StringPiece & rewrite)397 int PCRE::GlobalReplace(string *str,
398 const PCRE& pattern,
399 const StringPiece& rewrite) {
400 int count = 0;
401 int vec[kVecSize] = {};
402 string out;
403 size_t start = 0;
404 bool last_match_was_empty_string = false;
405
406 while (start <= str->size()) {
407 // If the previous match was for the empty string, we shouldn't
408 // just match again: we'll match in the same way and get an
409 // infinite loop. Instead, we do the match in a special way:
410 // anchored -- to force another try at the same position --
411 // and with a flag saying that this time, ignore empty matches.
412 // If this special match returns, that means there's a non-empty
413 // match at this position as well, and we can continue. If not,
414 // we do what perl does, and just advance by one.
415 // Notice that perl prints '@@@' for this;
416 // perl -le '$_ = "aa"; s/b*|aa/@/g; print'
417 int matches;
418 if (last_match_was_empty_string) {
419 matches = pattern.TryMatch(*str, start, ANCHOR_START, false,
420 vec, kVecSize);
421 if (matches <= 0) {
422 if (start < str->size())
423 out.push_back((*str)[start]);
424 start++;
425 last_match_was_empty_string = false;
426 continue;
427 }
428 } else {
429 matches = pattern.TryMatch(*str, start, UNANCHORED, true,
430 vec, kVecSize);
431 if (matches <= 0)
432 break;
433 }
434 size_t matchstart = vec[0], matchend = vec[1];
435 assert(matchstart >= start);
436 assert(matchend >= matchstart);
437
438 out.append(*str, start, matchstart - start);
439 pattern.Rewrite(&out, rewrite, *str, vec, matches);
440 start = matchend;
441 count++;
442 last_match_was_empty_string = (matchstart == matchend);
443 }
444
445 if (count == 0)
446 return 0;
447
448 if (start < str->size())
449 out.append(*str, start, str->size() - start);
450 using std::swap;
451 swap(out, *str);
452 return count;
453 }
454
Extract(const StringPiece & text,const PCRE & pattern,const StringPiece & rewrite,string * out)455 bool PCRE::Extract(const StringPiece &text,
456 const PCRE& pattern,
457 const StringPiece &rewrite,
458 string *out) {
459 int vec[kVecSize] = {};
460 int matches = pattern.TryMatch(text, 0, UNANCHORED, true, vec, kVecSize);
461 if (matches == 0)
462 return false;
463 out->clear();
464 return pattern.Rewrite(out, rewrite, text, vec, matches);
465 }
466
QuoteMeta(const StringPiece & unquoted)467 string PCRE::QuoteMeta(const StringPiece& unquoted) {
468 string result;
469 result.reserve(unquoted.size() << 1);
470
471 // Escape any ascii character not in [A-Za-z_0-9].
472 //
473 // Note that it's legal to escape a character even if it has no
474 // special meaning in a regular expression -- so this function does
475 // that. (This also makes it identical to the perl function of the
476 // same name except for the null-character special case;
477 // see `perldoc -f quotemeta`.)
478 for (size_t ii = 0; ii < unquoted.size(); ++ii) {
479 // Note that using 'isalnum' here raises the benchmark time from
480 // 32ns to 58ns:
481 if ((unquoted[ii] < 'a' || unquoted[ii] > 'z') &&
482 (unquoted[ii] < 'A' || unquoted[ii] > 'Z') &&
483 (unquoted[ii] < '0' || unquoted[ii] > '9') &&
484 unquoted[ii] != '_' &&
485 // If this is the part of a UTF8 or Latin1 character, we need
486 // to copy this byte without escaping. Experimentally this is
487 // what works correctly with the regexp library.
488 !(unquoted[ii] & 128)) {
489 if (unquoted[ii] == '\0') { // Special handling for null chars.
490 // Can't use "\\0" since the next character might be a digit.
491 result += "\\x00";
492 continue;
493 }
494 result += '\\';
495 }
496 result += unquoted[ii];
497 }
498
499 return result;
500 }
501
502 /***** Actual matching and rewriting code *****/
503
HitLimit()504 bool PCRE::HitLimit() {
505 return hit_limit_ != 0;
506 }
507
ClearHitLimit()508 void PCRE::ClearHitLimit() {
509 hit_limit_ = 0;
510 }
511
TryMatch(const StringPiece & text,size_t startpos,Anchor anchor,bool empty_ok,int * vec,int vecsize) const512 int PCRE::TryMatch(const StringPiece& text,
513 size_t startpos,
514 Anchor anchor,
515 bool empty_ok,
516 int *vec,
517 int vecsize) const {
518 pcre* re = (anchor == ANCHOR_BOTH) ? re_full_ : re_partial_;
519 if (re == NULL) {
520 PCREPORT(ERROR) << "Matching against invalid re: " << *error_;
521 return 0;
522 }
523
524 int match_limit = match_limit_;
525 if (match_limit <= 0) {
526 match_limit = FLAGS_regexp_match_limit;
527 }
528
529 int stack_limit = stack_limit_;
530 if (stack_limit <= 0) {
531 stack_limit = FLAGS_regexp_stack_limit;
532 }
533
534 pcre_extra extra = { 0 };
535 if (match_limit > 0) {
536 extra.flags |= PCRE_EXTRA_MATCH_LIMIT;
537 extra.match_limit = match_limit;
538 }
539 if (stack_limit > 0) {
540 extra.flags |= PCRE_EXTRA_MATCH_LIMIT_RECURSION;
541 extra.match_limit_recursion = stack_limit / kPCREFrameSize;
542 }
543
544 int options = 0;
545 if (anchor != UNANCHORED)
546 options |= PCRE_ANCHORED;
547 if (!empty_ok)
548 options |= PCRE_NOTEMPTY;
549
550 int rc = pcre_exec(re, // The regular expression object
551 &extra,
552 (text.data() == NULL) ? "" : text.data(),
553 static_cast<int>(text.size()),
554 static_cast<int>(startpos),
555 options,
556 vec,
557 vecsize);
558
559 // Handle errors
560 if (rc == 0) {
561 // pcre_exec() returns 0 as a special case when the number of
562 // capturing subpatterns exceeds the size of the vector.
563 // When this happens, there is a match and the output vector
564 // is filled, but we miss out on the positions of the extra subpatterns.
565 rc = vecsize / 2;
566 } else if (rc < 0) {
567 switch (rc) {
568 case PCRE_ERROR_NOMATCH:
569 return 0;
570 case PCRE_ERROR_MATCHLIMIT:
571 // Writing to hit_limit is not safe if multiple threads
572 // are using the PCRE, but the flag is only intended
573 // for use by unit tests anyway, so we let it go.
574 hit_limit_ = true;
575 PCREPORT(WARNING) << "Exceeded match limit of " << match_limit
576 << " when matching '" << pattern_ << "'"
577 << " against text that is " << text.size() << " bytes.";
578 return 0;
579 case PCRE_ERROR_RECURSIONLIMIT:
580 // See comment about hit_limit above.
581 hit_limit_ = true;
582 PCREPORT(WARNING) << "Exceeded stack limit of " << stack_limit
583 << " when matching '" << pattern_ << "'"
584 << " against text that is " << text.size() << " bytes.";
585 return 0;
586 default:
587 // There are other return codes from pcre.h :
588 // PCRE_ERROR_NULL (-2)
589 // PCRE_ERROR_BADOPTION (-3)
590 // PCRE_ERROR_BADMAGIC (-4)
591 // PCRE_ERROR_UNKNOWN_NODE (-5)
592 // PCRE_ERROR_NOMEMORY (-6)
593 // PCRE_ERROR_NOSUBSTRING (-7)
594 // ...
595 PCREPORT(ERROR) << "Unexpected return code: " << rc
596 << " when matching '" << pattern_ << "'"
597 << ", re=" << re
598 << ", text=" << text
599 << ", vec=" << vec
600 << ", vecsize=" << vecsize;
601 return 0;
602 }
603 }
604
605 return rc;
606 }
607
DoMatchImpl(const StringPiece & text,Anchor anchor,size_t * consumed,const Arg * const * args,int n,int * vec,int vecsize) const608 bool PCRE::DoMatchImpl(const StringPiece& text,
609 Anchor anchor,
610 size_t* consumed,
611 const Arg* const* args,
612 int n,
613 int* vec,
614 int vecsize) const {
615 assert((1 + n) * 3 <= vecsize); // results + PCRE workspace
616 if (NumberOfCapturingGroups() < n) {
617 // RE has fewer capturing groups than number of Arg pointers passed in.
618 return false;
619 }
620
621 int matches = TryMatch(text, 0, anchor, true, vec, vecsize);
622 assert(matches >= 0); // TryMatch never returns negatives
623 if (matches == 0)
624 return false;
625
626 *consumed = vec[1];
627
628 if (n == 0 || args == NULL) {
629 // We are not interested in results
630 return true;
631 }
632
633 // If we got here, we must have matched the whole pattern.
634 // We do not need (can not do) any more checks on the value of 'matches' here
635 // -- see the comment for TryMatch.
636 for (int i = 0; i < n; i++) {
637 const int start = vec[2*(i+1)];
638 const int limit = vec[2*(i+1)+1];
639
640 // Avoid invoking undefined behavior when text.data() happens
641 // to be null and start happens to be -1, the latter being the
642 // case for an unmatched subexpression. Even if text.data() is
643 // not null, pointing one byte before was a longstanding bug.
644 const char* addr = NULL;
645 if (start != -1) {
646 addr = text.data() + start;
647 }
648
649 if (!args[i]->Parse(addr, limit-start)) {
650 // TODO: Should we indicate what the error was?
651 return false;
652 }
653 }
654
655 return true;
656 }
657
DoMatch(const StringPiece & text,Anchor anchor,size_t * consumed,const Arg * const args[],int n) const658 bool PCRE::DoMatch(const StringPiece& text,
659 Anchor anchor,
660 size_t* consumed,
661 const Arg* const args[],
662 int n) const {
663 assert(n >= 0);
664 const int vecsize = (1 + n) * 3; // results + PCRE workspace
665 // (as for kVecSize)
666 int* vec = new int[vecsize];
667 bool b = DoMatchImpl(text, anchor, consumed, args, n, vec, vecsize);
668 delete[] vec;
669 return b;
670 }
671
Rewrite(string * out,const StringPiece & rewrite,const StringPiece & text,int * vec,int veclen) const672 bool PCRE::Rewrite(string *out, const StringPiece &rewrite,
673 const StringPiece &text, int *vec, int veclen) const {
674 int number_of_capturing_groups = NumberOfCapturingGroups();
675 for (const char *s = rewrite.data(), *end = s + rewrite.size();
676 s < end; s++) {
677 int c = *s;
678 if (c == '\\') {
679 c = *++s;
680 if (isdigit(c)) {
681 int n = (c - '0');
682 if (n >= veclen) {
683 if (n <= number_of_capturing_groups) {
684 // unmatched optional capturing group. treat
685 // its value as empty string; i.e., nothing to append.
686 } else {
687 PCREPORT(ERROR) << "requested group " << n
688 << " in regexp " << rewrite.data();
689 return false;
690 }
691 }
692 int start = vec[2 * n];
693 if (start >= 0)
694 out->append(text.data() + start, vec[2 * n + 1] - start);
695 } else if (c == '\\') {
696 out->push_back('\\');
697 } else {
698 PCREPORT(ERROR) << "invalid rewrite pattern: " << rewrite.data();
699 return false;
700 }
701 } else {
702 out->push_back(c);
703 }
704 }
705 return true;
706 }
707
CheckRewriteString(const StringPiece & rewrite,string * error) const708 bool PCRE::CheckRewriteString(const StringPiece& rewrite, string* error) const {
709 int max_token = -1;
710 for (const char *s = rewrite.data(), *end = s + rewrite.size();
711 s < end; s++) {
712 int c = *s;
713 if (c != '\\') {
714 continue;
715 }
716 if (++s == end) {
717 *error = "Rewrite schema error: '\\' not allowed at end.";
718 return false;
719 }
720 c = *s;
721 if (c == '\\') {
722 continue;
723 }
724 if (!isdigit(c)) {
725 *error = "Rewrite schema error: "
726 "'\\' must be followed by a digit or '\\'.";
727 return false;
728 }
729 int n = (c - '0');
730 if (max_token < n) {
731 max_token = n;
732 }
733 }
734
735 if (max_token > NumberOfCapturingGroups()) {
736 SStringPrintf(error, "Rewrite schema requests %d matches, "
737 "but the regexp only has %d parenthesized subexpressions.",
738 max_token, NumberOfCapturingGroups());
739 return false;
740 }
741 return true;
742 }
743
744
745 // Return the number of capturing subpatterns, or -1 if the
746 // regexp wasn't valid on construction.
NumberOfCapturingGroups() const747 int PCRE::NumberOfCapturingGroups() const {
748 if (re_partial_ == NULL) return -1;
749
750 int result;
751 int rc = pcre_fullinfo(re_partial_, // The regular expression object
752 NULL, // We did not study the pattern
753 PCRE_INFO_CAPTURECOUNT,
754 &result);
755 if (rc != 0) {
756 PCREPORT(ERROR) << "Unexpected return code: " << rc;
757 return -1;
758 }
759 return result;
760 }
761
762
763 /***** Parsers for various types *****/
764
parse_null(const char * str,size_t n,void * dest)765 bool PCRE::Arg::parse_null(const char* str, size_t n, void* dest) {
766 // We fail if somebody asked us to store into a non-NULL void* pointer
767 return (dest == NULL);
768 }
769
parse_string(const char * str,size_t n,void * dest)770 bool PCRE::Arg::parse_string(const char* str, size_t n, void* dest) {
771 if (dest == NULL) return true;
772 reinterpret_cast<string*>(dest)->assign(str, n);
773 return true;
774 }
775
parse_stringpiece(const char * str,size_t n,void * dest)776 bool PCRE::Arg::parse_stringpiece(const char* str, size_t n, void* dest) {
777 if (dest == NULL) return true;
778 *(reinterpret_cast<StringPiece*>(dest)) = StringPiece(str, n);
779 return true;
780 }
781
parse_char(const char * str,size_t n,void * dest)782 bool PCRE::Arg::parse_char(const char* str, size_t n, void* dest) {
783 if (n != 1) return false;
784 if (dest == NULL) return true;
785 *(reinterpret_cast<char*>(dest)) = str[0];
786 return true;
787 }
788
parse_schar(const char * str,size_t n,void * dest)789 bool PCRE::Arg::parse_schar(const char* str, size_t n, void* dest) {
790 if (n != 1) return false;
791 if (dest == NULL) return true;
792 *(reinterpret_cast<signed char*>(dest)) = str[0];
793 return true;
794 }
795
parse_uchar(const char * str,size_t n,void * dest)796 bool PCRE::Arg::parse_uchar(const char* str, size_t n, void* dest) {
797 if (n != 1) return false;
798 if (dest == NULL) return true;
799 *(reinterpret_cast<unsigned char*>(dest)) = str[0];
800 return true;
801 }
802
803 // Largest number spec that we are willing to parse
804 static const int kMaxNumberLength = 32;
805
806 // PCREQUIPCRES "buf" must have length at least kMaxNumberLength+1
807 // PCREQUIPCRES "n > 0"
808 // Copies "str" into "buf" and null-terminates if necessary.
809 // Returns one of:
810 // a. "str" if no termination is needed
811 // b. "buf" if the string was copied and null-terminated
812 // c. "" if the input was invalid and has no hope of being parsed
TerminateNumber(char * buf,const char * str,size_t n)813 static const char* TerminateNumber(char* buf, const char* str, size_t n) {
814 if ((n > 0) && isspace(*str)) {
815 // We are less forgiving than the strtoxxx() routines and do not
816 // allow leading spaces.
817 return "";
818 }
819
820 // See if the character right after the input text may potentially
821 // look like a digit.
822 if (isdigit(str[n]) ||
823 ((str[n] >= 'a') && (str[n] <= 'f')) ||
824 ((str[n] >= 'A') && (str[n] <= 'F'))) {
825 if (n > kMaxNumberLength) return ""; // Input too big to be a valid number
826 memcpy(buf, str, n);
827 buf[n] = '\0';
828 return buf;
829 } else {
830 // We can parse right out of the supplied string, so return it.
831 return str;
832 }
833 }
834
parse_long_radix(const char * str,size_t n,void * dest,int radix)835 bool PCRE::Arg::parse_long_radix(const char* str,
836 size_t n,
837 void* dest,
838 int radix) {
839 if (n == 0) return false;
840 char buf[kMaxNumberLength+1];
841 str = TerminateNumber(buf, str, n);
842 char* end;
843 errno = 0;
844 long r = strtol(str, &end, radix);
845 if (end != str + n) return false; // Leftover junk
846 if (errno) return false;
847 if (dest == NULL) return true;
848 *(reinterpret_cast<long*>(dest)) = r;
849 return true;
850 }
851
parse_ulong_radix(const char * str,size_t n,void * dest,int radix)852 bool PCRE::Arg::parse_ulong_radix(const char* str,
853 size_t n,
854 void* dest,
855 int radix) {
856 if (n == 0) return false;
857 char buf[kMaxNumberLength+1];
858 str = TerminateNumber(buf, str, n);
859 if (str[0] == '-') {
860 // strtoul() will silently accept negative numbers and parse
861 // them. This module is more strict and treats them as errors.
862 return false;
863 }
864
865 char* end;
866 errno = 0;
867 unsigned long r = strtoul(str, &end, radix);
868 if (end != str + n) return false; // Leftover junk
869 if (errno) return false;
870 if (dest == NULL) return true;
871 *(reinterpret_cast<unsigned long*>(dest)) = r;
872 return true;
873 }
874
parse_short_radix(const char * str,size_t n,void * dest,int radix)875 bool PCRE::Arg::parse_short_radix(const char* str,
876 size_t n,
877 void* dest,
878 int radix) {
879 long r;
880 if (!parse_long_radix(str, n, &r, radix)) return false; // Could not parse
881 if ((short)r != r) return false; // Out of range
882 if (dest == NULL) return true;
883 *(reinterpret_cast<short*>(dest)) = (short)r;
884 return true;
885 }
886
parse_ushort_radix(const char * str,size_t n,void * dest,int radix)887 bool PCRE::Arg::parse_ushort_radix(const char* str,
888 size_t n,
889 void* dest,
890 int radix) {
891 unsigned long r;
892 if (!parse_ulong_radix(str, n, &r, radix)) return false; // Could not parse
893 if ((unsigned short)r != r) return false; // Out of range
894 if (dest == NULL) return true;
895 *(reinterpret_cast<unsigned short*>(dest)) = (unsigned short)r;
896 return true;
897 }
898
parse_int_radix(const char * str,size_t n,void * dest,int radix)899 bool PCRE::Arg::parse_int_radix(const char* str,
900 size_t n,
901 void* dest,
902 int radix) {
903 long r;
904 if (!parse_long_radix(str, n, &r, radix)) return false; // Could not parse
905 if ((int)r != r) return false; // Out of range
906 if (dest == NULL) return true;
907 *(reinterpret_cast<int*>(dest)) = (int)r;
908 return true;
909 }
910
parse_uint_radix(const char * str,size_t n,void * dest,int radix)911 bool PCRE::Arg::parse_uint_radix(const char* str,
912 size_t n,
913 void* dest,
914 int radix) {
915 unsigned long r;
916 if (!parse_ulong_radix(str, n, &r, radix)) return false; // Could not parse
917 if ((unsigned int)r != r) return false; // Out of range
918 if (dest == NULL) return true;
919 *(reinterpret_cast<unsigned int*>(dest)) = (unsigned int)r;
920 return true;
921 }
922
parse_longlong_radix(const char * str,size_t n,void * dest,int radix)923 bool PCRE::Arg::parse_longlong_radix(const char* str,
924 size_t n,
925 void* dest,
926 int radix) {
927 if (n == 0) return false;
928 char buf[kMaxNumberLength+1];
929 str = TerminateNumber(buf, str, n);
930 char* end;
931 errno = 0;
932 long long r = strtoll(str, &end, radix);
933 if (end != str + n) return false; // Leftover junk
934 if (errno) return false;
935 if (dest == NULL) return true;
936 *(reinterpret_cast<long long*>(dest)) = r;
937 return true;
938 }
939
parse_ulonglong_radix(const char * str,size_t n,void * dest,int radix)940 bool PCRE::Arg::parse_ulonglong_radix(const char* str,
941 size_t n,
942 void* dest,
943 int radix) {
944 if (n == 0) return false;
945 char buf[kMaxNumberLength+1];
946 str = TerminateNumber(buf, str, n);
947 if (str[0] == '-') {
948 // strtoull() will silently accept negative numbers and parse
949 // them. This module is more strict and treats them as errors.
950 return false;
951 }
952 char* end;
953 errno = 0;
954 unsigned long long r = strtoull(str, &end, radix);
955 if (end != str + n) return false; // Leftover junk
956 if (errno) return false;
957 if (dest == NULL) return true;
958 *(reinterpret_cast<unsigned long long*>(dest)) = r;
959 return true;
960 }
961
parse_double_float(const char * str,size_t n,bool isfloat,void * dest)962 static bool parse_double_float(const char* str, size_t n, bool isfloat,
963 void* dest) {
964 if (n == 0) return false;
965 static const int kMaxLength = 200;
966 char buf[kMaxLength];
967 if (n >= kMaxLength) return false;
968 memcpy(buf, str, n);
969 buf[n] = '\0';
970 char* end;
971 errno = 0;
972 double r;
973 if (isfloat) {
974 r = strtof(buf, &end);
975 } else {
976 r = strtod(buf, &end);
977 }
978 if (end != buf + n) {
979 #ifdef _WIN32
980 // Microsoft's strtod() doesn't handle inf and nan, so we have to
981 // handle it explicitly. Speed is not important here because this
982 // code is only called in unit tests.
983 bool pos = true;
984 const char* i = buf;
985 if ('-' == *i) {
986 pos = false;
987 ++i;
988 } else if ('+' == *i) {
989 ++i;
990 }
991 if (0 == _stricmp(i, "inf") || 0 == _stricmp(i, "infinity")) {
992 r = std::numeric_limits<double>::infinity();
993 if (!pos)
994 r = -r;
995 } else if (0 == _stricmp(i, "nan")) {
996 r = std::numeric_limits<double>::quiet_NaN();
997 } else {
998 return false;
999 }
1000 #else
1001 return false; // Leftover junk
1002 #endif
1003 }
1004 if (errno) return false;
1005 if (dest == NULL) return true;
1006 if (isfloat) {
1007 *(reinterpret_cast<float*>(dest)) = (float)r;
1008 } else {
1009 *(reinterpret_cast<double*>(dest)) = r;
1010 }
1011 return true;
1012 }
1013
parse_double(const char * str,size_t n,void * dest)1014 bool PCRE::Arg::parse_double(const char* str, size_t n, void* dest) {
1015 return parse_double_float(str, n, false, dest);
1016 }
1017
parse_float(const char * str,size_t n,void * dest)1018 bool PCRE::Arg::parse_float(const char* str, size_t n, void* dest) {
1019 return parse_double_float(str, n, true, dest);
1020 }
1021
1022 #define DEFINE_INTEGER_PARSER(name) \
1023 bool PCRE::Arg::parse_##name(const char* str, size_t n, void* dest) { \
1024 return parse_##name##_radix(str, n, dest, 10); \
1025 } \
1026 bool PCRE::Arg::parse_##name##_hex(const char* str, size_t n, void* dest) { \
1027 return parse_##name##_radix(str, n, dest, 16); \
1028 } \
1029 bool PCRE::Arg::parse_##name##_octal(const char* str, size_t n, \
1030 void* dest) { \
1031 return parse_##name##_radix(str, n, dest, 8); \
1032 } \
1033 bool PCRE::Arg::parse_##name##_cradix(const char* str, size_t n, \
1034 void* dest) { \
1035 return parse_##name##_radix(str, n, dest, 0); \
1036 }
1037
1038 DEFINE_INTEGER_PARSER(short);
1039 DEFINE_INTEGER_PARSER(ushort);
1040 DEFINE_INTEGER_PARSER(int);
1041 DEFINE_INTEGER_PARSER(uint);
1042 DEFINE_INTEGER_PARSER(long);
1043 DEFINE_INTEGER_PARSER(ulong);
1044 DEFINE_INTEGER_PARSER(longlong);
1045 DEFINE_INTEGER_PARSER(ulonglong);
1046
1047 #undef DEFINE_INTEGER_PARSER
1048
1049 } // namespace re2
1050