checker: prohibit fixed array to fixed array assignment where elem_typ is a pointer (#10775)

pull/10809/head
shadowninja55 2021-07-15 01:38:03 -04:00 committed by GitHub
parent 6e942bf4c2
commit 7c0be629ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 1 deletions

View File

@ -222,7 +222,8 @@ pub fn (mut ctx Context) new_streaming_image(w int, h int, channels int) int {
// update_pixel_data is a helper for working with image streams (i.e. images,
// that are updated dynamically by the CPU on each frame)
pub fn (mut ctx Context) update_pixel_data(cached_image_idx int, buf &byte) {
ctx.get_cached_image_by_idx(cached_image_idx).update_pixel_data(buf)
mut image := ctx.get_cached_image_by_idx(cached_image_idx)
image.update_pixel_data(buf)
}
pub fn (mut img Image) update_pixel_data(buf &byte) {

View File

@ -3909,6 +3909,16 @@ pub fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
c.error('use `array2 $node.op.str() array1.clone()` instead of `array2 $node.op.str() array1` (or use `unsafe`)',
node.pos)
}
if left_sym.kind == .array_fixed && !c.inside_unsafe && node.op in [.assign, .decl_assign]
&& right_sym.kind == .array_fixed && (left is ast.Ident && !left.is_blank_ident())
&& right is ast.Ident {
if right_sym.info is ast.ArrayFixed {
if right_sym.info.elem_type.is_ptr() {
c.error('assignment from one fixed array to another with a pointer element type is prohibited outside of `unsafe`',
node.pos)
}
}
}
if left_sym.kind == .map && node.op in [.assign, .decl_assign] && right_sym.kind == .map
&& ((right is ast.Ident && right.is_auto_deref_var())
|| !right_type.is_ptr()) && !left.is_blank_ident() && right.is_lvalue() {

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/unsafe_fixed_array_assign.vv:8:7: error: assignment from one fixed array to another with a pointer element type is prohibited outside of `unsafe`
6 | mut box := Box { num: 10 }
7 | a := [&box]!
8 | mut b := a
| ~~
9 | b[0].num = 0
10 | println(a)

View File

@ -0,0 +1,10 @@
struct Box {
mut:
num int
}
mut box := Box { num: 10 }
a := [&box]!
mut b := a
b[0].num = 0
println(a)