From 62553dcc2e7505a2c663666efb3c419ed822eba2 Mon Sep 17 00:00:00 2001 From: yuyi Date: Mon, 28 Mar 2022 17:20:47 +0800 Subject: [PATCH] cgen: fix crash for casting bool to int (fix #13825) (#13844) --- vlib/os/process_windows.c.v | 6 ++---- vlib/v/gen/c/cgen.v | 5 +++++ vlib/v/gen/c/sql.v | 4 ++-- vlib/v/tests/cast_bool_to_int_test.v | 12 ++++++++++++ 4 files changed, 21 insertions(+), 6 deletions(-) create mode 100644 vlib/v/tests/cast_bool_to_int_test.v diff --git a/vlib/os/process_windows.c.v b/vlib/os/process_windows.c.v index f5face4c03..57bdccc39c 100644 --- a/vlib/os/process_windows.c.v +++ b/vlib/os/process_windows.c.v @@ -190,10 +190,8 @@ fn (mut p Process) win_read_string(idx int, maxbytes int) (string, int) { return '', 0 } mut bytes_avail := int(0) - unsafe { - if C.PeekNamedPipe(rhandle, voidptr(0), int(0), voidptr(0), &bytes_avail, voidptr(0)) == false { - return '', 0 - } + if !C.PeekNamedPipe(rhandle, voidptr(0), int(0), voidptr(0), &bytes_avail, voidptr(0)) { + return '', 0 } if bytes_avail == 0 { return '', 0 diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index b10df0b4a8..f37e24da90 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -3771,6 +3771,11 @@ fn (mut g Gen) cast_expr(node ast.CastExpr) { g.write('))') } else if sym.kind == .alias && g.table.final_sym(node.typ).kind == .array_fixed { g.expr(node.expr) + } else if node.expr_type == ast.bool_type && node.typ.is_int() { + styp := g.typ(node.typ) + g.write('($styp[]){(') + g.expr(node.expr) + g.write(')?1:0}[0]') } else { styp := g.typ(node.typ) if (g.pref.translated || g.file.is_translated) && sym.kind == .function { diff --git a/vlib/v/gen/c/sql.v b/vlib/v/gen/c/sql.v index 9b82d343a4..465caef20c 100644 --- a/vlib/v/gen/c/sql.v +++ b/vlib/v/gen/c/sql.v @@ -120,7 +120,7 @@ fn (mut g Gen) sql_create_table(node ast.SqlStmtLine, expr string, table_name st } g.write('.typ = $typ,') g.write('.is_arr = ${sym.kind == .array}, ') - g.write('.is_time = ${int(g.table.get_type_name(field.typ) == 'time__Time')},') + g.write('.is_time = ${g.table.get_type_name(field.typ) == 'time__Time'},') g.write('.default_val = (string){.str = (byteptr) "$field.default_val", .is_lit = 1},') g.write('.attrs = new_array_from_c_array($field.attrs.len, $field.attrs.len, sizeof(StructAttribute),') if field.attrs.len > 0 { @@ -128,7 +128,7 @@ fn (mut g Gen) sql_create_table(node ast.SqlStmtLine, expr string, table_name st for attr in field.attrs { g.write('(StructAttribute){') g.write('.name = _SLIT("$attr.name"),') - g.write('.has_arg = ${int(attr.has_arg)},') + g.write('.has_arg = $attr.has_arg,') g.write('.arg = _SLIT("$attr.arg"),') g.write('.kind = ${int(attr.kind)},') g.write('},') diff --git a/vlib/v/tests/cast_bool_to_int_test.v b/vlib/v/tests/cast_bool_to_int_test.v new file mode 100644 index 0000000000..2158366fdd --- /dev/null +++ b/vlib/v/tests/cast_bool_to_int_test.v @@ -0,0 +1,12 @@ +module main + +fn test_cast_bool_to_int() { + i := true + a := [1, 2, 3] + + println(a[int(!i)]) + assert a[int(!i)] == 1 + + println(a[int(i)]) + assert a[int(i)] == 2 +}