Skip to content

Commit

Permalink
checker: disallow invalid ptr operations (#21515)
Browse files Browse the repository at this point in the history
  • Loading branch information
Delta456 committed May 16, 2024
1 parent a6704be commit eeaf8d9
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 0 deletions.
6 changes: 6 additions & 0 deletions vlib/v/checker/infix.v
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,12 @@ fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type {
}
}

if ((unwrapped_left_type.is_ptr() && !node.left.is_auto_deref_var())
|| (unwrapped_right_type.is_ptr() && !node.right.is_auto_deref_var()))
&& node.op !in [.plus, .minus] {
c.error('infix `${node.op}` is not defined for pointer values', left_right_pos)
}

if !c.pref.translated && left_sym.kind in [.array, .array_fixed, .map, .struct_] {
if left_sym.has_method_with_generic_parent(node.op.str()) {
if method := left_sym.find_method_with_generic_parent(node.op.str()) {
Expand Down
14 changes: 14 additions & 0 deletions vlib/v/checker/tests/disallow_pointer_arithmetic_err.out
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,24 @@ vlib/v/checker/tests/disallow_pointer_arithmetic_err.vv:5:7: error: invalid oper
| ~~~~~
6 | _ := p * 2 //should be error
7 | _ := p + 5 //OK but only in unsafe block, r is *int
vlib/v/checker/tests/disallow_pointer_arithmetic_err.vv:5:7: error: infix `*` is not defined for pointer values
3 | p := &x
4 | _ := p + p //should be error
5 | _ := p * p //should be error
| ~~~~~
6 | _ := p * 2 //should be error
7 | _ := p + 5 //OK but only in unsafe block, r is *int
vlib/v/checker/tests/disallow_pointer_arithmetic_err.vv:6:7: error: invalid operator `*` to `&int` and `int literal`
4 | _ := p + p //should be error
5 | _ := p * p //should be error
6 | _ := p * 2 //should be error
| ~~~~~
7 | _ := p + 5 //OK but only in unsafe block, r is *int
8 | _ := p - p //OK even in safe code, but n should be isize
vlib/v/checker/tests/disallow_pointer_arithmetic_err.vv:6:7: error: infix `*` is not defined for pointer values
4 | _ := p + p //should be error
5 | _ := p * p //should be error
6 | _ := p * 2 //should be error
| ~~~~~
7 | _ := p + 5 //OK but only in unsafe block, r is *int
8 | _ := p - p //OK even in safe code, but n should be isize
14 changes: 14 additions & 0 deletions vlib/v/checker/tests/invalid_op_ptr_err.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
vlib/v/checker/tests/invalid_op_ptr_err.vv:3:6: error: infix `*` is not defined for pointer values
1 | num := 10
2 | p := &num
3 | x := 5 * p
| ~~~~~
4 | y := 5 / p
5 | dump(x)
vlib/v/checker/tests/invalid_op_ptr_err.vv:4:6: error: infix `/` is not defined for pointer values
2 | p := &num
3 | x := 5 * p
4 | y := 5 / p
| ~~~~~
5 | dump(x)
6 | dump(p)
7 changes: 7 additions & 0 deletions vlib/v/checker/tests/invalid_op_ptr_err.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
num := 10
p := &num
x := 5 * p
y := 5 / p
dump(x)
dump(p)
dump(y)
14 changes: 14 additions & 0 deletions vlib/v/checker/tests/pointer_ops.out
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ vlib/v/checker/tests/pointer_ops.vv:15:3: error: invalid operator `%` to `&Foo`
| ~~~~~~~
16 | }
17 | }
vlib/v/checker/tests/pointer_ops.vv:15:3: error: infix `%` is not defined for pointer values
13 | _ = p[3]
14 | mut foo := &Foo{}
15 | foo % 3
| ~~~~~~~
16 | }
17 | }
vlib/v/checker/tests/pointer_ops.vv:15:3: error: mismatched types `&Foo` and `int literal`
13 | _ = p[3]
14 | mut foo := &Foo{}
Expand Down Expand Up @@ -117,6 +124,13 @@ vlib/v/checker/tests/pointer_ops.vv:30:3: error: invalid operator `%` to `&Foo`
| ~~~~~~~
31 | }
32 | }
vlib/v/checker/tests/pointer_ops.vv:30:3: error: infix `%` is not defined for pointer values
28 | _ = p[3]
29 | mut foo := &Foo{}
30 | foo % 3
| ~~~~~~~
31 | }
32 | }
vlib/v/checker/tests/pointer_ops.vv:30:3: error: mismatched types `&Foo` and `int literal`
28 | _ = p[3]
29 | mut foo := &Foo{}
Expand Down

0 comments on commit eeaf8d9

Please sign in to comment.