[PATCH v3] Print absolute paths in printpathn when -yy is used

Zubin Mithra zubin.mithra at gmail.com
Tue Jun 17 06:24:11 UTC 2014


Hi Dmitry, Mike,

Thank you very much for the review! :)

>
> all that said, this is exactly the sort of thing asprintf() was invented.  so
> really we should delete all this logic and do:
>         char *path = asprintf("/proc/%u/cwd", tcp->pid);
>         if (!path)
>                 return false;
>         ... do stuff with path ...
>         free(path);


I've modified the function to use asprintf as follows :-

diff --git a/util.c b/util.c
> cd -                                                                                               ~@zm-ubuntu64
diff --git a/util.c b/util.c
index 9f9c774..1e53dec 100644
--- a/util.c
+++ b/util.c
@@ -570,12 +570,15 @@ string_quote(const char *instr, char *outstr,
long len, int size)
 static bool
 print_tracee_cwd(struct tcb *tcp)
 {
-       int link_size = sizeof("/proc//cwd") + sizeof(int) * 3;
-       char linkpath[link_size];
+       char *linkpath = NULL;
        char cwd[MAXPATHLEN+2];
+       int retval;
        ssize_t n;

-       snprintf(linkpath, link_size, "/proc/%u/cwd", tcp->pid);
+       retval = asprintf(&linkpath, "/proc/%u/cwd", tcp->pid);
+       if (retval == -1)
+               return false;
+
        n = readlink(linkpath, cwd, MAXPATHLEN);

        if (n > 0) {
@@ -589,8 +592,10 @@ print_tracee_cwd(struct tcb *tcp)
                string_quote(cwd, outstr, -1, n);
                outstr[strlen(outstr)-1] = '\0'; /* Don't print the
closing quotes */
                tprints(outstr);
+               free(linkpath);
                return true;
        }
+       free(linkpath);
        return false;
 }



-- zm




More information about the Strace-devel mailing list