mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker: disallow deferencing a nil
pointer (#18038)
This commit is contained in:
parent
f598bbde4e
commit
ac58eca015
@ -3876,6 +3876,15 @@ fn (mut c Checker) prefix_expr(mut node ast.PrefixExpr) ast.Type {
|
|||||||
if right_type.is_voidptr() {
|
if right_type.is_voidptr() {
|
||||||
c.error('cannot dereference to void', node.pos)
|
c.error('cannot dereference to void', node.pos)
|
||||||
}
|
}
|
||||||
|
if mut node.right is ast.Ident {
|
||||||
|
if var := node.right.scope.find_var('${node.right.name}') {
|
||||||
|
if var.expr is ast.UnsafeExpr {
|
||||||
|
if var.expr.expr is ast.Nil {
|
||||||
|
c.error('cannot deference a `nil` pointer', node.right.pos)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if node.op == .bit_not && !c.unwrap_generic(right_type).is_int() && !c.pref.translated
|
if node.op == .bit_not && !c.unwrap_generic(right_type).is_int() && !c.pref.translated
|
||||||
&& !c.file.is_translated {
|
&& !c.file.is_translated {
|
||||||
|
5
vlib/v/checker/tests/deference_nil_ptr_err.out
Normal file
5
vlib/v/checker/tests/deference_nil_ptr_err.out
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
vlib/v/checker/tests/deference_nil_ptr_err.vv:3:10: error: cannot deference a `nil` pointer
|
||||||
|
1 | foo_ptr := unsafe { nil }
|
||||||
|
2 |
|
||||||
|
3 | println(*foo_ptr)
|
||||||
|
| ~~~~~~~
|
3
vlib/v/checker/tests/deference_nil_ptr_err.vv
Normal file
3
vlib/v/checker/tests/deference_nil_ptr_err.vv
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
foo_ptr := unsafe { nil }
|
||||||
|
|
||||||
|
println(*foo_ptr)
|
Loading…
x
Reference in New Issue
Block a user