Handle small_vectors with 0 inline capacity correctly
authorChristopher Dykes <cdykes@fb.com>
Tue, 19 Jul 2016 22:44:31 +0000 (15:44 -0700)
committerFacebook Github Bot <facebook-github-bot-bot@fb.com>
Tue, 19 Jul 2016 22:53:32 +0000 (15:53 -0700)
Summary: It is an error to attempt to get the size of a zero length array, such as `unsigned char[0]`, which is what you get if `MaxInline` has been passed in as 0. We can work around this by simply defining `InlineStorageType` to be `void*` if the size is exactly 0, which will result in the capacity correctly being stored out of line.

Reviewed By: yfeldblum

Differential Revision: D3572898

fbshipit-source-id: c96bb7cc6a890044bb74b0f6d5238c503552ee25

folly/small_vector.h

index 042961bc4a7ba15a39c82563b377f14815235fa8..da4547b40ae6712b12cf6d6ef90736cae1b9e4fb 100644 (file)
@@ -1038,14 +1038,20 @@ private:
   } FOLLY_PACK_ATTR;
 
 #if (FOLLY_X64 || FOLLY_PPC64)
-  typedef unsigned char InlineStorageType[sizeof(value_type) * MaxInline];
+  typedef unsigned char InlineStorageDataType[sizeof(value_type) * MaxInline];
 #else
   typedef typename std::aligned_storage<
     sizeof(value_type) * MaxInline,
     alignof(value_type)
-  >::type InlineStorageType;
+  >::type InlineStorageDataType;
 #endif
 
+  typedef typename std::conditional<
+    sizeof(value_type) * MaxInline != 0,
+    InlineStorageDataType,
+    void*
+  >::type InlineStorageType;
+
   static bool const kHasInlineCapacity =
     sizeof(HeapPtrWithCapacity) < sizeof(InlineStorageType);