D3676: fuzzutil: make it possible to use absl when C++17 isn't supported

durin42 (Augie Fackler) phabricator at mercurial-scm.org
Wed May 30 21:01:49 EDT 2018


This revision was automatically updated to reflect the committed changes.
Closed by commit rHG36d55f90e2a3: fuzzutil: make it possible to use absl when C++17 isn't supported (authored by durin42, committed by ).

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D3676?vs=8933&id=8937

REVISION DETAIL
  https://phab.mercurial-scm.org/D3676

AFFECTED FILES
  contrib/fuzz/Makefile
  contrib/fuzz/fuzzutil.cc
  contrib/fuzz/fuzzutil.h

CHANGE DETAILS

diff --git a/contrib/fuzz/fuzzutil.h b/contrib/fuzz/fuzzutil.h
--- a/contrib/fuzz/fuzzutil.h
+++ b/contrib/fuzz/fuzzutil.h
@@ -2,8 +2,31 @@
 #define CONTRIB_FUZZ_FUZZUTIL_H
 #include <iostream>
 #include <memory>
+#include <stdint.h>
+
+/* Try and use std::optional, but failing that assume we'll have a
+ * workable https://abseil.io/ install on the include path to get
+ * their backport of std::optional. */
+#ifdef __has_include
+#if __has_include(<optional>) && __cplusplus >= 201703L
 #include <optional>
-#include <stdint.h>
+#define CONTRIB_FUZZ_HAVE_STD_OPTIONAL
+#endif
+#endif
+#ifdef CONTRIB_FUZZ_HAVE_STD_OPTIONAL
+namespace contrib
+{
+using std::nullopt;
+using std::optional;
+} /* namespace contrib */
+#else
+#include "third_party/absl/types/optional.h"
+namespace contrib
+{
+using absl::nullopt;
+using absl::optional;
+} /* namespace contrib */
+#endif
 
 /* set DEBUG to 1 for a few debugging prints, or 2 for a lot */
 #define DEBUG 0
@@ -19,6 +42,6 @@
 };
 
 /* Split a non-zero-length input into two inputs. */
-std::optional<two_inputs> SplitInputs(const uint8_t *Data, size_t Size);
+contrib::optional<two_inputs> SplitInputs(const uint8_t *Data, size_t Size);
 
 #endif /* CONTRIB_FUZZ_FUZZUTIL_H */
diff --git a/contrib/fuzz/fuzzutil.cc b/contrib/fuzz/fuzzutil.cc
--- a/contrib/fuzz/fuzzutil.cc
+++ b/contrib/fuzz/fuzzutil.cc
@@ -2,10 +2,10 @@
 
 #include <utility>
 
-std::optional<two_inputs> SplitInputs(const uint8_t *Data, size_t Size)
+contrib::optional<two_inputs> SplitInputs(const uint8_t *Data, size_t Size)
 {
 	if (!Size) {
-		return std::nullopt;
+		return contrib::nullopt;
 	}
 	// figure out a random point in [0, Size] to split our input.
 	size_t left_size = (Data[0] / 255.0) * (Size - 1);
diff --git a/contrib/fuzz/Makefile b/contrib/fuzz/Makefile
--- a/contrib/fuzz/Makefile
+++ b/contrib/fuzz/Makefile
@@ -3,6 +3,10 @@
 	  -std=c++17 \
 	  -I../../mercurial -c -o fuzzutil.o fuzzutil.cc
 
+fuzzutil-oss-fuzz.o: fuzzutil.cc fuzzutil.h
+	$$CXX $$CXXFLAGS -std=c++17 \
+	  -I../../mercurial -c -o fuzzutil-oss-fuzz.o fuzzutil.cc
+
 bdiff.o: ../../mercurial/bdiff.c
 	$$CC $$CFLAGS -fsanitize=fuzzer-no-link,address -c -o bdiff.o \
 	  ../../mercurial/bdiff.c
@@ -15,9 +19,10 @@
 bdiff-oss-fuzz.o: ../../mercurial/bdiff.c
 	$$CC $$CFLAGS -c -o bdiff-oss-fuzz.o ../../mercurial/bdiff.c
 
-bdiff_fuzzer: bdiff.cc bdiff-oss-fuzz.o fuzzutil.o
+bdiff_fuzzer: bdiff.cc bdiff-oss-fuzz.o fuzzutil-oss-fuzz.o
 	$$CXX $$CXXFLAGS -std=c++17 -I../../mercurial bdiff.cc \
-	  bdiff-oss-fuzz.o fuzzutil.o -lFuzzingEngine -o $$OUT/bdiff_fuzzer
+	  bdiff-oss-fuzz.o fuzzutil-oss-fuzz.o -lFuzzingEngine -o \
+	  $$OUT/bdiff_fuzzer
 
 x%.o: ../../mercurial/thirdparty/xdiff/x%.c ../../mercurial/thirdparty/xdiff/*.h
 	$$CC -g -O1 -fsanitize=fuzzer-no-link,address -c \
@@ -34,9 +39,9 @@
 	  -o $@ \
 	  $<
 
-xdiff_fuzzer: xdiff.cc fuzz-xdiffi.o fuzz-xprepare.o fuzz-xutils.o fuzzutil.o
+xdiff_fuzzer: xdiff.cc fuzz-xdiffi.o fuzz-xprepare.o fuzz-xutils.o fuzzutil-oss-fuzz.o
 	$$CXX $$CXXFLAGS -std=c++17 -I../../mercurial xdiff.cc \
-	  fuzz-xdiffi.o fuzz-xprepare.o fuzz-xutils.o fuzzutil.o \
+	  fuzz-xdiffi.o fuzz-xprepare.o fuzz-xutils.o fuzzutil-oss-fuzz.o \
 	  -lFuzzingEngine -o $$OUT/xdiff_fuzzer
 
 all: bdiff xdiff



To: durin42, #hg-reviewers
Cc: indygreg, mercurial-devel


More information about the Mercurial-devel mailing list