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

fmt: fix possible code corruption by unwrapped single line if (#8780)

This commit is contained in:
Lukas Neubert 2021-02-16 09:13:48 +01:00 committed by GitHub
parent 230372df09
commit ad20b3806f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 3 deletions

View File

@ -1639,7 +1639,8 @@ pub fn (mut f Fmt) if_expr(node ast.IfExpr) {
&& branch_is_single_line(node.branches[0]) && branch_is_single_line(node.branches[1])
&& (node.is_expr || f.is_assign || f.is_struct_init || f.single_line_fields)
f.single_line_if = single_line
if_start := f.line_len
start_pos := f.out.len
start_len := f.line_len
for {
for i, branch in node.branches {
if i == 0 {
@ -1685,8 +1686,9 @@ pub fn (mut f Fmt) if_expr(node ast.IfExpr) {
if single_line && f.line_len > fmt.max_len.last() && !f.buffering {
single_line = false
f.single_line_if = false
f.out.go_back(f.line_len - if_start)
f.line_len = if_start
f.out.go_back_to(start_pos)
f.line_len = start_len
f.empty_line = start_len == 0
continue
}
break

View File

@ -7,3 +7,14 @@ fn (f Foo) method_with_or() int {
f.fn_with_optional() or { return 10 }
return 20
}
fn unwrapped_single_line_if() {
namefound := publisher.name_fix_check(name_to_find, state.site.id, ispage) or {
if err.contains('Could not find') {
state.error('cannot find link: $name_to_find')
} else {
state.error('cannot find link: $name_to_find\n$err')
}
println('Another stmt')
}
}