Skip to content

Commit eeaf8d9

Browse files
authored
checker: disallow invalid ptr operations (#21515)
1 parent a6704be commit eeaf8d9

File tree

5 files changed

+55
-0
lines changed

5 files changed

+55
-0
lines changed

vlib/v/checker/infix.v

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,12 @@ fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type {
331331
}
332332
}
333333

334+
if ((unwrapped_left_type.is_ptr() && !node.left.is_auto_deref_var())
335+
|| (unwrapped_right_type.is_ptr() && !node.right.is_auto_deref_var()))
336+
&& node.op !in [.plus, .minus] {
337+
c.error('infix `${node.op}` is not defined for pointer values', left_right_pos)
338+
}
339+
334340
if !c.pref.translated && left_sym.kind in [.array, .array_fixed, .map, .struct_] {
335341
if left_sym.has_method_with_generic_parent(node.op.str()) {
336342
if method := left_sym.find_method_with_generic_parent(node.op.str()) {

vlib/v/checker/tests/disallow_pointer_arithmetic_err.out

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,24 @@ vlib/v/checker/tests/disallow_pointer_arithmetic_err.vv:5:7: error: invalid oper
2525
| ~~~~~
2626
6 | _ := p * 2 //should be error
2727
7 | _ := p + 5 //OK but only in unsafe block, r is *int
28+
vlib/v/checker/tests/disallow_pointer_arithmetic_err.vv:5:7: error: infix `*` is not defined for pointer values
29+
3 | p := &x
30+
4 | _ := p + p //should be error
31+
5 | _ := p * p //should be error
32+
| ~~~~~
33+
6 | _ := p * 2 //should be error
34+
7 | _ := p + 5 //OK but only in unsafe block, r is *int
2835
vlib/v/checker/tests/disallow_pointer_arithmetic_err.vv:6:7: error: invalid operator `*` to `&int` and `int literal`
2936
4 | _ := p + p //should be error
3037
5 | _ := p * p //should be error
3138
6 | _ := p * 2 //should be error
3239
| ~~~~~
3340
7 | _ := p + 5 //OK but only in unsafe block, r is *int
3441
8 | _ := p - p //OK even in safe code, but n should be isize
42+
vlib/v/checker/tests/disallow_pointer_arithmetic_err.vv:6:7: error: infix `*` is not defined for pointer values
43+
4 | _ := p + p //should be error
44+
5 | _ := p * p //should be error
45+
6 | _ := p * 2 //should be error
46+
| ~~~~~
47+
7 | _ := p + 5 //OK but only in unsafe block, r is *int
48+
8 | _ := p - p //OK even in safe code, but n should be isize
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
vlib/v/checker/tests/invalid_op_ptr_err.vv:3:6: error: infix `*` is not defined for pointer values
2+
1 | num := 10
3+
2 | p := &num
4+
3 | x := 5 * p
5+
| ~~~~~
6+
4 | y := 5 / p
7+
5 | dump(x)
8+
vlib/v/checker/tests/invalid_op_ptr_err.vv:4:6: error: infix `/` is not defined for pointer values
9+
2 | p := &num
10+
3 | x := 5 * p
11+
4 | y := 5 / p
12+
| ~~~~~
13+
5 | dump(x)
14+
6 | dump(p)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
num := 10
2+
p := &num
3+
x := 5 * p
4+
y := 5 / p
5+
dump(x)
6+
dump(p)
7+
dump(y)

vlib/v/checker/tests/pointer_ops.out

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ vlib/v/checker/tests/pointer_ops.vv:15:3: error: invalid operator `%` to `&Foo`
5454
| ~~~~~~~
5555
16 | }
5656
17 | }
57+
vlib/v/checker/tests/pointer_ops.vv:15:3: error: infix `%` is not defined for pointer values
58+
13 | _ = p[3]
59+
14 | mut foo := &Foo{}
60+
15 | foo % 3
61+
| ~~~~~~~
62+
16 | }
63+
17 | }
5764
vlib/v/checker/tests/pointer_ops.vv:15:3: error: mismatched types `&Foo` and `int literal`
5865
13 | _ = p[3]
5966
14 | mut foo := &Foo{}
@@ -117,6 +124,13 @@ vlib/v/checker/tests/pointer_ops.vv:30:3: error: invalid operator `%` to `&Foo`
117124
| ~~~~~~~
118125
31 | }
119126
32 | }
127+
vlib/v/checker/tests/pointer_ops.vv:30:3: error: infix `%` is not defined for pointer values
128+
28 | _ = p[3]
129+
29 | mut foo := &Foo{}
130+
30 | foo % 3
131+
| ~~~~~~~
132+
31 | }
133+
32 | }
120134
vlib/v/checker/tests/pointer_ops.vv:30:3: error: mismatched types `&Foo` and `int literal`
121135
28 | _ = p[3]
122136
29 | mut foo := &Foo{}

0 commit comments

Comments
 (0)