From patchwork Thu Nov 23 16:39:15 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 24477 Received: (qmail 77642 invoked by alias); 23 Nov 2017 16:39:21 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 77631 invoked by uid 89); 23 Nov 2017 16:39:21 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.5 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KB_WAM_FROM_NAME_SINGLEWORD, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=cwd, 94610 X-HELO: gateway33.websitewelcome.com Received: from gateway33.websitewelcome.com (HELO gateway33.websitewelcome.com) (192.185.145.24) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 23 Nov 2017 16:39:19 +0000 Received: from cm14.websitewelcome.com (cm14.websitewelcome.com [100.42.49.7]) by gateway33.websitewelcome.com (Postfix) with ESMTP id 01CC6CEE31A for ; Thu, 23 Nov 2017 10:39:18 -0600 (CST) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with SMTP id HuWzeV6o7HEImHuWzeXKGl; Thu, 23 Nov 2017 10:39:17 -0600 Received: from 71-218-90-63.hlrn.qwest.net ([71.218.90.63]:40702 helo=bapiya.Home) by box5379.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.89) (envelope-from ) id 1eHuWz-002esE-Oo; Thu, 23 Nov 2017 10:39:17 -0600 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [RFA] Return unique_xmalloc_ptr from target_fileio_readlink Date: Thu, 23 Nov 2017 09:39:15 -0700 Message-Id: <20171123163915.4604-1-tom@tromey.com> X-BWhitelist: no X-Source-L: No X-Exim-ID: 1eHuWz-002esE-Oo X-Source-Sender: 71-218-90-63.hlrn.qwest.net (bapiya.Home) [71.218.90.63]:40702 X-Source-Auth: tom+tromey.com X-Email-Count: 1 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes This changes to_fileio_readlink and target_fileio_readlink to return a unique_xmalloc_ptr, and then fixes up the callers and implementations. This allows the removal of some cleanups. I chose unique_xmalloc_ptr rather than std::string due to the NULL return -- although a symlink can't point to the empty string, so an empty string could be used instead, it seemed obscure to do so. Regression tested by the buildbot. ChangeLog 2017-11-23 Tom Tromey * linux-tdep.c (linux_info_proc): Update. * target.h (struct target_ops) : Return unique_xmalloc_ptr. (target_fileio_readlink): Return unique_xmalloc_ptr. * remote.c (remote_hostio_readlink): Return unique_xmalloc_ptr. * inf-child.c (inf_child_fileio_readlink): Return unique_xmalloc_ptr. * target.c (target_fileio_readlink): Return unique_xmalloc_ptr. --- gdb/ChangeLog | 11 +++++++++++ gdb/inf-child.c | 4 ++-- gdb/linux-nat.c | 4 ++-- gdb/linux-tdep.c | 22 ++++++++-------------- gdb/remote.c | 4 ++-- gdb/target.c | 10 +++++----- gdb/target.h | 13 ++++++------- 7 files changed, 36 insertions(+), 32 deletions(-) diff --git a/gdb/inf-child.c b/gdb/inf-child.c index a712613f08..58ce3385b2 100644 --- a/gdb/inf-child.c +++ b/gdb/inf-child.c @@ -333,7 +333,7 @@ inf_child_fileio_unlink (struct target_ops *self, /* Implementation of to_fileio_readlink. */ -static char * +static gdb::unique_xmalloc_ptr inf_child_fileio_readlink (struct target_ops *self, struct inferior *inf, const char *filename, int *target_errno) @@ -355,7 +355,7 @@ inf_child_fileio_readlink (struct target_ops *self, ret = (char *) xmalloc (len + 1); memcpy (ret, buf, len); ret[len] = '\0'; - return ret; + return gdb::unique_xmalloc_ptr (ret); #else *target_errno = FILEIO_ENOSYS; return NULL; diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c index a6395f4b5e..d11e44e568 100644 --- a/gdb/linux-nat.c +++ b/gdb/linux-nat.c @@ -4754,7 +4754,7 @@ linux_nat_fileio_open (struct target_ops *self, /* Implementation of to_fileio_readlink. */ -static char * +static gdb::unique_xmalloc_ptr linux_nat_fileio_readlink (struct target_ops *self, struct inferior *inf, const char *filename, int *target_errno) @@ -4774,7 +4774,7 @@ linux_nat_fileio_readlink (struct target_ops *self, ret = (char *) xmalloc (len + 1); memcpy (ret, buf, len); ret[len] = '\0'; - return ret; + return gdb::unique_xmalloc_ptr (ret); } /* Implementation of to_fileio_unlink. */ diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c index 24237b8d39..ce7d6a09d0 100644 --- a/gdb/linux-tdep.c +++ b/gdb/linux-tdep.c @@ -759,26 +759,20 @@ linux_info_proc (struct gdbarch *gdbarch, const char *args, if (cwd_f) { xsnprintf (filename, sizeof filename, "/proc/%ld/cwd", pid); - data = target_fileio_readlink (NULL, filename, &target_errno); - if (data) - { - struct cleanup *cleanup = make_cleanup (xfree, data); - printf_filtered ("cwd = '%s'\n", data); - do_cleanups (cleanup); - } + gdb::unique_xmalloc_ptr contents + = target_fileio_readlink (NULL, filename, &target_errno); + if (contents) + printf_filtered ("cwd = '%s'\n", contents.get ()); else warning (_("unable to read link '%s'"), filename); } if (exe_f) { xsnprintf (filename, sizeof filename, "/proc/%ld/exe", pid); - data = target_fileio_readlink (NULL, filename, &target_errno); - if (data) - { - struct cleanup *cleanup = make_cleanup (xfree, data); - printf_filtered ("exe = '%s'\n", data); - do_cleanups (cleanup); - } + gdb::unique_xmalloc_ptr contents + = target_fileio_readlink (NULL, filename, &target_errno); + if (contents) + printf_filtered ("exe = '%s'\n", contents.get ()); else warning (_("unable to read link '%s'"), filename); } diff --git a/gdb/remote.c b/gdb/remote.c index 62ac055119..3434af6f3c 100644 --- a/gdb/remote.c +++ b/gdb/remote.c @@ -11767,7 +11767,7 @@ remote_hostio_unlink (struct target_ops *self, /* Implementation of to_fileio_readlink. */ -static char * +static gdb::unique_xmalloc_ptr remote_hostio_readlink (struct target_ops *self, struct inferior *inf, const char *filename, int *remote_errno) @@ -11803,7 +11803,7 @@ remote_hostio_readlink (struct target_ops *self, error (_("Readlink returned %d, but %d bytes."), len, read_len); ret[len] = '\0'; - return ret; + return gdb::unique_xmalloc_ptr (ret); } /* Implementation of to_fileio_fstat. */ diff --git a/gdb/target.c b/gdb/target.c index 2e02a774e6..ae77ce9b76 100644 --- a/gdb/target.c +++ b/gdb/target.c @@ -2963,7 +2963,7 @@ target_fileio_unlink (struct inferior *inf, const char *filename, /* See target.h. */ -char * +gdb::unique_xmalloc_ptr target_fileio_readlink (struct inferior *inf, const char *filename, int *target_errno) { @@ -2973,16 +2973,16 @@ target_fileio_readlink (struct inferior *inf, const char *filename, { if (t->to_fileio_readlink != NULL) { - char *ret = t->to_fileio_readlink (t, inf, filename, - target_errno); + gdb::unique_xmalloc_ptr ret + = t->to_fileio_readlink (t, inf, filename, target_errno); if (targetdebug) fprintf_unfiltered (gdb_stdlog, "target_fileio_readlink (%d,%s)" " = %s (%d)\n", inf == NULL ? 0 : inf->num, - filename, ret? ret : "(nil)", - ret? 0 : *target_errno); + filename, ret ? ret.get () : "(nil)", + ret ? 0 : *target_errno); return ret; } } diff --git a/gdb/target.h b/gdb/target.h index 1683af64d5..7e3c58b5e0 100644 --- a/gdb/target.h +++ b/gdb/target.h @@ -946,10 +946,10 @@ struct target_ops seen by the debugger (GDB or, for remote targets, the remote stub). Return a null-terminated string allocated via xmalloc, or NULL if an error occurs (and set *TARGET_ERRNO). */ - char *(*to_fileio_readlink) (struct target_ops *, - struct inferior *inf, - const char *filename, - int *target_errno); + gdb::unique_xmalloc_ptr (*to_fileio_readlink) (struct target_ops *, + struct inferior *inf, + const char *filename, + int *target_errno); /* Implement the "info proc" command. */ @@ -2109,9 +2109,8 @@ extern int target_fileio_unlink (struct inferior *inf, by the debugger (GDB or, for remote targets, the remote stub). Return a null-terminated string allocated via xmalloc, or NULL if an error occurs (and set *TARGET_ERRNO). */ -extern char *target_fileio_readlink (struct inferior *inf, - const char *filename, - int *target_errno); +extern gdb::unique_xmalloc_ptr target_fileio_readlink + (struct inferior *inf, const char *filename, int *target_errno); /* Read target file FILENAME, in the filesystem as seen by INF. If INF is NULL, use the filesystem seen by the debugger (GDB or, for