This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
Re: [RFA 09/23] Remove close cleanup
- From: Pedro Alves <palves at redhat dot com>
- To: Tom Tromey <tom at tromey dot com>, gdb-patches at sourceware dot org
- Date: Fri, 2 Jun 2017 19:08:55 +0100
- Subject: Re: [RFA 09/23] Remove close cleanup
- Authentication-results: sourceware.org; auth=none
- Authentication-results: ext-mx07.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com
- Authentication-results: ext-mx07.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=palves at redhat dot com
- Dkim-filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 3B837C0467CB
- Dmarc-filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 3B837C0467CB
- References: <20170503224626.2818-1-tom@tromey.com> <20170503224626.2818-10-tom@tromey.com>
Patch looks good.
The bit below made me stop for a second:
On 05/03/2017 11:46 PM, Tom Tromey wrote:
> @@ -901,64 +900,59 @@ linux_mntns_access_fs (pid_t pid)
> if (ns == NULL)
> return MNSH_FS_DIRECT;
>
> - old_chain = make_cleanup (null_cleanup, NULL);
> -
> fd = gdb_open_cloexec (linux_ns_filename (ns, pid), O_RDONLY, 0);
> if (fd < 0)
> - goto error;
> -
> - make_cleanup_close (fd);
> + return MNSH_FS_ERROR;
>
> - if (fstat (fd, &sb) != 0)
> - goto error;
> + /* Introduce a new scope here so we can reset errno after
> + closing. */
> + {
> + gdb::fd_closer close_fd (fd);
>
Would the following be too clever?
/* Restore errno on exit to the value saved by the
last save() call. Ctor saves. */
struct scoped_errno_restore
{
scoped_errno_restore () { save (); }
~scoped_errno_restore () { errno = m_value; }
/* Take a snapshot of current errno. */
void save () { m_value = errno; }
private:
int m_value;
};
scoped_errno_restore restore_errno;
gdb::fd_closer close_fd (fd);
/* Save errno and return MNSH_FS_ERROR. */
auto fs_err = [&] ()
{
restore_errno.save ();
return MNSH_FS_ERROR;
};
if (fstat (fd, &sb) != 0)
return fs_err ();
(...)
helper = linux_mntns_get_helper ();
if (helper == NULL)
return fs_err ();
size = mnsh_send_setns (helper, fd, 0);
if (size < 0)
return fs_err ();
[It gets rid of both the scope, and the gotos.]
Thanks,
Pedro Alves