Skip to content

preallocated_* promotes unsoundness #665

Open
@Kixunil

Description

@Kixunil

These methods take &mut [AlignedType] which must be initialized because it does not contain MaybeUninit. The API may entice people to write this code though:

unsafe {
    let buffer = allocator.alloc(size, alignment); // returns uninitialized data
    let buffer = core::slice::from_raw_parts(buffer, size);
    Secp256k1::preallocated_new(buffer)
}

This is unsound and it's not obvious. (The whole maybe uninit business is subtle and not widely understood by people.)

I was thinking about making it accept &mut [MaybeUninit<AlignedType>] but that may be annoying for consumers and promotes casting &mut T to &mut MaybeUninit<T> which, while not unsound itself, needs to be treated carefully. So the only sane option that I can think of is defining AlignedType as struct AlignedType(MaybeUninit<[u8, 16]>), providing conversions from pointers/MaybeUninit<[AlignedType]> and documenting that it's fine to have it uninitialized.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions