From: Alex Rousskov Date: Sun, 2 Apr 2023 07:42:10 +0000 (+0000) Subject: Fix GCC v13 build [-Warray-bounds] (#1318) X-Git-Tag: SQUID_7_0_1~457 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=6f6db8c5b76b4e49059cd348d0e7afb3616f0e6b;p=thirdparty%2Fsquid.git Fix GCC v13 build [-Warray-bounds] (#1318) 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). --- diff --git a/src/ipc/mem/FlexibleArray.h b/src/ipc/mem/FlexibleArray.h index 6a0a4360b3..53ecd39a74 100644 --- a/src/ipc/mem/FlexibleArray.h +++ b/src/ipc/mem/FlexibleArray.h @@ -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 #include 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(&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