Skip to content
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

Make the Debug implementation for unions always opaque #4118

Open
tgross35 opened this issue Nov 19, 2024 · 0 comments
Open

Make the Debug implementation for unions always opaque #4118

tgross35 opened this issue Nov 19, 2024 · 0 comments
Labels
E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-help-wanted Call for participation: Help is requested to fix this issue.

Comments

@tgross35
Copy link
Contributor

We currently have a lot of unsafe reads of unions fields which is source of easy unsoundness. Let's make things easier while avoiding this completely:

  1. Change s_no_extra_traits to do an opaque Debug implementation on unions, here

    libc/src/macros.rs

    Lines 122 to 129 in 2f931d9

    (it: $(#[$attr:meta])* pub union $i:ident { $($field:tt)* }) => (
    __item! {
    #[repr(C)]
    #[derive(Copy, Clone)]
    $(#[$attr])*
    pub union $i { $($field)* }
    }
    );
  2. Delete all existing Debug implementations on unions (which will now conflict).

The implementation is easy, this will just print the types as some_union { .. }:

#[cfg(feature = "extra_traits")]
impl ::core::fmt::Debug for $i {
    fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
        f.debug_struct(stringify!($i)).finish_non_exhaustive()
    }
}

We can backport this to libc-0.2 since it fixes some soundness issues without breaking any API.

@tgross35 tgross35 added E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-help-wanted Call for participation: Help is requested to fix this issue. labels Nov 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-help-wanted Call for participation: Help is requested to fix this issue.
Projects
None yet
Development

No branches or pull requests

1 participant