]> granicus.if.org Git - strace/commitdiff
Stop using a large static buffer in getfdpath
authorDenys Vlasenko <vda.linux@googlemail.com>
Wed, 6 Mar 2013 17:24:34 +0000 (18:24 +0100)
committerDenys Vlasenko <vda.linux@googlemail.com>
Wed, 6 Mar 2013 17:24:34 +0000 (18:24 +0100)
   text    data     bss     dec     hex filename
 245075     680    9836  255591   3e667 strace_old
 245143     680    5708  251531   3d68b strace

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
defs.h
pathtrace.c
util.c

diff --git a/defs.h b/defs.h
index ca5d0883f4a1b594bbe6b2df76c4ad7a7b1cf4b6..c20fcc6160f76c53351ca43438582e432c226660 100644 (file)
--- a/defs.h
+++ b/defs.h
@@ -605,7 +605,7 @@ extern const char *signame(int);
 extern int is_restart_error(struct tcb *);
 extern void pathtrace_select(const char *);
 extern int pathtrace_match(struct tcb *);
-extern const char *getfdpath(struct tcb *, int);
+extern int getfdpath(struct tcb *, int, char *, unsigned);
 
 extern const char *xlookup(const struct xlat *, int);
 
index 95643c673ae8ab2526b0d08fe4de915833aa95eb..d4c2dc43cb519805c178f875a6b9c261bcb55617 100644 (file)
@@ -73,9 +73,10 @@ upathmatch(struct tcb *tcp, unsigned long upath)
 static int
 fdmatch(struct tcb *tcp, int fd)
 {
-       const char *path = getfdpath(tcp, fd);
+       char path[PATH_MAX + 1];
+       int n = getfdpath(tcp, fd, path, sizeof(path));
 
-       return path && pathmatch(path);
+       return n >= 0 && pathmatch(path);
 }
 
 /*
@@ -100,22 +101,24 @@ storepath(const char *path)
 /*
  * Get path associated with fd.
  */
-const char *
-getfdpath(struct tcb *tcp, int fd)
+int
+getfdpath(struct tcb *tcp, int fd, char *buf, unsigned bufsize)
 {
-       static char path[PATH_MAX+1];
        char linkpath[sizeof("/proc/%u/fd/%u") + 2 * sizeof(int)*3];
        ssize_t n;
 
        if (fd < 0)
-               return NULL;
+               return -1;
 
        sprintf(linkpath, "/proc/%u/fd/%u", tcp->pid, fd);
-       n = readlink(linkpath, path, (sizeof path) - 1);
-       if (n <= 0)
-               return NULL;
-       path[n] = '\0';
-       return path;
+       n = readlink(linkpath, buf, bufsize - 1);
+       /*
+        * NB: if buf is too small, readlink doesn't fail,
+        * it returns truncated result (IOW: n == bufsize - 1).
+        */
+       if (n >= 0)
+               buf[n] = '\0';
+       return n;
 }
 
 /*
diff --git a/util.c b/util.c
index f6e62e9d32c666dffb36a93a12cd12a2c6a151bd..0cc43feb9ddae9875fe15686cc7070ab5ae9edce 100644 (file)
--- a/util.c
+++ b/util.c
@@ -353,10 +353,10 @@ printnum_int(struct tcb *tcp, long addr, const char *fmt)
 void
 printfd(struct tcb *tcp, int fd)
 {
-       const char *p;
+       char path[PATH_MAX + 1];
 
-       if (show_fd_path && (p = getfdpath(tcp, fd)))
-               tprintf("%d<%s>", fd, p);
+       if (show_fd_path && getfdpath(tcp, fd, path, sizeof(path)) >= 0)
+               tprintf("%d<%s>", fd, path);
        else
                tprintf("%d", fd);
 }