Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Tom de Vries <tdevries@suse.de>
To: gdb-patches@sourceware.org
Subject: [PATCH] [gdb/build] Fix Wstringop-overflow
Date: Sat, 24 Oct 2026 01:09:30 +0200	[thread overview]
Message-ID: <20261023230930.1834289-1-tdevries@suse.de> (raw)

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


                 reply	other threads:[~2026-10-23 23:10 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261023230930.1834289-1-tdevries@suse.de \
    --to=tdevries@suse.de \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox