Skip to content

design flaw: Access out of bounds on pointer to array #386

@ofelas

Description

@ofelas

Unclear behaviour on array access

fn fillArray(src: &const [24]u8, dest: &[24]u8) -> usize {
    var idx: usize = 24;
    // access of of bounds
    dest[1] = src[0];
    // copies everything
    // dest[0] = src[0];

    // error: expected type '[24]u8', found 'u8'
    //dest[1] = u8('a');
    (*dest)[14] = 'b';
    (*dest)[3] = 'c';
    // access out of bounds
    dest[idx] = src[0];
    // segfault
    //(*dest)[idx] = (*src)[0];
    // error: index 24 outside array of size 24
    //(*dest)[24] = 'b';

    return (*dest).len;
}

pub fn main() -> %void {
    var s : [24]u8 = []u8{0} ** 24;
    var ss : [24]u8 = []u8{'a'} ** 24;
    const l = fillArray(&ss, &s);
    if (s[0] == 'a') {
       s[1] = s[0];
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    breakingImplementing this issue could cause existing code to no longer compile or have different behavior.enhancementSolving this issue will likely involve adding new logic or components to the codebase.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions