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

checker: only allow ptr += int in unsafe blocks

This commit is contained in:
Delyan Angelov 2021-04-05 22:01:18 +03:00
parent dd31a1de9f
commit 0e94612535
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED

View File

@ -3127,8 +3127,13 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) {
// Dual sides check (compatibility check)
c.check_expected(right_type_unwrapped, left_type_unwrapped) or {
// allow for ptr += 2
if !left_type_unwrapped.is_ptr() && !right_type_unwrapped.is_int()
&& assign_stmt.op !in [.plus_assign, .minus_assign] {
if left_type_unwrapped.is_ptr() && right_type_unwrapped.is_int()
&& assign_stmt.op in [.plus_assign, .minus_assign] {
if !c.inside_unsafe {
c.warn('pointer arithmetic is only allowed in `unsafe` blocks',
assign_stmt.pos)
}
} else {
c.error('cannot assign to `$left`: $err.msg', right.position())
}
}