[gdb/build] Reimplement Wstringop-overread workaround

While working on commit 391c402657 ("[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 <tom@tromey.com>

[1] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124879
This commit is contained in:
Tom de Vries 2026-04-16 12:55:17 +02:00
parent c51032987a
commit 0466831f8b

View file

@ -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 (),