Skip to content

PyArray1::<bool>::as_slice is unsound #509

@orlp

Description

@orlp

PyArray1::<bool>::as_slice returns a &[bool], but the underlying data may not at all be valid for Rust's bool, creating instant undefined behavior. For example

>>> a = np.uint8([24, 15, 32, 1, 0]).view(bool)
>>> a
array([ True,  True,  True,  True, False])

Calling as_slice on this from Rust reveals the invalid representation:

dbg!(arr.as_slice().unwrap());
dbg!(unsafe { core::mem::transmute::<&[bool], &[u8]>(arr.as_slice().unwrap()) });
[ false, true, false, true, false, ]
[ 24, 15, 32, 1, 0, ]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions