Prevent ShortVisit() from crashing fuzzers.

Change-Id: I2ec82a91a806a18bc1f801a77c9efb07019babe6
Reviewed-on: https://code-review.googlesource.com/c/re2/+/50210
Reviewed-by: Paul Wankadia <junyer@google.com>
diff --git a/re2/mimics_pcre.cc b/re2/mimics_pcre.cc
index ad197be..b1d6a51 100644
--- a/re2/mimics_pcre.cc
+++ b/re2/mimics_pcre.cc
@@ -38,14 +38,21 @@
 class PCREWalker : public Regexp::Walker<bool> {
  public:
   PCREWalker() {}
-  bool PostVisit(Regexp* re, bool parent_arg, bool pre_arg, bool* child_args,
-                 int nchild_args);
 
-  bool ShortVisit(Regexp* re, bool a) {
-    // Should never be called: we use Walk not WalkExponential.
-    LOG(DFATAL) << "EmptyStringWalker::ShortVisit called";
+  virtual bool PostVisit(Regexp* re, bool parent_arg, bool pre_arg,
+                         bool* child_args, int nchild_args);
+
+  virtual bool ShortVisit(Regexp* re, bool a) {
+    // Should never be called: we use Walk(), not WalkExponential().
+#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
+    LOG(DFATAL) << "PCREWalker::ShortVisit called";
+#endif
     return a;
   }
+
+ private:
+  PCREWalker(const PCREWalker&) = delete;
+  PCREWalker& operator=(const PCREWalker&) = delete;
 };
 
 // Called after visiting each of re's children and accumulating
@@ -114,13 +121,16 @@
 
 class EmptyStringWalker : public Regexp::Walker<bool> {
  public:
-  EmptyStringWalker() { }
-  bool PostVisit(Regexp* re, bool parent_arg, bool pre_arg,
-                 bool* child_args, int nchild_args);
+  EmptyStringWalker() {}
 
-  bool ShortVisit(Regexp* re, bool a) {
-    // Should never be called: we use Walk not WalkExponential.
+  virtual bool PostVisit(Regexp* re, bool parent_arg, bool pre_arg,
+                         bool* child_args, int nchild_args);
+
+  virtual bool ShortVisit(Regexp* re, bool a) {
+    // Should never be called: we use Walk(), not WalkExponential().
+#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
     LOG(DFATAL) << "EmptyStringWalker::ShortVisit called";
+#endif
     return a;
   }
 
diff --git a/re2/parse.cc b/re2/parse.cc
index 50dfdac..0a5ff74 100644
--- a/re2/parse.cc
+++ b/re2/parse.cc
@@ -556,9 +556,10 @@
 }
 
 int RepetitionWalker::ShortVisit(Regexp* re, int parent_arg) {
-  // This should never be called, since we use Walk and not
-  // WalkExponential.
+  // Should never be called: we use Walk(), not WalkExponential().
+#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
   LOG(DFATAL) << "RepetitionWalker::ShortVisit called";
+#endif
   return 0;
 }
 
diff --git a/re2/regexp.cc b/re2/regexp.cc
index 7995ffc..5732166 100644
--- a/re2/regexp.cc
+++ b/re2/regexp.cc
@@ -544,9 +544,12 @@
       ncapture_++;
     return ignored;
   }
+
   virtual Ignored ShortVisit(Regexp* re, Ignored ignored) {
-    // Should never be called: we use Walk not WalkExponential.
+    // Should never be called: we use Walk(), not WalkExponential().
+#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
     LOG(DFATAL) << "NumCapturesWalker::ShortVisit called";
+#endif
     return ignored;
   }
 
@@ -575,7 +578,7 @@
     return m;
   }
 
-  Ignored PreVisit(Regexp* re, Ignored ignored, bool* stop) {
+  virtual Ignored PreVisit(Regexp* re, Ignored ignored, bool* stop) {
     if (re->op() == kRegexpCapture && re->name() != NULL) {
       // Allocate map once we find a name.
       if (map_ == NULL)
@@ -591,8 +594,10 @@
   }
 
   virtual Ignored ShortVisit(Regexp* re, Ignored ignored) {
-    // Should never be called: we use Walk not WalkExponential.
+    // Should never be called: we use Walk(), not WalkExponential().
+#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
     LOG(DFATAL) << "NamedCapturesWalker::ShortVisit called";
+#endif
     return ignored;
   }
 
@@ -621,7 +626,7 @@
     return m;
   }
 
-  Ignored PreVisit(Regexp* re, Ignored ignored, bool* stop) {
+  virtual Ignored PreVisit(Regexp* re, Ignored ignored, bool* stop) {
     if (re->op() == kRegexpCapture && re->name() != NULL) {
       // Allocate map once we find a name.
       if (map_ == NULL)
@@ -633,8 +638,10 @@
   }
 
   virtual Ignored ShortVisit(Regexp* re, Ignored ignored) {
-    // Should never be called: we use Walk not WalkExponential.
+    // Should never be called: we use Walk(), not WalkExponential().
+#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
     LOG(DFATAL) << "CaptureNamesWalker::ShortVisit called";
+#endif
     return ignored;
   }
 
diff --git a/re2/simplify.cc b/re2/simplify.cc
index c6eb4a7..270f0ff 100644
--- a/re2/simplify.cc
+++ b/re2/simplify.cc
@@ -212,9 +212,10 @@
 }
 
 Regexp* CoalesceWalker::ShortVisit(Regexp* re, Regexp* parent_arg) {
-  // This should never be called, since we use Walk and not
-  // WalkExponential.
+  // Should never be called: we use Walk(), not WalkExponential().
+#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
   LOG(DFATAL) << "CoalesceWalker::ShortVisit called";
+#endif
   return re->Incref();
 }
 
@@ -437,9 +438,10 @@
 }
 
 Regexp* SimplifyWalker::ShortVisit(Regexp* re, Regexp* parent_arg) {
-  // This should never be called, since we use Walk and not
-  // WalkExponential.
+  // Should never be called: we use Walk(), not WalkExponential().
+#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
   LOG(DFATAL) << "SimplifyWalker::ShortVisit called";
+#endif
   return re->Incref();
 }
 
diff --git a/re2/testing/null_walker.cc b/re2/testing/null_walker.cc
index 77fa723..2bdea02 100644
--- a/re2/testing/null_walker.cc
+++ b/re2/testing/null_walker.cc
@@ -13,13 +13,16 @@
 
 class NullWalker : public Regexp::Walker<bool> {
  public:
-  NullWalker() { }
-  bool PostVisit(Regexp* re, bool parent_arg, bool pre_arg,
-                 bool* child_args, int nchild_args);
+  NullWalker() {}
 
-  bool ShortVisit(Regexp* re, bool a) {
-    // Should never be called: we use Walk not WalkExponential.
+  virtual bool PostVisit(Regexp* re, bool parent_arg, bool pre_arg,
+                         bool* child_args, int nchild_args);
+
+  virtual bool ShortVisit(Regexp* re, bool a) {
+    // Should never be called: we use Walk(), not WalkExponential().
+#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
     LOG(DFATAL) << "NullWalker::ShortVisit called";
+#endif
     return a;
   }