diff --git a/src/crystal/system/unix/getrandom.cr b/src/crystal/system/unix/getrandom.cr index 7be835ff5ab9..229716a3d846 100644 --- a/src/crystal/system/unix/getrandom.cr +++ b/src/crystal/system/unix/getrandom.cr @@ -62,9 +62,9 @@ module Crystal::System::Random init unless @@initialized if @@getrandom_available - buf = uninitialized UInt8[1] - getrandom(buf.to_slice) - buf.unsafe_as(UInt8) + buf = uninitialized UInt8 + getrandom(pointerof(buf).to_slice(1)) + buf elsif urandom = @@urandom urandom.read_byte.not_nil! else diff --git a/src/crystal/system/wasi/random.cr b/src/crystal/system/wasi/random.cr index 72649fac3e7c..3b50ff7fdf44 100644 --- a/src/crystal/system/wasi/random.cr +++ b/src/crystal/system/wasi/random.cr @@ -7,8 +7,8 @@ module Crystal::System::Random end def self.next_u : UInt8 - buf = uninitialized UInt8[1] - random_bytes(buf.to_slice) - buf.unsafe_as(UInt8) + buf = uninitialized UInt8 + random_bytes(pointerof(buf).to_slice(1)) + buf end end diff --git a/src/crystal/system/win32/random.cr b/src/crystal/system/win32/random.cr index 03c54fa8aa40..faec72ca9b57 100644 --- a/src/crystal/system/win32/random.cr +++ b/src/crystal/system/win32/random.cr @@ -8,8 +8,8 @@ module Crystal::System::Random end def self.next_u : UInt8 - buf = uninitialized UInt8[1] - random_bytes(buf.to_slice) - buf.unsafe_as(UInt8) + buf = uninitialized UInt8 + random_bytes(pointerof(buf).to_slice(1)) + buf end end