From c03798e3907c1796cdbb6bccbf30bb4dae220ddc Mon Sep 17 00:00:00 2001 From: Nick Treleaven Date: Thu, 25 Feb 2021 23:28:47 +0000 Subject: [PATCH] checker: disallow implicit conversion from fixed array to fooptr (#8823) --- vlib/math/big/big.v | 10 +++++++--- vlib/net/http/backend_nix.c.v | 7 ++++--- vlib/os/os_windows.c.v | 2 +- vlib/v/checker/check_types.v | 7 ------- vlib/v/checker/tests/fixed_array_conv.out | 18 ++++++++++++++++++ vlib/v/checker/tests/fixed_array_conv.vv | 6 ++++++ vlib/v/util/util.v | 10 ++++++---- 7 files changed, 42 insertions(+), 18 deletions(-) create mode 100644 vlib/v/checker/tests/fixed_array_conv.out create mode 100644 vlib/v/checker/tests/fixed_array_conv.vv diff --git a/vlib/math/big/big.v b/vlib/math/big/big.v index 451fcb17c4..fba8bc398d 100644 --- a/vlib/math/big/big.v +++ b/vlib/math/big/big.v @@ -147,9 +147,13 @@ pub fn (n Number) str() string { // .hexstr returns a hexadecimal representation of the bignum `n` pub fn (n Number) hexstr() string { mut buf := [8192]byte{} - // NB: C.bignum_to_string(), returns the HEXADECIMAL representation of the bignum n - C.bignum_to_string(&n, buf, 8192) - s := unsafe { tos_clone(buf) } + mut s := '' + unsafe { + bp := &buf[0] + // NB: C.bignum_to_string(), returns the HEXADECIMAL representation of the bignum n + C.bignum_to_string(&n, bp, 8192) + s = tos_clone(bp) + } if s.len == 0 { return '0' } diff --git a/vlib/net/http/backend_nix.c.v b/vlib/net/http/backend_nix.c.v index 2d87cc7d12..01a8ee14cb 100644 --- a/vlib/net/http/backend_nix.c.v +++ b/vlib/net/http/backend_nix.c.v @@ -41,20 +41,21 @@ fn (req &Request) ssl_do(port int, method Method, host_name string, path string) C.BIO_puts(web, req_headers.str) mut content := strings.new_builder(100) mut buff := [bufsize]byte{} + bp := &buff[0] mut readcounter := 0 for { readcounter++ - len := unsafe { C.BIO_read(web, buff, bufsize) } + len := unsafe { C.BIO_read(web, bp, bufsize) } if len <= 0 { break } $if debug_http ? { eprintln('ssl_do, read ${readcounter:4d} | len: $len') eprintln('-'.repeat(20)) - eprintln(unsafe { tos(buff, len) }) + eprintln(unsafe { tos(bp, len) }) eprintln('-'.repeat(20)) } - unsafe { content.write_bytes(buff, len) } + unsafe { content.write_bytes(bp, len) } } if web != 0 { C.BIO_free_all(web) diff --git a/vlib/os/os_windows.c.v b/vlib/os/os_windows.c.v index 31000f21a2..b9945d8110 100644 --- a/vlib/os/os_windows.c.v +++ b/vlib/os/os_windows.c.v @@ -276,7 +276,7 @@ pub fn exec(cmd string) ?Result { mut result := false unsafe { result = C.ReadFile(child_stdout_read, buf, 1000, voidptr(&bytes_read), 0) - read_data.write_bytes(buf, int(bytes_read)) + read_data.write_bytes(&buf[0], int(bytes_read)) } if result == false || int(bytes_read) == 0 { break diff --git a/vlib/v/checker/check_types.v b/vlib/v/checker/check_types.v index 55836d75c8..540d3af5c9 100644 --- a/vlib/v/checker/check_types.v +++ b/vlib/v/checker/check_types.v @@ -46,13 +46,6 @@ pub fn (mut c Checker) check_basic(got table.Type, expected table.Type) bool { return true } } - // e.g. [4096]byte vs byteptr - if got_sym.kind == .array_fixed { - info := got_sym.info as table.ArrayFixed - if c.table.type_to_str(info.elem_type) == c.table.type_to_str(expected).trim('ptr') { - return true - } - } if exp_sym.kind in [.voidptr, .any] || got_sym.kind in [.voidptr, .any] { return true } diff --git a/vlib/v/checker/tests/fixed_array_conv.out b/vlib/v/checker/tests/fixed_array_conv.out new file mode 100644 index 0000000000..c8e6684621 --- /dev/null +++ b/vlib/v/checker/tests/fixed_array_conv.out @@ -0,0 +1,18 @@ +vlib/v/checker/tests/fixed_array_conv.vv:3:3: error: mismatched types `voidptr` and `[2]int` + 1 | arr := [2,3]! + 2 | mut p := voidptr(0) + 3 | p = arr + | ^ + 4 | mut ip := &int(0) + 5 | ip = arr +vlib/v/checker/tests/fixed_array_conv.vv:5:4: error: mismatched types `&int` and `[2]int` + 3 | p = arr + 4 | mut ip := &int(0) + 5 | ip = arr + | ^ + 6 | _ = &int(arr) +vlib/v/checker/tests/fixed_array_conv.vv:6:6: error: cannot cast a fixed array (use e.g. `&arr[0]` instead) + 4 | mut ip := &int(0) + 5 | ip = arr + 6 | _ = &int(arr) + | ~~~~~~~~ diff --git a/vlib/v/checker/tests/fixed_array_conv.vv b/vlib/v/checker/tests/fixed_array_conv.vv new file mode 100644 index 0000000000..4a1f015ec2 --- /dev/null +++ b/vlib/v/checker/tests/fixed_array_conv.vv @@ -0,0 +1,6 @@ +arr := [2,3]! +mut p := voidptr(0) +p = arr +mut ip := &int(0) +ip = arr +_ = &int(arr) diff --git a/vlib/v/util/util.v b/vlib/v/util/util.v index c22ce068d6..8ed09ed015 100644 --- a/vlib/v/util/util.v +++ b/vlib/v/util/util.v @@ -30,8 +30,9 @@ pub fn vhash() string { mut buf := [50]byte{} buf[0] = 0 unsafe { - C.snprintf(charptr(&buf[0]), 50, '%s', C.V_COMMIT_HASH) - return tos_clone(buf) + bp := &buf[0] + C.snprintf(charptr(bp), 50, '%s', C.V_COMMIT_HASH) + return tos_clone(bp) } } @@ -98,8 +99,9 @@ pub fn githash(should_get_from_filesystem bool) string { mut buf := [50]byte{} buf[0] = 0 unsafe { - C.snprintf(charptr(&buf[0]), 50, '%s', C.V_CURRENT_COMMIT_HASH) - return tos_clone(buf) + bp := &buf[0] + C.snprintf(charptr(bp), 50, '%s', C.V_CURRENT_COMMIT_HASH) + return tos_clone(bp) } }