[binutils-gdb] [gdb/build] Reimplement Wstringop-overread workaround

Tom de Vries via Gdb-cvs <[email protected]>
Newsgroups gmane.comp.gdb.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=0466831f8b8980d7313bdf1922990ae7411fdf57

commit 0466831f8b8980d7313bdf1922990ae7411fdf57
Author: Tom de Vries <[email protected]>
Date:   Thu Apr 16 12:55:17 2026 +0200

    [gdb/build] Reimplement Wstringop-overread workaround
    
    While working on commit 391c4026573 ("[gdb] Simplify debuginfod_is_enabled") I
    noticed this Wstringop-overread workaround:
    ...
          url_view = url_view.substr (off);
          /* g++ 11.2.1 on s390x, g++ 11.3.1 on ppc64le and g++ 11 on
             hppa seem convinced url_view might be of SIZE_MAX length.
             And so complains because the length of an array can only
             be PTRDIFF_MAX.  */
          DIAGNOSTIC_PUSH
          DIAGNOSTIC_IGNORE_STRINGOP_OVERREAD
          off = url_view.find_first_of (' ');
          DIAGNOSTIC_POP
    ...
    
    I had difficulty understanding how the warning got triggered, and why it was
    ok to ignore it, so I investigated this and ended up filing a gcc PR [1].
    
    While doing so, I realized that this:
    ...
    -      url_view = url_view.substr (off);
    +      url_view = url_view.substr (off, PTRDIFF_MAX);
    ...
    is a simpler workaround, that:
    - is not specific to the warning and also
    - states explicitly what the assumption is we're making.
    
    I ended up using this instead to make the workaround part more minimal:
    ...
           url_view = url_view.substr (off);
    +      url_view = url_view.substr (0, PTRDIFF_MAX);
           off = url_view.find_first_of (' ');
    ...
    
    The gcc PR got closed because it's supposed to be fixed in 12.1, so the
    workaround is enabled only for g++ < 12.1.
    
    Tested on x86_64-linux.
    
    Approved-By: Tom Tromey <[email protected]>
    
    [1] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124879

Diff:
---
 gdb/debuginfod-support.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/gdb/debuginfod-support.c b/gdb/debuginfod-support.c
index 40b816c5a40..0d666692ff8 100644
--- a/gdb/debuginfod-support.c
+++ b/gdb/debuginfod-support.c
@@ -249,14 +249,17 @@ debuginfod_is_enabled ()
       if (off == std::string_view::npos)
 	break;
       url_view = url_view.substr (off);
-      /* g++ 11.2.1 on s390x, g++ 11.3.1 on ppc64le and g++ 11 on
-	 hppa seem convinced url_view might be of SIZE_MAX length.
-	 And so complains because the length of an array can only
-	 be PTRDIFF_MAX.  */
-      DIAGNOSTIC_PUSH
-      DIAGNOSTIC_IGNORE_STRINGOP_OVERREAD
+#if defined (__GNUC__) && !defined (__clang__)				\
+  && ((__GNUC__ <= 11) || (__GNUC__ == 12 && __GNUC_MINOR__ < 1))
+      /* With g++ 11, we encounter a Wstringop-overread in
+	 url_view.find_first_of.  G++ seems convinced url_view might be of
+	 SIZE_MAX length here.  And so complains because the length of an
+	 array can only be PTRDIFF_MAX.  Work around this by explicitly
+	 limiting the size of url_view to PTRDIFF_MAX.  This is supposed to be
+	 fixed in GCC 12.1, see PR gcc/124879.  */
+      url_view = url_view.substr (0, PTRDIFF_MAX);
+#endif
       off = url_view.find_first_of (' ');
-      DIAGNOSTIC_POP
       gdb_printf
 	(_("  <%ps>\n"),
 	 styled_string (file_name_style.style (),
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.