From 770ffa1ebd03cd619aeef7c3a1e76b3c0a34782f Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Sun, 5 Jul 2020 01:33:36 +0300 Subject: [PATCH] crypto.rand: fix linux/solaris rand.read() compilation --- vlib/crypto/rand/rand_linux.c.v | 34 +++++++++++++++---------------- vlib/crypto/rand/rand_solaris.c.v | 29 +++++++++++++------------- 2 files changed, 31 insertions(+), 32 deletions(-) diff --git a/vlib/crypto/rand/rand_linux.c.v b/vlib/crypto/rand/rand_linux.c.v index b03ed090b8..8c8ddb6378 100644 --- a/vlib/crypto/rand/rand_linux.c.v +++ b/vlib/crypto/rand/rand_linux.c.v @@ -1,34 +1,34 @@ // Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved. // Use of this source code is governed by an MIT license // that can be found in the LICENSE file. - module rand -import math - #include - const ( read_batch_size = 256 ) -pub fn read(bytes_needed int) ?[]byte { - buffer := malloc(bytes_needed) +pub fn read(bytes_needed int) ?[]byte { + mut buffer := &byte(0) + unsafe { + buffer = malloc(bytes_needed) + } + mut bstart := buffer mut bytes_read := 0 + mut remaining_bytes := bytes_needed // getrandom syscall wont block if requesting <= 256 bytes - if bytes_needed > read_batch_size { - no_batches := int(math.floor(f64(bytes_needed/read_batch_size))) - for i:=0; i < no_batches; i++ { - if getrandom(read_batch_size, buffer+bytes_read) == -1 { - return read_error - } - bytes_read += read_batch_size + for bytes_read < bytes_needed { + batch_size := if remaining_bytes > read_batch_size { read_batch_size } else { remaining_bytes } + unsafe { + bstart = buffer + bytes_read } + rbytes := getrandom(batch_size, bstart) + if rbytes == -1 { + free(buffer) + return read_error + } + bytes_read += rbytes } - if getrandom(bytes_needed-bytes_read, buffer+bytes_read) == -1 { - return read_error - } - return c_array_to_bytes_tmp(bytes_needed, buffer) } diff --git a/vlib/crypto/rand/rand_solaris.c.v b/vlib/crypto/rand/rand_solaris.c.v index 3ac6d2a434..52ceb17375 100644 --- a/vlib/crypto/rand/rand_solaris.c.v +++ b/vlib/crypto/rand/rand_solaris.c.v @@ -4,8 +4,6 @@ module rand -import math - #include fn C.getrandom(p byteptr, n size_t, flags u32) int @@ -14,26 +12,27 @@ const ( read_batch_size = 256 ) -pub fn read(bytes_needed int) ?[]byte { +pub fn read(bytes_needed int) ?[]byte { mut buffer := &byte(0) unsafe { - buffer = malloc(bytes_needed) + buffer = malloc(bytes_needed) } + mut bstart := buffer mut bytes_read := 0 + mut remaining_bytes := bytes_needed // getrandom syscall wont block if requesting <= 256 bytes - if bytes_needed > read_batch_size { - no_batches := int(math.floor(f64(bytes_needed/read_batch_size))) - for i:=0; i read_batch_size { read_batch_size } else { remaining_bytes } + unsafe { + bstart = buffer + bytes_read } + rbytes := getrandom(batch_size, bstart) + if rbytes == -1 { + free(buffer) + return read_error + } + bytes_read += rbytes } - if v_getrandom(bytes_needed-bytes_read, buffer+bytes_read) == -1 { - return read_error - } - return c_array_to_bytes_tmp(bytes_needed, buffer) }