From 276c1de19026e907eba27fcaa4d83eaa5fd67d2a Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Wed, 22 Jul 2020 21:42:51 +0300 Subject: [PATCH] unsafe: tag with unsafe{} some more pointer manipulations --- vlib/builtin/string_test.v | 12 +++++++----- vlib/clipboard/clipboard_linux.c.v | 10 +++++----- vlib/crypto/rand/rand_linux.c.v | 2 +- vlib/net/ftp/ftp.v | 2 +- vlib/picohttpparser/misc.v | 6 +++--- vlib/v/tests/autolock_array1_test.v | 4 +++- vlib/v/tests/autolock_array2_test.v | 8 +++++--- vlib/v/tests/semaphore_timed_test.v | 2 +- vlib/v/tests/shared_array_test.v | 23 ++++++++++++++++++----- 9 files changed, 44 insertions(+), 25 deletions(-) diff --git a/vlib/builtin/string_test.v b/vlib/builtin/string_test.v index abc8e22d73..9232530967 100644 --- a/vlib/builtin/string_test.v +++ b/vlib/builtin/string_test.v @@ -478,11 +478,13 @@ fn test_reverse() { fn test_bytes_to_string() { mut buf := vcalloc(10) - buf[0] = `h` - buf[1] = `e` - buf[2] = `l` - buf[3] = `l` - buf[4] = `o` + unsafe { + buf[0] = `h` + buf[1] = `e` + buf[2] = `l` + buf[3] = `l` + buf[4] = `o` + } assert string(buf) == 'hello' assert string(buf, 2) == 'he' bytes := [`h`, `e`, `l`, `l`, `o`] diff --git a/vlib/clipboard/clipboard_linux.c.v b/vlib/clipboard/clipboard_linux.c.v index ea1cdf60d2..8a0471803c 100644 --- a/vlib/clipboard/clipboard_linux.c.v +++ b/vlib/clipboard/clipboard_linux.c.v @@ -381,12 +381,12 @@ fn (cb &Clipboard) pick_target(prop Property) C.Atom { //See if this data type is allowed and of higher priority (closer to zero) //than the present one. - if cb.is_supported_target(atom_list[i]) { - index := cb.get_target_index(atom_list[i]) - if priority > index && index >= 0 - { + target := unsafe{ atom_list[i] } + if cb.is_supported_target(target) { + index := cb.get_target_index(target) + if priority > index && index >= 0 { priority = index - to_be_requested = atom_list[i] + to_be_requested = target } } } diff --git a/vlib/crypto/rand/rand_linux.c.v b/vlib/crypto/rand/rand_linux.c.v index 8c8ddb6378..d29de7173a 100644 --- a/vlib/crypto/rand/rand_linux.c.v +++ b/vlib/crypto/rand/rand_linux.c.v @@ -36,5 +36,5 @@ fn getrandom(bytes_needed int, buffer voidptr) int { if bytes_needed > read_batch_size { panic('getrandom() dont request more than $read_batch_size bytes at once.') } - return C.syscall(C.SYS_getrandom, buffer, bytes_needed, 0) + return unsafe { C.syscall(C.SYS_getrandom, buffer, bytes_needed, 0) } } diff --git a/vlib/net/ftp/ftp.v b/vlib/net/ftp/ftp.v index 6790293f90..51c9ea0fe0 100644 --- a/vlib/net/ftp/ftp.v +++ b/vlib/net/ftp/ftp.v @@ -48,7 +48,7 @@ fn (dtp DTP) read() []byte { break } for i in 0 .. len { - data << buf[i] + data << unsafe { buf[i] } } unsafe { free(buf) diff --git a/vlib/picohttpparser/misc.v b/vlib/picohttpparser/misc.v index 2610925693..038a2f25a3 100644 --- a/vlib/picohttpparser/misc.v +++ b/vlib/picohttpparser/misc.v @@ -2,17 +2,17 @@ module picohttpparser [inline] [unsafe_fn] fn cpy(dst, src byteptr, len int) int { - C.memcpy(dst, src, len) + unsafe { C.memcpy(dst, src, len) } return len } [inline] pub fn cmp(dst, src string) bool { if dst.len != src.len { return false } - return C.memcmp(dst.str, src.str, src.len) == 0 + return unsafe { C.memcmp(dst.str, src.str, src.len) == 0 } } [inline] pub fn cmpn(dst, src string, n int) bool { - return C.memcmp(dst.str, src.str, n) == 0 + return unsafe { C.memcmp(dst.str, src.str, n) == 0 } } diff --git a/vlib/v/tests/autolock_array1_test.v b/vlib/v/tests/autolock_array1_test.v index 1f2fe5deb5..0598ee81ae 100644 --- a/vlib/v/tests/autolock_array1_test.v +++ b/vlib/v/tests/autolock_array1_test.v @@ -24,7 +24,9 @@ fn test_autolocked_array() { for { mut finished_threads := 0 rlock abc { - finished_threads = abc[0] + finished_threads = unsafe { + abc[0] + } } if finished_threads == 2 { break diff --git a/vlib/v/tests/autolock_array2_test.v b/vlib/v/tests/autolock_array2_test.v index 4385bc6207..75a552b6ab 100644 --- a/vlib/v/tests/autolock_array2_test.v +++ b/vlib/v/tests/autolock_array2_test.v @@ -17,14 +17,16 @@ fn test_autolocked_array_2() { go inc_elements(shared abc, 1, sem) go inc_elements(shared abc, 2, sem) for _ in 0 .. iterations_per_thread2 { - abc[2]++ + unsafe { + abc[2]++ + } } // wait for the 2 coroutines to finish using the semaphore for _ in 0 .. 2 { sem.wait() } rlock abc { - assert abc[1] == iterations_per_thread2 - assert abc[2] == 2 * iterations_per_thread2 + assert unsafe { abc[1] } == iterations_per_thread2 + assert unsafe { abc[2] } == 2 * iterations_per_thread2 } } diff --git a/vlib/v/tests/semaphore_timed_test.v b/vlib/v/tests/semaphore_timed_test.v index f106409ccf..ca876a551d 100644 --- a/vlib/v/tests/semaphore_timed_test.v +++ b/vlib/v/tests/semaphore_timed_test.v @@ -13,7 +13,7 @@ fn test_semaphore() { sem := sync.new_semaphore() go run_forever(shared abc, sem) for _ in 0 .. 1000 { - abc[0]-- + unsafe { abc[0]-- } } // wait for the 2 coroutines to finish using the semaphore stopwatch := time.new_stopwatch({}) diff --git a/vlib/v/tests/shared_array_test.v b/vlib/v/tests/shared_array_test.v index 2382940f47..cdad8a92f1 100644 --- a/vlib/v/tests/shared_array_test.v +++ b/vlib/v/tests/shared_array_test.v @@ -20,14 +20,19 @@ fn test_shared_array() { go incr(shared foo, 1) for _ in 0 .. 50000 { lock foo { - foo[0] -= 2 - foo[1] += 3 + unsafe { + foo[0] -= 2 + foo[1] += 3 + } } } mut finished_threads := 0 for { rlock foo { - finished_threads = foo[2] + finished_threads = unsafe { + foo[2] + } + } if finished_threads == 4 { break @@ -35,7 +40,15 @@ fn test_shared_array() { time.sleep_ms(100) } rlock foo { - assert foo[0] == 100010 - assert foo[1] == 350020 + f0 := unsafe { + foo[0] + } + + f1 := unsafe { + foo[1] + } + + assert f0 == 100010 + assert f1 == 350020 } }