1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix crash for casting bool to int (fix #13825) (#13844)

This commit is contained in:
yuyi 2022-03-28 17:20:47 +08:00 committed by GitHub
parent c7a92425f0
commit 62553dcc2e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 6 deletions

View File

@ -190,11 +190,9 @@ 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 {
if !C.PeekNamedPipe(rhandle, voidptr(0), int(0), voidptr(0), &bytes_avail, voidptr(0)) {
return '', 0
}
}
if bytes_avail == 0 {
return '', 0
}

View File

@ -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 {

View File

@ -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('},')

View File

@ -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
}