public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Alan Modra <amodra@gmail.com>
To: binutils@sourceware.org
Subject: Sanity check read_section_stabs_debugging_info
Date: Tue, 14 Mar 2023 12:35:31 +1030	[thread overview]
Message-ID: <ZA/Wa/V9ZyhkIDlz@squeak.grove.modra.org> (raw)

	* rddbg.c (read_section_stabs_debugging_info): Exclude sections
	without contents.  Use bfd_malloc_and_get_section.  Don't alloc
	one extra for strings.

diff --git a/binutils/rddbg.c b/binutils/rddbg.c
index 036a74fc16a..f2ed6bcb3dd 100644
--- a/binutils/rddbg.c
+++ b/binutils/rddbg.c
@@ -117,39 +117,37 @@ read_section_stabs_debugging_info (bfd *abfd, asymbol **syms, long symcount,
 
       sec = bfd_get_section_by_name (abfd, names[i].secname);
       strsec = bfd_get_section_by_name (abfd, names[i].strsecname);
-      if (sec != NULL && strsec != NULL)
+      if (sec != NULL
+	  && (bfd_section_flags (sec) & SEC_HAS_CONTENTS) != 0
+	  && strsec != NULL
+	  && (bfd_section_flags (strsec) & SEC_HAS_CONTENTS) != 0)
 	{
 	  bfd_size_type stabsize, strsize;
 	  bfd_byte *stabs, *strings;
 	  bfd_byte *stab;
 	  bfd_size_type stroff, next_stroff;
 
-	  stabsize = bfd_section_size (sec);
-	  stabs = (bfd_byte *) xmalloc (stabsize);
-	  if (! bfd_get_section_contents (abfd, sec, stabs, 0, stabsize))
+	  if (!bfd_malloc_and_get_section (abfd, sec, &stabs))
 	    {
 	      fprintf (stderr, "%s: %s: %s\n",
 		       bfd_get_filename (abfd), names[i].secname,
 		       bfd_errmsg (bfd_get_error ()));
 	      free (shandle);
-	      free (stabs);
 	      return false;
 	    }
 
-	  strsize = bfd_section_size (strsec);
-	  strings = (bfd_byte *) xmalloc (strsize + 1);
-	  if (! bfd_get_section_contents (abfd, strsec, strings, 0, strsize))
+	  if (!bfd_malloc_and_get_section (abfd, strsec, &strings))
 	    {
 	      fprintf (stderr, "%s: %s: %s\n",
 		       bfd_get_filename (abfd), names[i].strsecname,
 		       bfd_errmsg (bfd_get_error ()));
 	      free (shandle);
-	      free (strings);
 	      free (stabs);
 	      return false;
 	    }
 	  /* Zero terminate the strings table, just in case.  */
-	  strings [strsize] = 0;
+	  strsize = bfd_section_size (strsec);
+	  strings [strsize - 1] = 0;
 	  if (shandle == NULL)
 	    {
 	      shandle = start_stab (dhandle, abfd, true, syms, symcount);
@@ -165,6 +163,7 @@ read_section_stabs_debugging_info (bfd *abfd, asymbol **syms, long symcount,
 
 	  stroff = 0;
 	  next_stroff = 0;
+	  stabsize = bfd_section_size (sec);
 	  /* PR 17512: file: 078-60391-0.001:0.1.  */
 	  for (stab = stabs; stab <= (stabs + stabsize) - 12; stab += 12)
 	    {

-- 
Alan Modra
Australia Development Lab, IBM

             reply	other threads:[~2023-03-14  2:05 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-14  2:05 Alan Modra [this message]
2023-03-16  0:55 Alan Modra

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=ZA/Wa/V9ZyhkIDlz@squeak.grove.modra.org \
    --to=amodra@gmail.com \
    --cc=binutils@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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).