Delete the non-char integeral forms of fbstring::operator=
authorChristopher Dykes <cdykes@fb.com>
Wed, 26 Apr 2017 00:14:26 +0000 (17:14 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Wed, 26 Apr 2017 00:23:41 +0000 (17:23 -0700)
Summary: They allow for assignments that make no sense, so make it impossible to do so.

Reviewed By: yfeldblum

Differential Revision: D4919606

fbshipit-source-id: 24d8e036eff33a8c6def4672c0d098f0edd5c5b3

folly/FBString.h
folly/test/FBStringTest.cpp

index 91b1ab684013f31597c0f96d9a8d2b9275608bae..45710dc79bbcb40bd52c1f47806d58c831891bcb 100644 (file)
@@ -1230,7 +1230,15 @@ public:
     return assign(s);
   }
 
-  basic_fbstring& operator=(value_type c);
+  // This actually goes directly against the C++ spec, but the
+  // value_type overload is dangerous, so we're explicitly deleting
+  // any overloads of operator= that could implicitly convert to
+  // value_type.
+  template <typename TP>
+  typename std::enable_if<
+      std::is_same<typename std::decay<TP>::type, value_type>::value,
+      basic_fbstring&>::type
+  operator=(TP c);
 
   basic_fbstring& operator=(std::initializer_list<value_type> il) {
     return assign(il.begin(), il.end());
@@ -1860,8 +1868,13 @@ inline basic_fbstring<E, T, A, S>& basic_fbstring<E, T, A, S>::operator=(
 }
 
 template <typename E, class T, class A, class S>
-inline basic_fbstring<E, T, A, S>& basic_fbstring<E, T, A, S>::operator=(
-    const value_type c) {
+template <typename TP>
+inline typename std::enable_if<
+    std::is_same<
+        typename std::decay<TP>::type,
+        typename basic_fbstring<E, T, A, S>::value_type>::value,
+    basic_fbstring<E, T, A, S>&>::type
+basic_fbstring<E, T, A, S>::operator=(TP c) {
   Invariant checker(*this);
 
   if (empty()) {
index 2c867dd1d230b8d283a815a6c14a698891960771..c2110d1f7830c1d0970cc4be10de0ef088d544d6 100644 (file)
@@ -208,7 +208,8 @@ template <class String> void clause11_21_4_2_lprime(String & test) {
 }
 template <class String> void clause11_21_4_2_m(String & test) {
   // Assignment from char
-  test = random('a', 'z');
+  using value_type = typename String::value_type;
+  test = random(static_cast<value_type>('a'), static_cast<value_type>('z'));
 }
 template <class String> void clause11_21_4_2_n(String & test) {
   // Assignment from initializer_list<char>