Skip to content

Commit afb1ff1

Browse files
committed
fix(dap): validate opts.default_port to the same 1-65535 contract as config.port
attach_endpoint validated config.port but let opts.default_port through raw, so an invalid default (0, 70000, a non-numeric string) could return an out-of-range/non-integer port despite the documented contract. Share one coerce_port check across both. The only in-repo caller passing a default (delve, 38697) is valid; verified bad defaults now error clearly and the config.port / required-port paths are unchanged.
1 parent aaea9fe commit afb1ff1

1 file changed

Lines changed: 26 additions & 5 deletions

File tree

lua/modules/utils/dap.lua

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,38 @@ function M.attach_endpoint(config, opts)
3434
-- (this helper's whole purpose): guard the two shapes it dereferences.
3535
assert(type(config) == "table", "attach_endpoint: config must be a table")
3636
assert(type(opts) == "table" and type(opts.label) == "string", "attach_endpoint: opts.label (string) is required")
37-
local port = opts.default_port
38-
if config.port ~= nil then
39-
local n = tonumber(config.port)
37+
-- config.port and opts.default_port share the 1-65535 integer contract, so
38+
-- validate them the same way — an invalid default (0, 70000, "abc") must not
39+
-- slip through to the return.
40+
local function coerce_port(v)
41+
local n = tonumber(v)
4042
if not n or n ~= math.floor(n) or n < 1 or n > 65535 then
43+
return nil
44+
end
45+
return n
46+
end
47+
local port
48+
if config.port ~= nil then
49+
port = coerce_port(config.port)
50+
if not port then
4151
error(
4252
string.format("%s: invalid `port` %s (want an integer 1-65535)", opts.label, vim.inspect(config.port)),
4353
0
4454
)
4555
end
46-
port = n
47-
elseif port == nil then
56+
elseif opts.default_port ~= nil then
57+
port = coerce_port(opts.default_port)
58+
if not port then
59+
error(
60+
string.format(
61+
"%s: invalid `default_port` %s (want an integer 1-65535)",
62+
opts.label,
63+
vim.inspect(opts.default_port)
64+
),
65+
0
66+
)
67+
end
68+
else
4869
error(string.format("%s: `port` is required", opts.label), 0)
4970
end
5071
local host = "127.0.0.1"

0 commit comments

Comments
 (0)