-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tuple struct's private field can be accessed outside the defining module #111220
Comments
This comment was marked as outdated.
This comment was marked as outdated.
This can be reproduced with cross-crate (e.i., any other crate can access the private field). This means that all encapsulations that take advantage of the field being private can be bypassed. (For example, if we change Vec to a tuple struct, the same thing as set_len could be done in safe code by abusing this bug.) // crate a
#[derive(Default)]
pub struct A(u32); // crate b
pub trait B {
fn f(&self);
}
impl B for b::A {
fn f(&self) {
let Self(a) = self;
println!("{}", a);
}
}
fn main() {
let a = b::A::default();
a.f();
} @rustbot label +I-unsound Also, this can be reproduced in all versions where self_struct_ctor is stable (e.i., 1.32+). https://godbolt.org/z/dWbKP3voc @KittyBorgX It's a matter of how you initialize Foo. If you provide a constructor for Foo, you won't get a compile error: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=3e9ec99b1aa66a2f767d61bf71cab9f2 |
Just for completeness, turning this into a segfault without external crates/just std: |
@rustbot label A-visibility |
this has been unsound ever since #56365 in version 1.32 |
WG-prioritization assigning priority (Zulip discussion). @rustbot label -I-prioritize +P-high |
This fixes rust-lang#111220 by checking the privacy of tuple constructors using `Self`, so the following code now errors ```rust mod my { pub struct Foo(&'static str); } impl AsRef<str> for my::Foo { fn as_ref(&self) -> &str { let Self(s) = self; // previously compiled, now errors correctly s } } ```
…truct-field, r=lcnr fix for `Self` not respecting tuple Ctor privacy This PR fixes rust-lang#111220 by checking the privacy of tuple constructors using `Self`, so the following code now errors ```rust mod my { pub struct Foo(&'static str); } impl AsRef<str> for my::Foo { fn as_ref(&self) -> &str { let Self(s) = self; // previously compiled, now errors correctly s } } ```
…truct-field, r=lcnr fix for `Self` not respecting tuple Ctor privacy This PR fixes rust-lang#111220 by checking the privacy of tuple constructors using `Self`, so the following code now errors ```rust mod my { pub struct Foo(&'static str); } impl AsRef<str> for my::Foo { fn as_ref(&self) -> &str { let Self(s) = self; // previously compiled, now errors correctly s } } ```
…truct-field, r=lcnr fix for `Self` not respecting tuple Ctor privacy This PR fixes rust-lang#111220 by checking the privacy of tuple constructors using `Self`, so the following code now errors ```rust mod my { pub struct Foo(&'static str); } impl AsRef<str> for my::Foo { fn as_ref(&self) -> &str { let Self(s) = self; // previously compiled, now errors correctly s } } ```
…uct-field, r=lcnr fix for `Self` not respecting tuple Ctor privacy This PR fixes rust-lang#111220 by checking the privacy of tuple constructors using `Self`, so the following code now errors ```rust mod my { pub struct Foo(&'static str); } impl AsRef<str> for my::Foo { fn as_ref(&self) -> &str { let Self(s) = self; // previously compiled, now errors correctly s } } ```
I tried this code:
I expected to see this happen: compilation should fail because the trait impl is defined outside of the module for the type but refers to its private field.
Instead, this happened: this compiled successfully
Meta
This reproduces on the playground for stable Rust, version 1.69.0: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=20f12a0f682d8dd6530d75f1b5e7a8dd
The text was updated successfully, but these errors were encountered: