]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Fix GCC v13 build [-Warray-bounds] (#1318)
authorAlex Rousskov <rousskov@measurement-factory.com>
Sun, 2 Apr 2023 07:42:10 +0000 (07:42 +0000)
committerSquid Anubis <squid-anubis@squid-cache.org>
Mon, 3 Apr 2023 21:04:17 +0000 (21:04 +0000)
    src/ipc/mem/FlexibleArray.h:34:52: error: array subscript -1 is
    below array bounds of 'int [1]' [-Werror=array-bounds]

We suspect this warning is a GCC v13 regression bug because the callers
marked as problematic by GCC (e.g., Rock::LoadingEntry::LoadingEntry) do
not use "array subscript -1", and the Ipc::StoreMapItems::at() operator
they use even asserts that the subscript is not negative. It might be
GCC bug 107699: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107699

This change replaces the fake one-item array hack with a properly
aligned byte (still used as the "start of the real array" marker).

Also removed some unused and problematic code (instead of polishing it).

src/ipc/mem/FlexibleArray.h

index 6a0a4360b32634e08ae8a14b28083019f5c1afc7..53ecd39a742576a682750234429d0c3148c48451 100644 (file)
@@ -9,7 +9,7 @@
 #ifndef SQUID_IPC_MEM_FLEXIBLE_ARRAY_H
 #define SQUID_IPC_MEM_FLEXIBLE_ARRAY_H
 
-// sometimes required for placement-new operator to be declared
+#include <cstddef>
 #include <new>
 
 namespace Ipc
@@ -27,20 +27,15 @@ class FlexibleArray
 {
 public:
     explicit FlexibleArray(const int capacity) {
-        if (capacity > 1) // the first item is initialized automatically
-            new (raw()+1) Item[capacity-1];
+        new (raw()) Item[capacity];
     }
 
-    Item &operator [](const int idx) { return items[idx]; }
-    const Item &operator [](const int idx) const { return items[idx]; }
+    Item &operator [](const int idx) { return *(raw() + idx); }
 
-    //const Item *operator ()() const { return items; }
-    //Item *operator ()() { return items; }
-
-    Item *raw() { return items; }
+    Item *raw() { return reinterpret_cast<Item*>(&start_); }
 
 private:
-    Item items[1]; // ensures proper alignment of array elements
+    alignas(Item) std::byte start_; ///< the first byte of the first array item
 };
 
 } // namespace Mem