Skip to content

Commit

Permalink
fix(random): return number of bytes received
Browse files Browse the repository at this point in the history
  • Loading branch information
Tieske committed Nov 14, 2023
1 parent 1dd4e45 commit d73d7e6
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/random.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit d73d7e6

Please sign in to comment.