Skip to content

Commit

Permalink
Merge pull request #9 from savi-lang/fix/linux-getrandom
Browse files Browse the repository at this point in the history
Fix getrandom FFI signature and usage on Linux.
  • Loading branch information
jemc authored Jan 31, 2023
2 parents 178a92a + 82cd9fe commit f3b4c29
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 8 deletions.
1 change: 0 additions & 1 deletion spec/SecureRandom.Spec.savi
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,3 @@
assert: random.u32 <: U32
assert: random.u16 <: U16
assert: random.u8 <: U8

7 changes: 1 addition & 6 deletions src/SecureRandom.savi
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,7 @@
| Platform.is_windows |
_FFI.windows_getrandom(@_buffer.cpointer(i * @_chunk), @_chunk)
| Platform.is_linux |
// Linux `getrandom` can sometimes fail when there isn't enough entropy.
// We'll just spin here until it succeeds, to avoid insecure output.
result = -1
while (result == -1) (
result = _FFI.getrandom(@_buffer.cpointer(i * @_chunk), @_chunk)
)
_FFI.getrandom(@_buffer.cpointer(i * @_chunk), @_chunk, 0)
)
)

Expand Down
2 changes: 1 addition & 1 deletion src/_FFI.savi
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
:ffi getentropy(buffer CPointer(U8), size USize) I32

// Linux
:ffi getrandom(buffer CPointer(U8), size USize) I32
:ffi getrandom(buffer CPointer(U8), size USize, flags U32) ISize

// Windows
:ffi _windowsBCryptGenRandom(
Expand Down

0 comments on commit f3b4c29

Please sign in to comment.