diff --git a/src/random.c b/src/random.c index dfd07ac..ea1c5ae 100644 --- a/src/random.c +++ b/src/random.c @@ -18,7 +18,8 @@ /*** Generate random bytes. -This uses `getrandom()` on Linux, `CryptGenRandom()` on Windows, and `/dev/urandom` on macOS. +This uses `getrandom()` on Linux, `CryptGenRandom()` on Windows, and `/dev/urandom` on macOS. The number of +bytes returned can be less than `length` if the system is unable to generate that many bytes or was interrupted. @function random @tparam[opt=1] int length number of bytes to get, must be less than or equal to `MAX_RANDOM_BUFFER_SIZE` (1024) @treturn[1] string string of random bytes @@ -53,6 +54,7 @@ static int lua_get_random_bytes(lua_State* L) { CryptReleaseContext(hCryptProv, 0); return 2; } + n = num_bytes; // CryptGenRandom() returns a BOOL, not the number of bytes CryptReleaseContext(hCryptProv, 0); #else @@ -85,7 +87,7 @@ static int lua_get_random_bytes(lua_State* L) { #endif - lua_pushlstring(L, (const char*)buffer, num_bytes); + lua_pushlstring(L, (const char*)buffer, n); return 1; }