From: Denys Vlasenko Date: Wed, 6 Mar 2013 17:24:34 +0000 (+0100) Subject: Stop using a large static buffer in getfdpath X-Git-Tag: v4.8~99 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=61ad0a401c6d1b7b4c1a209f9d61c3790e1e7d15;p=strace Stop using a large static buffer in getfdpath text data bss dec hex filename 245075 680 9836 255591 3e667 strace_old 245143 680 5708 251531 3d68b strace Signed-off-by: Denys Vlasenko --- diff --git a/defs.h b/defs.h index ca5d0883..c20fcc61 100644 --- 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); diff --git a/pathtrace.c b/pathtrace.c index 95643c67..d4c2dc43 100644 --- a/pathtrace.c +++ b/pathtrace.c @@ -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 f6e62e9d..0cc43feb 100644 --- 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); }