Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH] [gdb/build] Fix Wstringop-overflow
@ 2026-10-23 23:09 Tom de Vries
  0 siblings, 0 replies; only message in thread
From: Tom de Vries @ 2026-10-23 23:09 UTC (permalink / raw)
  To: gdb-patches

I built gdb with -O3 -flto and --disable-nls on aarch64-linux, and ran into:
...
In function 'core_process_module_section',
    inlined from 'windows_core_xfer_shared_libraries' at
    gdb/windows-tdep.c:1218:33:
gdb/windows-tdep.c:1173:15: warning: writing 1 byte into a region ofsize 0 \
  [-Wstringop-overflow=]
 1173 |   buf.back () = 0;
      |               ^
...

The code triggering this is:
...
  gdb::byte_vector buf (bfd_section_size (sect) + 1);
  if (!bfd_get_section_contents (abfd, sect,
                                 buf.data (), 0, bfd_section_size (sect)))
    return;
  buf.back () = 0;
...

The warning triggers about the (unlikely) possibility that
"bfd_section_size (sect)" is UINT64_MAX, so effectively we have
"gdb::byte_vector buf (0)", which makes buf.back () undefined behavior.

Fix the warning by dropping the "+ 1" and using push_back instead of back.

Likewise in two other spots.

Tested on aarch64-linux.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34878
---
 gdb/corelow.c      | 4 ++--
 gdb/windows-tdep.c | 4 ++--
 gdb/xml-support.c  | 4 ++--
 3 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/gdb/corelow.c b/gdb/corelow.c
index cd36882a12c..86ab0b1257a 100644
--- a/gdb/corelow.c
+++ b/gdb/corelow.c
@@ -1724,13 +1724,13 @@ core_target::read_description ()
 	tdesc_note_size = bfd_section_size (tdesc_note_section);
       if (tdesc_note_size > 0)
 	{
-	  gdb::char_vector contents (tdesc_note_size + 1);
+	  gdb::char_vector contents (tdesc_note_size);
 	  if (bfd_get_section_contents (m_core_bfd.get (), tdesc_note_section,
 					contents.data (), (file_ptr) 0,
 					tdesc_note_size))
 	    {
 	      /* Ensure we have a null terminator.  */
-	      contents[tdesc_note_size] = '\0';
+	      contents.push_back ('\0');
 	      const struct target_desc *result
 		= string_read_description_xml (contents.data ());
 	      if (result != nullptr)
diff --git a/gdb/windows-tdep.c b/gdb/windows-tdep.c
index e9bf4398d83..dcab6afde94 100644
--- a/gdb/windows-tdep.c
+++ b/gdb/windows-tdep.c
@@ -1164,13 +1164,13 @@ core_process_module_section (bfd *abfd, asection *sect, cpms_data &data)
   if (!startswith (sect->name, ".module"))
     return;
 
-  gdb::byte_vector buf (bfd_section_size (sect) + 1);
+  gdb::byte_vector buf (bfd_section_size (sect));
   if (!bfd_get_section_contents (abfd, sect,
 				 buf.data (), 0, bfd_section_size (sect)))
     return;
   /* We're going to treat part of the buffer as a string, so make sure
      it is NUL-terminated.  */
-  buf.back () = 0;
+  buf.push_back (0);
 
   /* A DWORD (data_type) followed by struct windows_core_module_info.  */
   if (bfd_section_size (sect) < 4)
diff --git a/gdb/xml-support.c b/gdb/xml-support.c
index 0e33ce54d12..4467f395f9a 100644
--- a/gdb/xml-support.c
+++ b/gdb/xml-support.c
@@ -985,7 +985,7 @@ xml_fetch_content_from_file (const char *filename, const char *dirname)
   len = ftell (file.get ());
   rewind (file.get ());
 
-  gdb::char_vector text (len + 1);
+  gdb::char_vector text (len);
 
   if (fread (text.data (), 1, len, file.get ()) != len
       || ferror (file.get ()))
@@ -995,7 +995,7 @@ xml_fetch_content_from_file (const char *filename, const char *dirname)
       return {};
     }
 
-  text.back () = '\0';
+  text.push_back ('\0');
   return text;
 }
 

base-commit: 44bc3b1dc42a8b5a3b472320eda15cc440b66966
-- 
2.51.0


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-10-23 23:10 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-23 23:09 [PATCH] [gdb/build] Fix Wstringop-overflow Tom de Vries

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox