OSDN Git Service

getcwd(2): saner logics around prepend_path() call
authorAl Viro <viro@zeniv.linux.org.uk>
Tue, 18 May 2021 01:56:38 +0000 (21:56 -0400)
committerAl Viro <viro@zeniv.linux.org.uk>
Wed, 19 May 2021 00:08:11 +0000 (20:08 -0400)
The only negative value that might get returned by prepend_path() is
-ENAMETOOLONG, and that happens only on overflow.  The same goes for
prepend_unreachable().  Overflow is detectable by observing negative
buflen, so we can simplify the control flow around the prepend_path()
call.  Expand prepend_unreachable(), while we are at it - that's the
only caller.

Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
fs/d_path.c

index 7f3fac5..311d432 100644 (file)
@@ -211,11 +211,6 @@ char *d_absolute_path(const struct path *path,
        return res;
 }
 
-static int prepend_unreachable(char **buffer, int *buflen)
-{
-       return prepend(buffer, buflen, "(unreachable)", 13);
-}
-
 static void get_fs_root_rcu(struct fs_struct *fs, struct path *root)
 {
        unsigned seq;
@@ -414,17 +409,13 @@ SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
                int buflen = PATH_MAX;
 
                prepend(&cwd, &buflen, "", 1);
-               error = prepend_path(&pwd, &root, &cwd, &buflen);
+               if (prepend_path(&pwd, &root, &cwd, &buflen) > 0)
+                       prepend(&cwd, &buflen, "(unreachable)", 13);
                rcu_read_unlock();
 
-               if (error < 0)
+               if (buflen < 0) {
+                       error = -ENAMETOOLONG;
                        goto out;
-
-               /* Unreachable from current root */
-               if (error > 0) {
-                       error = prepend_unreachable(&cwd, &buflen);
-                       if (error)
-                               goto out;
                }
 
                error = -ERANGE;