From 0af415fa28bf61dc0522a788a110dcc4bfbe8bfd Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Tue, 21 Jul 2020 09:05:31 +0300 Subject: [PATCH] vlib: add unsafe{} wrappers to C. fn calls, to allow compiling with -prod again --- vlib/json/json_primitives.v | 2 +- vlib/net/websocket/utils.v | 2 +- vlib/net/websocket/ws.v | 6 +++--- vlib/rand/sys/system_rng.c.v | 2 +- vlib/sync/sync_nix.c.v | 10 +++++----- vlib/v/tests/cstrings_test.v | 6 +++--- 6 files changed, 14 insertions(+), 14 deletions(-) diff --git a/vlib/json/json_primitives.v b/vlib/json/json_primitives.v index a0a7af47e7..b4377e7a8b 100644 --- a/vlib/json/json_primitives.v +++ b/vlib/json/json_primitives.v @@ -187,7 +187,7 @@ fn json_parse(s string) &C.cJSON { // json_string := json_print(encode_User(user)) fn json_print(json &C.cJSON) string { s := C.cJSON_PrintUnformatted(json) - return tos(s, C.strlen(s)) + return unsafe { tos(s, C.strlen(s)) } } // / cjson wrappers diff --git a/vlib/net/websocket/utils.v b/vlib/net/websocket/utils.v index d3c1c6117c..b201961a71 100644 --- a/vlib/net/websocket/utils.v +++ b/vlib/net/websocket/utils.v @@ -20,7 +20,7 @@ fn htonl64(payload_len u64) byteptr { fn create_masking_key() []byte { mask_bit := byte(rand.intn(255)) buf := [`0`].repeat(4) - C.memcpy(buf.data, &mask_bit, 4) + unsafe { C.memcpy(buf.data, &mask_bit, 4) } return buf } diff --git a/vlib/net/websocket/ws.v b/vlib/net/websocket/ws.v index bbdf53513e..f595156d11 100644 --- a/vlib/net/websocket/ws.v +++ b/vlib/net/websocket/ws.v @@ -430,7 +430,7 @@ pub fn (mut ws Client) read() int { if payload == 0 { l.f('out of memory') } - C.memcpy(payload, &data[header_len], payload_len) + unsafe { C.memcpy(payload, &data[header_len], payload_len) } if frame.fin { if ws.fragments.len > 0 { // join fragments @@ -522,7 +522,7 @@ pub fn (mut ws Client) read() int { mut payload := []byte{} if payload_len > 0 { payload = [`0`].repeat(int(payload_len)) - C.memcpy(payload.data, &data[header_len], payload_len) + unsafe { C.memcpy(payload.data, &data[header_len], payload_len) } } unsafe { free(data) @@ -601,7 +601,7 @@ fn (mut ws Client) send_control_frame(code OPCode, frame_typ string, payload []b if code == .close { if payload.len > 2 { mut parsed_payload := [`0`].repeat(payload.len + 1) - C.memcpy(parsed_payload.data, &payload[0], payload.len) + unsafe { C.memcpy(parsed_payload.data, &payload[0], payload.len) } parsed_payload[payload.len] = `\0` for i in 0 .. payload.len { control_frame[6 + i] = (parsed_payload[i] ^ masking_key[i % 4]) & 0xff diff --git a/vlib/rand/sys/system_rng.c.v b/vlib/rand/sys/system_rng.c.v index 5ef2343683..60786d23ca 100644 --- a/vlib/rand/sys/system_rng.c.v +++ b/vlib/rand/sys/system_rng.c.v @@ -46,7 +46,7 @@ pub fn (mut r SysRNG) seed(seed_data []u32) { exit(1) } r.seed = seed_data[0] - C.srand(int(r.seed)) + unsafe { C.srand(int(r.seed)) } } // r.default_rand() exposes the default behavior of the system's RNG diff --git a/vlib/sync/sync_nix.c.v b/vlib/sync/sync_nix.c.v index 0fb125c2d1..027e4dbf88 100644 --- a/vlib/sync/sync_nix.c.v +++ b/vlib/sync/sync_nix.c.v @@ -114,7 +114,7 @@ pub fn new_semaphore() Semaphore { s := Semaphore{ sem: &PosixSemaphore{} } - C.sem_init(&&PosixSemaphore(s.sem).sem, 0, 0) + unsafe { C.sem_init(&&PosixSemaphore(s.sem).sem, 0, 0) } return s } } @@ -126,7 +126,7 @@ pub fn (s Semaphore) post() { C.pthread_cond_signal(&&MacOSX_Semaphore(s.sem).cond) C.pthread_mutex_unlock(&&MacOSX_Semaphore(s.sem).mtx) } $else { - C.sem_post(&&PosixSemaphore(s.sem).sem) + unsafe { C.sem_post(&&PosixSemaphore(s.sem).sem) } } } @@ -139,7 +139,7 @@ pub fn (s Semaphore) wait() { (&MacOSX_Semaphore(s.sem)).count-- C.pthread_mutex_unlock(&&MacOSX_Semaphore(s.sem).mtx) } $else { - C.sem_wait(&&PosixSemaphore(s.sem).sem) + unsafe { C.sem_wait(&&PosixSemaphore(s.sem).sem) } } } @@ -161,7 +161,7 @@ pub fn (s Semaphore) try_wait() bool { C.pthread_mutex_unlock(&&MacOSX_Semaphore(s.sem).mtx) return res } $else { - return C.sem_trywait(&&PosixSemaphore(s.sem).sem) == 0 + return unsafe { C.sem_trywait(&&PosixSemaphore(s.sem).sem) == 0 } } } @@ -183,6 +183,6 @@ pub fn (s Semaphore) timed_wait(timeout time.Duration) bool { C.pthread_mutex_unlock(&&MacOSX_Semaphore(s.sem).mtx) return res } $else { - return C.sem_timedwait(&&PosixSemaphore(s.sem).sem, &t_spec) == 0 + return unsafe { C.sem_timedwait(&&PosixSemaphore(s.sem).sem, &t_spec) == 0 } } } diff --git a/vlib/v/tests/cstrings_test.v b/vlib/v/tests/cstrings_test.v index b41dfe055a..6ebe0c4dc5 100644 --- a/vlib/v/tests/cstrings_test.v +++ b/vlib/v/tests/cstrings_test.v @@ -1,8 +1,8 @@ fn test_cstring() { w := c'world' - hlen := C.strlen(c'hello') - hlen2 := C.strlen('hello') - wlen := C.strlen(w) + hlen := unsafe{ C.strlen(c'hello') } + hlen2 := unsafe{ C.strlen('hello') } + wlen := unsafe{ C.strlen(w) } assert hlen == 5 assert hlen2 == 5 assert wlen == 5