Fix random.random erroring with a negative length

Calling random.random(-1) will return an empty string and set the state
to a 31-byte string. This makes any further call in the module error.
This commit is contained in:
Miguel Oliveira 2023-06-09 21:22:25 -03:00
parent 0a23090e99
commit 5c615a14d3

View file

@ -39,7 +39,7 @@ end
local function random(len) local function random(len)
expect(1, len, "number") expect(1, len, "number")
lassert(initialized, "attempt to use an uninitialized random generator", 2) lassert(initialized, "attempt to use an uninitialized random generator", 2)
local msg = ("\0"):rep(len + 32) local msg = ("\0"):rep(math.min(len, 0) + 32)
local nonce = ("\0"):rep(12) local nonce = ("\0"):rep(12)
local out = chacha20.crypt(state, nonce, msg, 8, 0) local out = chacha20.crypt(state, nonce, msg, 8, 0)
state = out:sub(1, 32) state = out:sub(1, 32)