This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH 02/10] remote: Eliminate remote_hostio_close_cleanup


On 05/16/2018 06:37 PM, Tom Tromey wrote:
>>>>>> "Pedro" == Pedro Alves <palves@redhat.com> writes:
> 
> Pedro> +  ~scoped_remote_fd ()
> Pedro> +  {
> Pedro> +    if (m_fd != -1)
> Pedro> +      {
> Pedro> +	int remote_errno;
> Pedro> +	remote_hostio_close (find_target_at (process_stratum),
> Pedro> +			     m_fd, &remote_errno);
> 
> The only danger here is if remote_hostio_close can throw.  However, this
> was already a danger (in theory) before the patch -- there is a rule
> (perhaps unwritten and/or unenforced) that one cannot throw during a
> "do_cleanups".

Yeah, a cleanup throwing leaves things in a bad state.

I've been ignoring this whole "destructors shouldn't throw" thing
in RAII-fying patches for that reason, but it wouldn't hurt to
start handling it properly.

I don't think there's anything that can be done in this case other than
swallowing the exception.  We already call remote_hostio_close
explicitly in the normal paths, so the destructor trying to close
the file is just for the already-unwinding-due-to-some-other-exception
case.

> I didn't look but some assurance that this can't happen would be good to
> have.

So it seems to me that the below would be the way to go.  WDYT?

diff --git a/gdb/remote.c b/gdb/remote.c
index bc4815c67e..9761332ee4 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -12213,8 +12213,17 @@ public:
     if (m_fd != -1)
       {
 	int remote_errno;
-	remote_hostio_close (find_target_at (process_stratum),
-			     m_fd, &remote_errno);
+	try
+	  {
+	    remote_hostio_close (find_target_at (process_stratum),
+				 m_fd, &remote_errno);
+	  }
+	catch (...)
+	  {
+	    /* Swallow exception before it escapes the dtor.  If
+	       something goes wrong, likely the connection is gone,
+	       and there's nothing else that can be done.  */
+	  }
       }
   }
 
-- 
2.14.3


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]