-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Invalid PartialEq implementation for unions #2816
Comments
Note that an implementation that compares the larger, ptr-sized fields would be unsound, since |
Crap, I never thought of that. Are you saying that it's impossible to correctly implement PartialEq for a union type then? In that case, would the only way to do it be to create a newtype that always zero-initializes the entire union before initializing any field? |
Depends on what you mean by 'correctly'... if you mean it should compare all the bytes, then yes. Probably the type should just not implement |
Is there some way to indicate that the |
I don't think so. AFAIK you still can't deprecate a trait impl rust-lang/rust#39935 . |
sigevent's Debug, PartialEq, and Hash trait impls might read union fields that could be potentially uninitialized by a standard initializer. Those trait impls shouldn't be present (see rust-lang#2816), but can't easily be removed. Until they get removed, the constructor must be `unsafe` to force the user to zero all fields. The same issue applies to the Deref<Target=sigevent_0_2_126> trait impl, which exists only for backwards compatibility.
sigevent's Debug, PartialEq, and Hash trait impls might read union fields that could be potentially uninitialized by a standard initializer. Those trait impls shouldn't be present (see rust-lang#2816), but can't easily be removed. Until they get removed, the constructor must be `unsafe` to force the user to zero all fields. The same issue applies to the Deref<Target=sigevent_0_2_126> trait impl, which exists only for backwards compatibility.
sigevent's Debug, PartialEq, and Hash trait impls might read union fields that could be potentially uninitialized by a standard initializer. Those trait impls shouldn't be present (see rust-lang#2816), but can't easily be removed. Until they get removed, the constructor must be `unsafe` to force the user to zero all fields. The same issue applies to the Deref<Target=sigevent_0_2_126> trait impl, which exists only for backwards compatibility.
sigevent's Debug, PartialEq, and Hash trait impls might read union fields that could be potentially uninitialized by a standard initializer. Those trait impls shouldn't be present (see rust-lang#2816), but can't easily be removed. Until they get removed, the constructor must be `unsafe` to force the user to zero all fields. The same issue applies to the Deref<Target=sigevent_0_2_126> trait impl, which exists only for backwards compatibility.
New instance of this unsoundness are still being introduced, e.g.: Lines 516 to 525 in a0f5b4b
|
To be discussed as part of #3880 |
union semun
, in unix/bsd/apple/mod.rs, has three fields. On LP64, these fields will have unequal sizes. But its PartialEq and Hash implementations only check the smallest field. So the following code should fail (untested, because I don't have access to a Mac):A similar situation holds on Linux for
__c_anonymous_ptrace_syscall_info_data
. That union has three members of different sizes, and itsPartialEq
implementation will return turn as long as any pair of fields compare equal. Effectively, that means it's only comparing the smallest field.There are other unions in libc, but I haven't audited them all.
The text was updated successfully, but these errors were encountered: