Skip to content

IsZero for raw pointers is unsound #135338

Closed
@joboet

Description

@joboet

The IsZero trait is used to specialize vec![val; n] to use allocate_zeroed when the val being duplicated is zero. But in the case of raw pointers, this is not correct as the bytes returned by allocate_zeroed do not have the same provenance as val. Thus, the following code triggers undefined behaviour (playground) when it shouldn't

let ptr = std::ptr::from_ref(&42);
let zero = ptr.with_addr(0);
let roundtripped = vec![zero; 1].pop().unwrap();
let new = roundtripped.with_addr(ptr.addr());
unsafe { new.read() };

Metadata

Metadata

Assignees

Labels

C-bugCategory: This is a bug.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessT-libsRelevant to the library team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions