1
0
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:
Swastik Baranwal 2023-04-25 03:40:01 +05:30 committed by GitHub
parent f598bbde4e
commit ac58eca015
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 0 deletions

View File

@ -3876,6 +3876,15 @@ fn (mut c Checker) prefix_expr(mut node ast.PrefixExpr) ast.Type {
if right_type.is_voidptr() {
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
&& !c.file.is_translated {

View 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)
| ~~~~~~~

View File

@ -0,0 +1,3 @@
foo_ptr := unsafe { nil }
println(*foo_ptr)