From 33d80748468ebf850acd6b392291964ffb949e16 Mon Sep 17 00:00:00 2001 From: Nick Treleaven Date: Wed, 17 Feb 2021 19:47:19 +0000 Subject: [PATCH] crypto.rand: use byteptr.vbytes instead of c_array_to_bytes_tmp (#8786) --- vlib/crypto/rand/rand.v | 17 ----------------- vlib/crypto/rand/rand_darwin.c.v | 6 +++--- vlib/crypto/rand/rand_linux.c.v | 13 +++---------- vlib/crypto/rand/rand_solaris.c.v | 15 ++++----------- vlib/crypto/rand/rand_windows.c.v | 6 +++--- 5 files changed, 13 insertions(+), 44 deletions(-) diff --git a/vlib/crypto/rand/rand.v b/vlib/crypto/rand/rand.v index 17f5c10f91..9ad4602fc0 100644 --- a/vlib/crypto/rand/rand.v +++ b/vlib/crypto/rand/rand.v @@ -7,20 +7,3 @@ module rand const ( read_error = error('crypto.rand.read() error reading random bytes') ) - -// NOTE: temp until we have []bytes(buff) -fn c_array_to_bytes_tmp(len int, buffer voidptr) []byte { - - mut arr := []byte{len:len, cap:1} - arr.data = buffer - /* - - arr = array { - len: len - cap: 1 - element_size: 1 - data: buffer - } - */ - return arr -} diff --git a/vlib/crypto/rand/rand_darwin.c.v b/vlib/crypto/rand/rand_darwin.c.v index a85468e492..e42e762869 100644 --- a/vlib/crypto/rand/rand_darwin.c.v +++ b/vlib/crypto/rand/rand_darwin.c.v @@ -12,10 +12,10 @@ fn C.SecRandomCopyBytes() int // read returns an array of `bytes_needed` random bytes read from the OS. pub fn read(bytes_needed int) ?[]byte { - mut buffer := malloc(bytes_needed) - status := C.SecRandomCopyBytes(0, bytes_needed, buffer) + mut buffer := []byte{ len: bytes_needed } + status := C.SecRandomCopyBytes(0, bytes_needed, buffer.data) if status != 0 { return read_error } - return c_array_to_bytes_tmp(bytes_needed, buffer) + return buffer } diff --git a/vlib/crypto/rand/rand_linux.c.v b/vlib/crypto/rand/rand_linux.c.v index fd7dbe1cef..53ab02c5ac 100644 --- a/vlib/crypto/rand/rand_linux.c.v +++ b/vlib/crypto/rand/rand_linux.c.v @@ -10,27 +10,20 @@ const ( // read returns an array of `bytes_needed` random bytes read from the OS. pub fn read(bytes_needed int) ?[]byte { - mut buffer := &byte(0) - unsafe { - buffer = malloc(bytes_needed) - } - mut bstart := buffer + mut buffer := unsafe { malloc(bytes_needed) } mut bytes_read := 0 mut remaining_bytes := bytes_needed // getrandom syscall wont block if requesting <= 256 bytes 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) + rbytes := unsafe { getrandom(batch_size, buffer + bytes_read) } if rbytes == -1 { unsafe { free(buffer) } return read_error } bytes_read += rbytes } - return c_array_to_bytes_tmp(bytes_needed, buffer) + return unsafe {buffer.vbytes(bytes_needed)} } fn getrandom(bytes_needed int, buffer voidptr) int { diff --git a/vlib/crypto/rand/rand_solaris.c.v b/vlib/crypto/rand/rand_solaris.c.v index 9fc91be715..09f1e43061 100644 --- a/vlib/crypto/rand/rand_solaris.c.v +++ b/vlib/crypto/rand/rand_solaris.c.v @@ -14,27 +14,20 @@ const ( // read returns an array of `bytes_needed` random bytes read from the OS. pub fn read(bytes_needed int) ?[]byte { - mut buffer := &byte(0) - unsafe { - buffer = malloc(bytes_needed) - } - mut bstart := buffer + mut buffer := unsafe { malloc(bytes_needed) } mut bytes_read := 0 mut remaining_bytes := bytes_needed // getrandom syscall wont block if requesting <= 256 bytes 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) + rbytes := unsafe { getrandom(batch_size, buffer + bytes_read) } if rbytes == -1 { - free(buffer) + unsafe { free(buffer) } return read_error } bytes_read += rbytes } - return c_array_to_bytes_tmp(bytes_needed, buffer) + return unsafe {buffer.vbytes(bytes_needed)} } fn v_getrandom(bytes_needed int, buffer voidptr) int { diff --git a/vlib/crypto/rand/rand_windows.c.v b/vlib/crypto/rand/rand_windows.c.v index 8059aef548..24ad4b9409 100644 --- a/vlib/crypto/rand/rand_windows.c.v +++ b/vlib/crypto/rand/rand_windows.c.v @@ -15,11 +15,11 @@ const ( // read returns an array of `bytes_needed` random bytes read from the OS. pub fn read(bytes_needed int) ?[]byte { - mut buffer := unsafe {malloc(bytes_needed)} + mut buffer := []byte{ len: bytes_needed } // use bcrypt_use_system_preferred_rng because we passed null as algo - status := C.BCryptGenRandom(0, buffer, bytes_needed, bcrypt_use_system_preferred_rng) + status := C.BCryptGenRandom(0, buffer.data, bytes_needed, bcrypt_use_system_preferred_rng) if status != status_success { return read_error } - return c_array_to_bytes_tmp(bytes_needed, buffer) + return buffer }