Skip to content

Commit

Permalink
fix(argon2) avoid getting config upvalue in largon2_verify and better
Browse files Browse the repository at this point in the history
"too many arguments" error messages
  • Loading branch information
thibaultcha committed Nov 29, 2016
1 parent 94fa58f commit 398777f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
14 changes: 9 additions & 5 deletions spec/argon2_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe("encrypt()", function()

assert.has_error(function()
argon2.encrypt("", "", {}, "")
end, "bad argument #4 to 'encrypt' (found too many arguments)")
end, "expecting no more than 3 arguments, but got 4")
end)
it("salt too short", function()
local hash, err = argon2.encrypt("password", "")
Expand Down Expand Up @@ -92,6 +92,10 @@ describe("verify()", function()
it("should throw error on invalid argument", function()
assert.has_error(function()
argon2.verify(nil)
end, "expecting 2 arguments, but got 1")

assert.has_error(function()
argon2.verify(nil, nil)
end, "bad argument #1 to 'verify' (string expected, got nil)")

assert.has_error(function()
Expand All @@ -100,7 +104,7 @@ describe("verify()", function()

assert.has_error(function()
argon2.verify("", "", "")
end, "bad argument #3 to 'verify' (found too many arguments)")
end, "expecting 2 arguments, but got 3")
end)
it("should verify ok", function()
local hash = assert(argon2.encrypt("password", "somesalt"))
Expand Down Expand Up @@ -128,15 +132,15 @@ describe("module settings", function()
it("should throw error on invalid argument", function()
assert.has_error(function()
argon2.t_cost(0, 0)
end, "bad argument #2 to 't_cost' (found too many arguments)")
end, "expecting no more than 1 arguments, but got 2")

assert.has_error(function()
argon2.t_cost ""
end, "bad argument #1 to 't_cost' (expected t_cost to be a number, got string)")

assert.has_error(function()
argon2.argon2d(0, 0)
end, "bad argument #2 to 'argon2d' (found too many arguments)")
argon2.argon2d(0)
end, "bad argument #1 to 'argon2d' (invalid option '0')")

assert.has_error(function()
argon2.argon2d "idk"
Expand Down
15 changes: 10 additions & 5 deletions src/argon2.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,13 @@ largon2_fetch_config(lua_State *L)
static largon2_config_t*
largon2_arg_init(lua_State *L, int nargs)
{
luaL_argcheck(L, lua_gettop(L) <= nargs, nargs + 1,
"found too many arguments");
if (lua_gettop(L) > nargs) {
luaL_error(L, "expecting no more than %d arguments, but got %d",
nargs, lua_gettop(L));
return NULL;
}

while (lua_gettop(L) < nargs)
lua_pushnil(L);
lua_settop(L, nargs);

return largon2_fetch_config(L);
}
Expand Down Expand Up @@ -305,7 +307,10 @@ largon2_verify(lua_State *L)
size_t plainlen;
argon2_type type;

largon2_arg_init(L, 2);
if (lua_gettop(L) != 2) {
return luaL_error(L, "expecting 2 arguments, but got %d",
lua_gettop(L));
}

encoded = luaL_checkstring(L, 1);
plain = luaL_checklstring(L, 2, &plainlen);
Expand Down

0 comments on commit 398777f

Please sign in to comment.