Skip to content

Unsoundness: Patterns in function parameters are not checked for union access #130528

Closed
@ChayimFriedman2

Description

@ChayimFriedman2

I tried this code:

union U {
    a: &'static i32,
    b: usize,
}

fn foo(U { a }: U) {
    dbg!(*a);
}

fn main() {
    foo(U { b: 0 });
}

Playground.

I expected to see this happen: rustc rejecting this code. We are transmuting arbitrary types without unsafe, this is clearly unsound.

Instead, this happened: rustc accepted this code, and it SIGSEGVs at runtime.

Metadata

Metadata

Labels

C-bugCategory: This is a bug.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessP-criticalCritical priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions