Return gdb::optional<std::string> from target_fileio_readlink
authorTom Tromey <tom@tromey.com>
Thu, 23 Nov 2017 06:37:38 +0000 (23:37 -0700)
committerTom Tromey <tom@tromey.com>
Wed, 7 Mar 2018 22:36:28 +0000 (15:36 -0700)
This changes to_fileio_readlink and target_fileio_readlink to return a
gdb::optional<std::sring>, and then fixes up the callers and
implementations.  This allows the removal of some cleanups.

Regression tested by the buildbot.

gdb/ChangeLog
2018-03-07  Tom Tromey  <tom@tromey.com>

* linux-tdep.c (linux_info_proc): Update.
* target.h (struct target_ops) <to_fileio_readlink>: Return
optional<string>.
(target_fileio_readlink): Return optional<string>.
* remote.c (remote_hostio_readlink): Return optional<string>.
* inf-child.c (inf_child_fileio_readlink): Return
optional<string>.
* target.c (target_fileio_readlink): Return optional<string>.

gdb/ChangeLog
gdb/inf-child.c
gdb/linux-nat.c
gdb/linux-tdep.c
gdb/remote.c
gdb/target.c
gdb/target.h

index 928dbab31f9a04a42b203eca83b83abf7e02e490..49b91e1e1508b79207cc2621256a4cb2634ed311 100644 (file)
@@ -1,3 +1,14 @@
+2018-03-07  Tom Tromey  <tom@tromey.com>
+
+       * linux-tdep.c (linux_info_proc): Update.
+       * target.h (struct target_ops) <to_fileio_readlink>: Return
+       optional<string>.
+       (target_fileio_readlink): Return optional<string>.
+       * remote.c (remote_hostio_readlink): Return optional<string>.
+       * inf-child.c (inf_child_fileio_readlink): Return
+       optional<string>.
+       * target.c (target_fileio_readlink): Return optional<string>.
+
 2018-03-07  Andrew Burgess  <andrew.burgess@embecosm.com>
 
        * regcache.c (cooked_read_test): Add riscv to the list of
index 6875596f33677ddd06aeffc19ed3349858652f97..c7c45530b6bc38a54e60c55e258ec0c3ac2e4ee7 100644 (file)
@@ -333,7 +333,7 @@ inf_child_fileio_unlink (struct target_ops *self,
 
 /* Implementation of to_fileio_readlink.  */
 
-static char *
+static gdb::optional<std::string>
 inf_child_fileio_readlink (struct target_ops *self,
                           struct inferior *inf, const char *filename,
                           int *target_errno)
@@ -343,22 +343,18 @@ inf_child_fileio_readlink (struct target_ops *self,
 #if defined (PATH_MAX)
   char buf[PATH_MAX];
   int len;
-  char *ret;
 
   len = readlink (filename, buf, sizeof buf);
   if (len < 0)
     {
       *target_errno = host_to_fileio_error (errno);
-      return NULL;
+      return {};
     }
 
-  ret = (char *) xmalloc (len + 1);
-  memcpy (ret, buf, len);
-  ret[len] = '\0';
-  return ret;
+  return std::string (buf, len);
 #else
   *target_errno = FILEIO_ENOSYS;
-  return NULL;
+  return {};
 #endif
 }
 
index cc930c63347f8221aa344389a38dc214b54934cd..1bbad7bacc4a161f8b14f13caaebdbe581378daf 100644 (file)
@@ -4709,27 +4709,23 @@ linux_nat_fileio_open (struct target_ops *self,
 
 /* Implementation of to_fileio_readlink.  */
 
-static char *
+static gdb::optional<std::string>
 linux_nat_fileio_readlink (struct target_ops *self,
                           struct inferior *inf, const char *filename,
                           int *target_errno)
 {
   char buf[PATH_MAX];
   int len;
-  char *ret;
 
   len = linux_mntns_readlink (linux_nat_fileio_pid_of (inf),
                              filename, buf, sizeof (buf));
   if (len < 0)
     {
       *target_errno = host_to_fileio_error (errno);
-      return NULL;
+      return {};
     }
 
-  ret = (char *) xmalloc (len + 1);
-  memcpy (ret, buf, len);
-  ret[len] = '\0';
-  return ret;
+  return std::string (buf, len);
 }
 
 /* Implementation of to_fileio_unlink.  */
index d54e56ce8d0ec935b6e7e3e60b2a81e623875427..b5bb16215d658321d2466fd9d7ae026e6d7f32a2 100644 (file)
@@ -764,26 +764,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::optional<std::string> contents
+       = target_fileio_readlink (NULL, filename, &target_errno);
+      if (contents.has_value ())
+       printf_filtered ("cwd = '%s'\n", contents->c_str ());
       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::optional<std::string> contents
+       = target_fileio_readlink (NULL, filename, &target_errno);
+      if (contents.has_value ())
+       printf_filtered ("exe = '%s'\n", contents->c_str ());
       else
        warning (_("unable to read link '%s'"), filename);
     }
index 15d6c5bdbf1a81e7401aa9def79541416cb83fff..134a97eed98ff597ea5834b08861a9fd02eb8d6f 100644 (file)
@@ -11713,7 +11713,7 @@ remote_hostio_unlink (struct target_ops *self,
 
 /* Implementation of to_fileio_readlink.  */
 
-static char *
+static gdb::optional<std::string>
 remote_hostio_readlink (struct target_ops *self,
                        struct inferior *inf, const char *filename,
                        int *remote_errno)
@@ -11724,10 +11724,9 @@ remote_hostio_readlink (struct target_ops *self,
   int left = get_remote_packet_size ();
   int len, attachment_len;
   int read_len;
-  char *ret;
 
   if (remote_hostio_set_filesystem (inf, remote_errno) != 0)
-    return NULL;
+    return {};
 
   remote_buffer_add_string (&p, &left, "vFile:readlink:");
 
@@ -11739,16 +11738,15 @@ remote_hostio_readlink (struct target_ops *self,
                                    &attachment_len);
 
   if (len < 0)
-    return NULL;
+    return {};
 
-  ret = (char *) xmalloc (len + 1);
+  std::string ret (len, '\0');
 
   read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
-                                   (gdb_byte *) ret, len);
+                                   (gdb_byte *) &ret[0], len);
   if (read_len != len)
     error (_("Readlink returned %d, but %d bytes."), len, read_len);
 
-  ret[len] = '\0';
   return ret;
 }
 
index 197e7ff5420e63e04e70495b903595be45381c7d..2b97118217359f696ac40acced03e4e5bf5e2dd8 100644 (file)
@@ -3059,7 +3059,7 @@ target_fileio_unlink (struct inferior *inf, const char *filename,
 
 /* See target.h.  */
 
-char *
+gdb::optional<std::string>
 target_fileio_readlink (struct inferior *inf, const char *filename,
                        int *target_errno)
 {
@@ -3069,22 +3069,22 @@ 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::optional<std::string> 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->c_str () : "(nil)",
+                               ret ? 0 : *target_errno);
          return ret;
        }
     }
 
   *target_errno = FILEIO_ENOSYS;
-  return NULL;
+  return {};
 }
 
 static void
index de562139c74b44648eb58754cf42956716e92d23..fd6c30b98377a3156ad7285315ad053e60c6223d 100644 (file)
@@ -933,12 +933,12 @@ struct target_ops
     /* Read value of symbolic link FILENAME on the target, in the
        filesystem as seen by INF.  If INF is NULL, use the filesystem
        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);
+       stub).  Return a string, or an empty optional if an error
+       occurs (and set *TARGET_ERRNO).  */
+    gdb::optional<std::string> (*to_fileio_readlink) (struct target_ops *,
+                                                     struct inferior *inf,
+                                                     const char *filename,
+                                                     int *target_errno);
 
 
     /* Implement the "info proc" command.  */
@@ -2095,9 +2095,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::optional<std::string> 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
This page took 0.035775 seconds and 4 git commands to generate.