diff options
author | Joey Gouly <joey.gouly@gmail.com> | 2024-04-13 04:25:55 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-13 11:25:55 +0800 |
commit | f064e72b9b9cc89059638f00876353d5a1b30c21 (patch) | |
tree | db8e03c0b541d7c77b1962c391ab604f3d3be7df | |
parent | 780509aedf5c914d1615d57019485ef85c16f669 (diff) | |
download | rneovim-f064e72b9b9cc89059638f00876353d5a1b30c21.tar.gz rneovim-f064e72b9b9cc89059638f00876353d5a1b30c21.tar.bz2 rneovim-f064e72b9b9cc89059638f00876353d5a1b30c21.zip |
fix(path): check return value of append_path() (#28309)
If the filename passed to vim_FullName() is a relative directory, and
does not exist, it is appended to the current working directory. Since
the return value of append_path() was ignored, and if the buffer length
was too small to fit getcwd() + dirname(filename), it would still try to
append the basename(filename).
This was manifesting as a failure in test/unit/path_spec.lua in:
itp('fails and uses filename if given filename contains non-existing directory', ..
This failure occurs when running the tests from directory with a short
path such as: /work/src/nv
test/unit/path_spec.lua:420: Expected objects to be the same.
Passed in:
(string) '/work/src/nv/test.file'
Expected:
(string) 'non_existing_dir/test.file'
This return value for the second call to append_path() to append
basename(filename) was checked, and this is where it would fail for
normal / longer getcwd()s.
-rw-r--r-- | src/nvim/os/fs.c | 2 | ||||
-rw-r--r-- | src/nvim/path.c | 4 | ||||
-rw-r--r-- | test/unit/path_spec.lua | 15 |
3 files changed, 13 insertions, 8 deletions
diff --git a/src/nvim/os/fs.c b/src/nvim/os/fs.c index 85caf4aa43..aa344d786c 100644 --- a/src/nvim/os/fs.c +++ b/src/nvim/os/fs.c @@ -375,7 +375,7 @@ static bool is_executable_in_path(const char *name, char **abspath) // Combine the $PATH segment with `name`. xstrlcpy(buf, p, (size_t)(e - p) + 1); - append_path(buf, name, buf_len); + (void)append_path(buf, name, buf_len); #ifdef MSWIN if (is_executable_ext(buf, abspath)) { diff --git a/src/nvim/path.c b/src/nvim/path.c index 4de18c7530..e3947c54cd 100644 --- a/src/nvim/path.c +++ b/src/nvim/path.c @@ -2301,7 +2301,9 @@ int path_full_dir_name(char *directory, char *buffer, size_t len) retval = FAIL; } else { xstrlcpy(buffer, old_dir, len); - append_path(buffer, directory, len); + if (append_path(buffer, directory, len) == FAIL) { + retval = FAIL; + } } } else if (os_dirname(buffer, len) == FAIL) { // Do not return immediately since we are in the wrong directory. diff --git a/test/unit/path_spec.lua b/test/unit/path_spec.lua index 8df6a52cd1..44919368bf 100644 --- a/test/unit/path_spec.lua +++ b/test/unit/path_spec.lua @@ -413,12 +413,15 @@ describe('path.c', function() end) itp('fails and uses filename if given filename contains non-existing directory', function() - local filename = 'non_existing_dir/test.file' - local buflen = string.len(filename) + 1 - local do_expand = 1 - local buf, result = vim_FullName(filename, buflen, do_expand) - eq(filename, ffi.string(buf)) - eq(FAIL, result) + -- test with different filename lengths + for rep = 1, 10 do + local filename = ('non_existing_'):rep(rep) .. 'dir/test.file' + local buflen = string.len(filename) + 1 + local do_expand = 1 + local buf, result = vim_FullName(filename, buflen, do_expand) + eq(filename, ffi.string(buf)) + eq(FAIL, result) + end end) itp('concatenates filename if it does not contain a slash', function() |