nvim-lua / plenary.nvim

plenary: full; complete; entire; absolute; unqualified. All the lua functions I don't want to write twice.
MIT License
2.82k stars 287 forks source link

Path:shorten() segfaults neovim #320

Closed shadmansaleh closed 2 years ago

shadmansaleh commented 2 years ago

function signature of shorten_dir changed in upstream commit https://github.com/neovim/neovim/commit/d65ee129143fedd43178c9be52095b5d2d06b5c2

So now it returns void and plenary is dereferencing that as a pointer and causing segfault . See https://github.com/neovim/neovim/issues/17361

This patch seems to fix the issue

diff --git a/lua/plenary/path.lua b/lua/plenary/path.lua
index a8152f1..dd9cb9d 100644
--- a/lua/plenary/path.lua
+++ b/lua/plenary/path.lua
@@ -397,7 +397,7 @@ local shorten = (function()
     local ffi = require "ffi"
     ffi.cdef [[
     typedef unsigned char char_u;
-    char_u *shorten_dir(char_u *str);
+    void shorten_dir(char_u *str);
     ]]
     return function(filename)
       if not filename or is_uri(filename) then
@@ -406,7 +406,8 @@ local shorten = (function()

       local c_str = ffi.new("char[?]", #filename + 1)
       ffi.copy(c_str, filename)
-      return ffi.string(ffi.C.shorten_dir(c_str))
+      ffi.C.shorten_dir(c_str)
+      return ffi.string(c_str)
     end
   end
   return function(filename)

Actually why not just use pathshorten() exposed through viml ? That's stable and not prone it changes like this .

zeertzjq commented 2 years ago

Using void seems to still work fine in v0.6.1.

Conni2461 commented 2 years ago

we cant use vim.fn.pathshorten because we need a api fast method. And schedule is not feasible because we cant schedule every time we want to display an shorten path in telescope.

Thanks for debugging.