diff --git a/src/crystal/system/unix/getrandom.cr b/src/crystal/system/unix/getrandom.cr index 229716a3d846..e759ff0406e6 100644 --- a/src/crystal/system/unix/getrandom.cr +++ b/src/crystal/system/unix/getrandom.cr @@ -13,7 +13,10 @@ require "./syscall" # TODO: Implement syscall for interpreter def self.getrandom(buf : UInt8*, buflen : LibC::SizeT, flags : UInt32) : LibC::SSizeT - LibC.getrandom(buf, buflen, flags) + # the syscall returns the negative of errno directly, the C function + # doesn't, so we mimic the syscall behavior + read_bytes = LibC.getrandom(buf, buflen, flags) + read_bytes >= 0 ? read_bytes : LibC::SSizeT.new(-Errno.value.value) end end {% end %}