From 4edade50674c6ec326e3d0aa5c4fc4c2ee1c2700 Mon Sep 17 00:00:00 2001 From: Dwight Schauer <52499484+bogen85@users.noreply.github.com> Date: Fri, 22 Nov 2019 00:11:06 -0600 Subject: [PATCH] compiler: fix a deferencing cast was causing a segfault --- vlib/compiler/gen_c.v | 2 +- vlib/compiler/tests/voidptr_to_u64_cast_a_test.v | 11 +++++++++++ vlib/compiler/tests/voidptr_to_u64_cast_b_test.v | 11 +++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 vlib/compiler/tests/voidptr_to_u64_cast_a_test.v create mode 100644 vlib/compiler/tests/voidptr_to_u64_cast_b_test.v diff --git a/vlib/compiler/gen_c.v b/vlib/compiler/gen_c.v index bedc14cc41..c35c7b653f 100644 --- a/vlib/compiler/gen_c.v +++ b/vlib/compiler/gen_c.v @@ -538,7 +538,7 @@ fn (p mut Parser) cast(typ string) { p.error('cannot cast `$expr_typ` to `$typ`, use backquotes `` to create a `$typ` or access the value of an index of `$expr_typ` using []') } else if casting_voidptr_to_value { - p.cgen.set_placeholder(pos, '*($typ*)(') + p.cgen.set_placeholder(pos, '($typ)(') } else { // Nothing can be cast to bool diff --git a/vlib/compiler/tests/voidptr_to_u64_cast_a_test.v b/vlib/compiler/tests/voidptr_to_u64_cast_a_test.v new file mode 100644 index 0000000000..667e9d4381 --- /dev/null +++ b/vlib/compiler/tests/voidptr_to_u64_cast_a_test.v @@ -0,0 +1,11 @@ +fn receive_addr_return_u64 (addr voidptr) u64 { + return u64(addr) +} + +fn test_void_pointer_to_u64_cast_via_fn_call() { + a := u64(10) + b := voidptr(a) + c := receive_addr_return_u64(b) + assert (a == c) +} + diff --git a/vlib/compiler/tests/voidptr_to_u64_cast_b_test.v b/vlib/compiler/tests/voidptr_to_u64_cast_b_test.v new file mode 100644 index 0000000000..7f1bfe20a1 --- /dev/null +++ b/vlib/compiler/tests/voidptr_to_u64_cast_b_test.v @@ -0,0 +1,11 @@ +fn receive_u64_return_addr (something u64) voidptr { + return voidptr(something) +} + +fn test_u64_to_void_pointer_cast_via_fn_call() { + a := u64(100) + b := receive_u64_return_addr(a) + c := u64(b) + assert (a == c) +} +