commit 28e2a5c1513dd3adb6fc085a92f66bf7d7786317
parent 1ee18b40611487e50abbec425d74d3af7de7897d
Author: Evgeni Chasnovski <evgeni.chasnovski@gmail.com>
Date: Tue, 29 Jul 2025 16:06:01 +0300
fix(pack): improve `vim.pack.add()` input validation
Diffstat:
1 file changed, 9 insertions(+), 4 deletions(-)
diff --git a/runtime/lua/vim/pack.lua b/runtime/lua/vim/pack.lua
@@ -209,7 +209,11 @@ end
--- @param x string|vim.VersionRange
--- @return boolean
local function is_version(x)
- return type(x) == 'string' or (pcall(x.has, x, '1'))
+ return type(x) == 'string' or (type(x) == 'table' and pcall(x.has, x, '1'))
+end
+
+local function is_nonempty_string(x)
+ return type(x) == 'string' and x ~= ''
end
--- @return string
@@ -239,9 +243,10 @@ end
local function normalize_spec(spec)
spec = type(spec) == 'string' and { src = spec } or spec
vim.validate('spec', spec, 'table')
- vim.validate('spec.src', spec.src, 'string')
- local name = (spec.name or spec.src:gsub('%.git$', '')):match('[^/]+$')
- vim.validate('spec.name', name, 'string')
+ vim.validate('spec.src', spec.src, is_nonempty_string, false, 'non-empty string')
+ local name = spec.name or spec.src:gsub('%.git$', '')
+ name = (type(name) == 'string' and name or ''):match('[^/]+$') or ''
+ vim.validate('spec.name', name, is_nonempty_string, true, 'non-empty string')
vim.validate('spec.version', spec.version, is_version, true, 'string or vim.VersionRange')
return { src = spec.src, name = name, version = spec.version }
end