Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [PATCH 1/5] Reject null pointers in program_space::set_exec_bfd
Date: Tue, 10 Nov 2026 19:06:55 -0700	[thread overview]
Message-ID: <20261110-minor-assert-v1-1-bb33660ca557@tromey.com> (raw)
In-Reply-To: <20261110-minor-assert-v1-0-bb33660ca557@tromey.com>

The sole caller of set_exec_bfd ensures that the passed-in BFD cannot
be nullptr.  It seemed to me that the method itself should assert this
as well, as clearing the exec BFD requires a bit of extra work -- see
program_space::exec_close.

This also changes set_exec_bfd to require the precondition that there
is currently no exec BFD; again, if there was one, exec_close would
need to be called.
---
 gdb/exec.c      | 6 +++---
 gdb/progspace.h | 2 ++
 2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/gdb/exec.c b/gdb/exec.c
index 806f9b0a67a..4f0a0ca23e3 100644
--- a/gdb/exec.c
+++ b/gdb/exec.c
@@ -465,13 +465,13 @@ exec_file_attach (const char *filename, int from_tty)
 			      FOPEN_RUB, scratch_chan);
       else
 	temp = gdb_bfd_open (canonical_pathname, gnutarget, scratch_chan);
-      current_program_space->set_exec_bfd (std::move (temp));
-
-      if (!current_program_space->exec_bfd ())
+      if (temp == nullptr)
 	error (_("\"%ps\": could not open as an executable file: %s."),
 	       styled_string (file_name_style.style (), scratch_pathname),
 	       bfd_errmsg (bfd_get_error ()));
 
+      current_program_space->set_exec_bfd (std::move (temp));
+
       /* gdb_realpath_keepfile resolves symlinks on the local
 	 filesystem and so cannot be used for "target:" files.  */
       gdb_assert (current_program_space->exec_filename () == nullptr);
diff --git a/gdb/progspace.h b/gdb/progspace.h
index a5ef3deb337..d0d8466a8e5 100644
--- a/gdb/progspace.h
+++ b/gdb/progspace.h
@@ -312,6 +312,8 @@ struct program_space
   /* Set the exec BFD for this program space to ABFD.  */
   void set_exec_bfd (gdb_bfd_ref_ptr &&abfd)
   {
+    gdb_assert (ebfd == nullptr);
+    gdb_assert (abfd != nullptr);
     ebfd = std::move (abfd);
   }
 

-- 
2.49.0


  reply	other threads:[~2026-11-11  2:08 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-11  2:06 [PATCH 0/5] Minor updates to program space and setting the exec file Tom Tromey
2026-11-11  2:06 ` Tom Tromey [this message]
2026-11-11  2:06 ` [PATCH 2/5] Hoist call to gdb_bfd_check_format_matches in exec.c Tom Tromey
2026-11-11  2:06 ` [PATCH 3/5] Have program_space::set_exec_bfd do more work Tom Tromey
2026-11-11  2:06 ` [PATCH 4/5] Mark two program_space fields as private Tom Tromey
2026-11-11  2:06 ` [PATCH 5/5] Add some uses of bool to progspace and exec_file_attach Tom Tromey

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=20261110-minor-assert-v1-1-bb33660ca557@tromey.com \
    --to=tom@tromey.com \
    --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