[scudo] Use template specialization on Quarantine to avoid zero-length array
authorDominic Chen <ddchen@apple.com>
Fri, 25 Mar 2022 23:44:31 +0000 (16:44 -0700)
committerDominic Chen <ddchen@apple.com>
Mon, 28 Mar 2022 23:36:25 +0000 (16:36 -0700)
Use a separate templated QuarantineBlocks class to avoid a zero-length array

Differential Revision: https://reviews.llvm.org/D122518

compiler-rt/lib/scudo/standalone/secondary.h

index abb58a2882affd93cfc63242f404db33b1c37080..0f8fafe0a6b90316cb0371a915b531663780f288 100644 (file)
@@ -362,6 +362,18 @@ private:
     u64 Time;
   };
 
+  // Template specialization to avoid producing zero-length array
+  template <size_t Size> class QuarantineBlocks {
+  public:
+    CachedBlock &operator[](uptr Idx) { return Blocks[Idx]; }
+  private:
+    CachedBlock Blocks[Size];
+  };
+  template <> class QuarantineBlocks<0> {
+  public:
+    CachedBlock &operator[](uptr UNUSED Idx) { UNREACHABLE("Unsupported!"); }
+  };
+
   void releaseIfOlderThan(CachedBlock &Entry, u64 Time) {
     if (!Entry.CommitBase || !Entry.Time)
       return;
@@ -395,7 +407,7 @@ private:
   atomic_s32 ReleaseToOsIntervalMs = {};
 
   CachedBlock Entries[Config::SecondaryCacheEntriesArraySize] = {};
-  CachedBlock Quarantine[Config::SecondaryCacheQuarantineSize] = {};
+  QuarantineBlocks<Config::SecondaryCacheQuarantineSize> Quarantine = {};
 };
 
 template <typename Config> class MapAllocator {