Skip to content

Suppress GCC 16 false-positive -Warray-bounds warning in SmallVector::data_end() - #59

Closed
alt-graph with Copilot wants to merge 1 commit into
mainfrom
copilot/fix-smallvector-warnings
Closed

Suppress GCC 16 false-positive -Warray-bounds warning in SmallVector::data_end()#59
alt-graph with Copilot wants to merge 1 commit into
mainfrom
copilot/fix-smallvector-warnings

Conversation

Copilot AI commented Sep 2, 2026

Copy link
Copy Markdown

GCC 16.2.1 emits spurious -Warray-bounds warnings in gul17::SmallVector::data_end() when it's inlined through deep std::variant construction chains, such as those used in DOOCS clientlib's doocs::Field. Clang does not reproduce the warning.

Root cause

  • The warning is a false positive: GCC's interprocedural array-bounds analysis loses track of the true object type/size while inlining across std::variant's placement-new constructors, and misattributes the pointer arithmetic in data_end() (data_ptr_ + size_) to an unrelated, smaller object.
  • The pointer arithmetic itself is sound — data_ptr_ always points to storage sized capacity_ (internal SBO array or heap allocation), and size_ <= capacity_ is a class invariant maintained throughout.
  • Verified with reproducer programs on g++-13/g++-14 at -O2 -Wall -Wextra -Warray-bounds=2: no warnings, correct runtime behavior for move construction and SBO-heavy usage patterns.

Fix

  • Wrapped both data_end() overloads in include/gul17/SmallVector.h with a GCC-only #pragma GCC diagnostic push / ignored "-Warray-bounds" / pop, guarded by defined(__GNUC__) && !defined(__clang__).
  • Follows the existing pragma-guarding convention already used in include/gul17/date.h.
  • No logic or behavior changes; Clang builds are unaffected.

Copilot AI linked an issue Sep 2, 2026 that may be closed by this pull request
Copilot AI changed the title [WIP] Fix SmallVector warnings with GCC 16.2.1 Suppress GCC 16 false-positive -Warray-bounds warning in SmallVector::data_end() Sep 2, 2026
Copilot AI requested a review from alt-graph September 2, 2026 06:58
@soerengrunewald

Copy link
Copy Markdown
Contributor

This is the patch I carry arround. Very similar, plus another spot. As mentioned in the issue, I had to cut the error messages, since gitlab has a length limit for text in a single entry:

diff --git a/include/gul17/SmallVector.h b/include/gul17/SmallVector.h
index 4810da0..31c6f7e 100644
--- a/include/gul17/SmallVector.h
+++ b/include/gul17/SmallVector.h
@@ -1304,7 +1304,10 @@ private:
     /// Return a non-dereferenceable pointer past the last element.
     constexpr ValueType* data_end() noexcept
     {
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Warray-bounds"
         return data_ptr_ + size_;
+#pragma GCC diagnostic pop
     }

     /// Return a non-dereferenceable pointer past the last element.
@@ -1648,7 +1651,13 @@ private:

         while (src != src_end)
         {
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Warray-bounds"
+#ifndef __clang__
+#pragma GCC diagnostic ignored "-Wstringop-overflow"
+#endif
             ::new (static_cast<void*>(dest)) ValueType(std::move(*src));
+#pragma GCC diagnostic pop
             ++src;
             ++dest;
         }

Also I'm not sure if we need the extra #if defined(__GNUC__) && !defined(__clang__) guards. Since pragma GCC, is normally only evaluated by GCC (and clang for compatibility reasons 🙄). But MSVC should not evaluate it, right?

@alt-graph

Copy link
Copy Markdown
Member

Also I'm not sure if we need the extra #if defined(__GNUC__) && !defined(__clang__) guards. Since pragma GCC, is normally only evaluated by GCC (and clang for compatibility reasons 🙄). But MSVC should not evaluate it, right?

Unfortunately Clang seems to emulate GCC pragmas (https://clang.llvm.org/docs/UsersManual.html):

Clang supports GCC’s pragma for compatibility with existing source code, so #pragma GCC diagnostic and #pragma clang diagnostic are synonyms for Clang. GCC will ignore #pragma clang diagnostic, though.

So I propose we leave the Copilot patch as it is. I'll just make the comment more concise. Your second suppression belongs into a separate PR, I think.

@alt-graph

Copy link
Copy Markdown
Member

I guess one could ask if we should suppress the warning here or around the "problematic" use case in the clientlib. But since this particular false positive has already cost us at least 3 issues across at least two projects PLUS time discussing it, I think we should suppress it in GUL17.

[why]
GCC 16 emits a spurious -Warray-bounds warning in SmallVector::data_end()
when it is inlined through deep std::variant construction chains, such as
those as those used in the doocs::Field class. Clang does not reproduce
the warning, and the address sanitizer confirms that the generated code
is OK.

[how]
Use pragmas for gcc to suppress the warning.
@alt-graph
alt-graph force-pushed the copilot/fix-smallvector-warnings branch from b6126dc to bd2fe9a Compare September 2, 2026 13:11
@alt-graph
alt-graph marked this pull request as ready for review September 2, 2026 13:11
@alt-graph
alt-graph self-requested a review September 2, 2026 13:15
@alt-graph

Copy link
Copy Markdown
Member

Merged by hand, so I'll close this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SmallVector warnings with GCC 16.2.1

3 participants