]> granicus.if.org Git - procps-ng/commitdiff
Fix readlink's do-while-loop
authorTobias Stoeckmann <tobias@stoeckmann.org>
Sat, 11 Jul 2015 19:30:31 +0000 (21:30 +0200)
committerTobias Stoeckmann <tobias@stoeckmann.org>
Sat, 11 Jul 2015 19:30:31 +0000 (21:30 +0200)
The function pid_link tries to handle programs which contain very
long paths to their executables. If 1024 bytes are not enough to
contain the path, the loop wants to get more and more space until
the path can fit.

The loop's condition does not fit though.

readlink will never return a value higher than its supplied size
limit, which is "path_alloc_size - 1", therefore the loop-check of
"len == path_alloc_size" will always be false: the loop will never
be repeated.

While at it, the if-condition inside the loop's body can be omitted,
because it is always true.

pidof.c

diff --git a/pidof.c b/pidof.c
index d1a9f315292dd12cfdaff1f082f39550731c42b1..8712d113621522e744b0379cc46af615c19a20a9 100644 (file)
--- a/pidof.c
+++ b/pidof.c
@@ -103,20 +103,18 @@ static char *pid_link (pid_t pid, const char *base_name)
 {
        char link [PROCPATHLEN];
        char *result;
-       int path_alloc_size;
-       int len;
+       ssize_t path_alloc_size;
+       ssize_t len;
 
        snprintf(link, sizeof(link), "/proc/%d/%s", pid, base_name);
 
        len = path_alloc_size = 0;
        result = NULL;
        do {
-               if (len == path_alloc_size) {
-                       grow_size (path_alloc_size);
-                       result = (char *) xrealloc (result, path_alloc_size);
-               }
+               grow_size(path_alloc_size);
+               result = xrealloc(result, path_alloc_size);
 
-               if ((len = readlink(link, result, path_alloc_size - 1)) < 0) {
+               if ((len = readlink(link, result, path_alloc_size)) < 0) {
                        len = 0;
                        break;
                }