]> Sergey Matveev's repositories - nnn.git/commitdiff
handle tilde more strictly in mkpath and abspath
authorNRK <nrk@disroot.org>
Sun, 26 Feb 2023 12:11:43 +0000 (18:11 +0600)
committerNRK <nrk@disroot.org>
Sun, 26 Feb 2023 12:13:26 +0000 (18:13 +0600)
otherwise, if a file is named "~" it will get incorrectly expanded into
$HOME and disaster can happen.

src/nnn.c

index 5284e9ef60ea06f25ce10c17448c9cf887ebbc35..75ab5a06676ab0e7f1a695f917d6899bc361a490 100644 (file)
--- a/src/nnn.c
+++ b/src/nnn.c
@@ -1132,6 +1132,11 @@ static inline bool tilde_is_home(const char *s)
        return s[0] == '~' && (s[1] == '\0' || s[1] == '/');
 }
 
+static inline bool tilde_is_home_strict(const char *s)
+{
+       return s[0] == '~' && s[1] == '/';
+}
+
 /*
  * Updates out with "dir/name or "/name"
  * Returns the number of bytes copied including the terminating NULL byte
@@ -1142,10 +1147,8 @@ static size_t mkpath(const char *dir, const char *name, char *out)
 {
        size_t len = 0;
 
-       if (tilde_is_home(name)) { //NOLINT
+       if (tilde_is_home_strict(name)) { //NOLINT
                len = xstrsncpy(out, home, PATH_MAX);
-               if (!name[1])
-                       return len;
                --len;
                ++name;
        } else if (name[0] != '/') { // NOLINT
@@ -1212,11 +1215,9 @@ static char *abspath(const char *filepath, char *cwd, char *buf)
        if (!path)
                return NULL;
 
-       if (tilde_is_home(path)) {
+       if (tilde_is_home_strict(path)) {
                cwd = home;
-               ++path;
-               if (*path == '/')
-                       ++path;
+               path += 2; /* advance 2 bytes past the "~/" */
        } else if ((path[0] != '/') && !cwd) {
                cwd = getcwd(NULL, 0);
                if (!cwd)