diff --git a/std/native/src/main/scala/cats/effect/std/SecureRandomCompanionPlatform.scala b/std/native/src/main/scala/cats/effect/std/SecureRandomCompanionPlatform.scala index c0a7523479..679b1c4b26 100644 --- a/std/native/src/main/scala/cats/effect/std/SecureRandomCompanionPlatform.scala +++ b/std/native/src/main/scala/cats/effect/std/SecureRandomCompanionPlatform.scala @@ -18,7 +18,8 @@ package cats.effect.std import org.typelevel.scalaccompat.annotation._ -import scala.scalanative.libc.errno +import scala.scalanative.libc.errno._ +import scala.scalanative.libc.string._ import scala.scalanative.unsafe._ import scala.scalanative.unsigned._ @@ -30,19 +31,12 @@ private[std] trait SecureRandomCompanionPlatform { override def nextBytes(bytes: Array[Byte]): Unit = { val len = bytes.length - val buffer = stackalloc[Byte](256) var i = 0 while (i < len) { val n = Math.min(256, len - i) - if (sysrandom.getentropy(buffer, n.toULong) < 0) - throw new RuntimeException(s"getentropy: ${errno.errno}") - - var j = 0L - while (j < n) { - bytes(i) = buffer(j) - i += 1 - j += 1 - } + if (sysrandom.getentropy(bytes.at(i), n.toULong) < 0) + throw new RuntimeException(fromCString(strerror(errno))) + i += n } }