From ac91a79b121297454e9b1d4496cf1fc44de1a1ba Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sun, 18 Mar 2018 23:23:00 +1100 Subject: [PATCH] Fix implementation of real_path to handle non-existing directories in / Fixes #1937 --- src/file.cc | 10 +++++----- .../1937-opening-missing-file-by-full-path-fails/cmd | 1 + .../1937-opening-missing-file-by-full-path-fails/in | 1 + .../1937-opening-missing-file-by-full-path-fails/out | 1 + .../1937-opening-missing-file-by-full-path-fails/rc | 4 ++++ 5 files changed, 12 insertions(+), 5 deletions(-) create mode 100644 test/regression/1937-opening-missing-file-by-full-path-fails/cmd create mode 100644 test/regression/1937-opening-missing-file-by-full-path-fails/in create mode 100644 test/regression/1937-opening-missing-file-by-full-path-fails/out create mode 100644 test/regression/1937-opening-missing-file-by-full-path-fails/rc diff --git a/src/file.cc b/src/file.cc index 3dee5db57..f4e92e19f 100644 --- a/src/file.cc +++ b/src/file.cc @@ -67,6 +67,7 @@ std::pair split_path(StringView path) String real_path(StringView filename) { + kak_assert(not filename.empty()); char buffer[PATH_MAX+1]; StringView existing = filename; @@ -74,22 +75,21 @@ String real_path(StringView filename) while (true) { - char* res = realpath(existing.zstr(), buffer); - if (res) + if (char* res = realpath(existing.zstr(), buffer)) { if (non_existing.empty()) return res; - return format("{}/{}", res, non_existing); + return format("{}{}", res, non_existing); } - auto it = find(existing.rbegin(), existing.rend(), '/'); + auto it = find(existing.rbegin() + 1, existing.rend(), '/'); if (it == existing.rend()) { char cwd[1024]; return format("{}/{}", getcwd(cwd, 1024), filename); } - existing = StringView{existing.begin(), it.base()-1}; + existing = StringView{existing.begin(), it.base()}; non_existing = StringView{it.base(), filename.end()}; } } diff --git a/test/regression/1937-opening-missing-file-by-full-path-fails/cmd b/test/regression/1937-opening-missing-file-by-full-path-fails/cmd new file mode 100644 index 000000000..c22f6feea --- /dev/null +++ b/test/regression/1937-opening-missing-file-by-full-path-fails/cmd @@ -0,0 +1 @@ +"aP diff --git a/test/regression/1937-opening-missing-file-by-full-path-fails/in b/test/regression/1937-opening-missing-file-by-full-path-fails/in new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/test/regression/1937-opening-missing-file-by-full-path-fails/in @@ -0,0 +1 @@ + diff --git a/test/regression/1937-opening-missing-file-by-full-path-fails/out b/test/regression/1937-opening-missing-file-by-full-path-fails/out new file mode 100644 index 000000000..321fce9c6 --- /dev/null +++ b/test/regression/1937-opening-missing-file-by-full-path-fails/out @@ -0,0 +1 @@ +/kakoune-inexisting-directory/missing-file diff --git a/test/regression/1937-opening-missing-file-by-full-path-fails/rc b/test/regression/1937-opening-missing-file-by-full-path-fails/rc new file mode 100644 index 000000000..05a21ce5f --- /dev/null +++ b/test/regression/1937-opening-missing-file-by-full-path-fails/rc @@ -0,0 +1,4 @@ +eval -draft %{ + edit /kakoune-inexisting-directory/missing-file + reg a %val{buffile} +}