Skip to content

Commit 62553dc

Browse files
authored
cgen: fix crash for casting bool to int (fix #13825) (#13844)
1 parent c7a9242 commit 62553dc

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

vlib/os/process_windows.c.v

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,8 @@ fn (mut p Process) win_read_string(idx int, maxbytes int) (string, int) {
190190
return '', 0
191191
}
192192
mut bytes_avail := int(0)
193-
unsafe {
194-
if C.PeekNamedPipe(rhandle, voidptr(0), int(0), voidptr(0), &bytes_avail, voidptr(0)) == false {
195-
return '', 0
196-
}
193+
if !C.PeekNamedPipe(rhandle, voidptr(0), int(0), voidptr(0), &bytes_avail, voidptr(0)) {
194+
return '', 0
197195
}
198196
if bytes_avail == 0 {
199197
return '', 0

vlib/v/gen/c/cgen.v

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3771,6 +3771,11 @@ fn (mut g Gen) cast_expr(node ast.CastExpr) {
37713771
g.write('))')
37723772
} else if sym.kind == .alias && g.table.final_sym(node.typ).kind == .array_fixed {
37733773
g.expr(node.expr)
3774+
} else if node.expr_type == ast.bool_type && node.typ.is_int() {
3775+
styp := g.typ(node.typ)
3776+
g.write('($styp[]){(')
3777+
g.expr(node.expr)
3778+
g.write(')?1:0}[0]')
37743779
} else {
37753780
styp := g.typ(node.typ)
37763781
if (g.pref.translated || g.file.is_translated) && sym.kind == .function {

vlib/v/gen/c/sql.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,15 @@ fn (mut g Gen) sql_create_table(node ast.SqlStmtLine, expr string, table_name st
120120
}
121121
g.write('.typ = $typ,')
122122
g.write('.is_arr = ${sym.kind == .array}, ')
123-
g.write('.is_time = ${int(g.table.get_type_name(field.typ) == 'time__Time')},')
123+
g.write('.is_time = ${g.table.get_type_name(field.typ) == 'time__Time'},')
124124
g.write('.default_val = (string){.str = (byteptr) "$field.default_val", .is_lit = 1},')
125125
g.write('.attrs = new_array_from_c_array($field.attrs.len, $field.attrs.len, sizeof(StructAttribute),')
126126
if field.attrs.len > 0 {
127127
g.write(' _MOV((StructAttribute[$field.attrs.len]){')
128128
for attr in field.attrs {
129129
g.write('(StructAttribute){')
130130
g.write('.name = _SLIT("$attr.name"),')
131-
g.write('.has_arg = ${int(attr.has_arg)},')
131+
g.write('.has_arg = $attr.has_arg,')
132132
g.write('.arg = _SLIT("$attr.arg"),')
133133
g.write('.kind = ${int(attr.kind)},')
134134
g.write('},')

vlib/v/tests/cast_bool_to_int_test.v

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module main
2+
3+
fn test_cast_bool_to_int() {
4+
i := true
5+
a := [1, 2, 3]
6+
7+
println(a[int(!i)])
8+
assert a[int(!i)] == 1
9+
10+
println(a[int(i)])
11+
assert a[int(i)] == 2
12+
}

0 commit comments

Comments
 (0)