diff options
author | Kim Alvefur <zash@zash.se> | 2022-01-15 16:25:25 +0100 |
---|---|---|
committer | Kim Alvefur <zash@zash.se> | 2022-01-15 16:25:25 +0100 |
commit | cced954ac3e55bff2dabb86c555044eb007e44d4 (patch) | |
tree | 25ff592845b6021179d182bbbc8db134a08426f6 | |
parent | 91055b49bba7b0be8f2b25b96b0ae9ce2a4337ff (diff) | |
download | prosody-cced954ac3e55bff2dabb86c555044eb007e44d4.tar.gz prosody-cced954ac3e55bff2dabb86c555044eb007e44d4.zip |
util.jid: Explicitly check for nil rather than falsy
A boolean false should blow up.
-rw-r--r-- | spec/util_jid_spec.lua | 11 | ||||
-rw-r--r-- | util/jid.lua | 28 |
2 files changed, 25 insertions, 14 deletions
diff --git a/spec/util_jid_spec.lua b/spec/util_jid_spec.lua index 17cadbee..b92ca06c 100644 --- a/spec/util_jid_spec.lua +++ b/spec/util_jid_spec.lua @@ -13,6 +13,11 @@ describe("util.jid", function() assert.are.equal(jid.join(nil, nil, "c"), nil, "invalid JID is nil"); assert.are.equal(jid.join("a", nil, "c"), nil, "invalid JID is nil"); end); + it("should reject invalid arguments", function () + assert.has_error(function () jid.join(false, "bork", nil) end) + assert.has_error(function () jid.join(nil, "bork", false) end) + assert.has_error(function () jid.join(false, false, false) end) + end) end); describe("#split()", function() it("should work", function() @@ -38,6 +43,9 @@ describe("util.jid", function() test("@server/resource", nil, nil, nil); test("@/resource", nil, nil, nil); end); + it("should reject invalid arguments", function () + assert.has_error(function () jid.split(false) end) + end) end); @@ -59,6 +67,9 @@ describe("util.jid", function() assert.are.equal(jid.bare("user@@host/resource"), nil, "invalid JID is nil"); assert.are.equal(jid.bare("user@host/"), nil, "invalid JID is nil"); end); + it("should reject invalid arguments", function () + assert.has_error(function () jid.bare(false) end) + end) end); describe("#compare()", function() diff --git a/util/jid.lua b/util/jid.lua index a1180534..694a6b1f 100644 --- a/util/jid.lua +++ b/util/jid.lua @@ -32,18 +32,18 @@ local _ENV = nil; -- luacheck: std none local function split(jid) - if not jid then return; end + if jid == nil then return; end local node, nodepos = match(jid, "^([^@/]+)@()"); local host, hostpos = match(jid, "^([^@/]+)()", nodepos); - if node and not host then return nil, nil, nil; end + if node ~= nil and host == nil then return nil, nil, nil; end local resource = match(jid, "^/(.+)$", hostpos); - if (not host) or ((not resource) and #jid >= hostpos) then return nil, nil, nil; end + if (host == nil) or ((resource == nil) and #jid >= hostpos) then return nil, nil, nil; end return node, host, resource; end local function bare(jid) local node, host = split(jid); - if node and host then + if node ~= nil and host ~= nil then return node.."@"..host; end return host; @@ -51,31 +51,31 @@ end local function prepped_split(jid, strict) local node, host, resource = split(jid); - if host and host ~= "." then + if host ~= nil and host ~= "." then if sub(host, -1, -1) == "." then -- Strip empty root label host = sub(host, 1, -2); end host = nameprep(host, strict); - if not host then return; end - if node then + if host == nil then return; end + if node ~= nil then node = nodeprep(node, strict); - if not node then return; end + if node == nil then return; end end - if resource then + if resource ~= nil then resource = resourceprep(resource, strict); - if not resource then return; end + if resource == nil then return; end end return node, host, resource; end end local function join(node, host, resource) - if not host then return end - if node and resource then + if host == nil then return end + if node ~= nil and resource ~= nil then return node.."@"..host.."/"..resource; - elseif node then + elseif node ~= nil then return node.."@"..host; - elseif resource then + elseif resource ~= nil then return host.."/"..resource; end return host; |