Disallow nullptr literal in StringPiece constructor
authorAnton Likhtarov <alikhtarov@fb.com>
Tue, 3 Nov 2015 18:45:21 +0000 (10:45 -0800)
committerfacebook-github-bot-1 <folly-bot@fb.com>
Tue, 3 Nov 2015 19:20:21 +0000 (11:20 -0800)
Summary: This disallows implicitly constructing a StringPiece from a literal nullptr at compile time
(without this change, nullptr would cause a segfault in strlen()).

Reviewed By: meyering, andriigrynenko

Differential Revision: D2603597

fb-gh-sync-id: cafbc45945bacc72a7c89310b99aa62d19a3ff9f

folly/Conv.h
folly/Range.h

index 4acb9c1e40aa995da9de2477a7ac8264b90cec5b..f50bde71de84259f55ce686d3ebbcec3fbfcf0b0 100644 (file)
@@ -388,6 +388,11 @@ estimateSpaceNeeded(Src value) {
   return folly::StringPiece(value).size();
 }
 
+template<>
+inline size_t estimateSpaceNeeded(std::nullptr_t value) {
+  return 0;
+}
+
 template<class Src>
 typename std::enable_if<
   std::is_pointer<Src>::value &&
index 8e51760728a6e56817e98bf23d435fe483322672..c72288e262aba3f46c95e7a38fdb0284f024c017 100644 (file)
@@ -27,6 +27,7 @@
 #include <algorithm>
 #include <boost/operators.hpp>
 #include <climits>
+#include <cstddef>
 #include <cstring>
 #include <glog/logging.h>
 #include <iosfwd>
@@ -200,6 +201,12 @@ public:
   constexpr Range(Iter start, size_t size)
       : b_(start), e_(start + size) { }
 
+# if !defined(__clang__) || __clang_major__ > 3 || \
+  (__clang_major__ == 3 && __clang_minor__ > 6)
+  // Clang 3.6 crashes on this line
+  /* implicit */ Range(std::nullptr_t) = delete;
+# endif
+
   template <class T = Iter, typename detail::IsCharPointer<T>::type = 0>
   constexpr /* implicit */ Range(Iter str)
       : b_(str), e_(str + constexpr_strlen(str)) {}