Skip to content

Broken PartialEq impl for sigset_t on Linux #2700

Closed
@magicant

Description

@magicant

On Linux, sigset_t is defined as a 1024-bit struct whereas only its first 64 bits are used. The sigemptyset function leaves the other 960 bits uninitialized.

The derived implementation of PartialEq for sigset_t naively compares all the bits of the struct and wrongly returns false depending on the uninitialized part.

$ cargo version -v
cargo 1.59.0 (49d8809dc 2022-02-10)
release: 1.59.0
commit-hash: 49d8809dc2d3e6e0d5ec634fcf26d8e2aab67130
commit-date: 2022-02-10
host: x86_64-unknown-linux-gnu
libgit2: 1.3.0 (sys:0.13.23 vendored)
libcurl: 7.80.0-DEV (sys:0.4.51+curl-7.80.0 vendored ssl:OpenSSL/1.1.1l)
os: OracleLinux 35.0.0 [64-bit]
$ cat Cargo.toml
[package]
name = "sigset-test"
version = "0.1.0"
edition = "2021"

[dependencies]
libc = { version = "0.2.119", features = ["extra_traits"] }
$ cat src/main.rs
fn main() {
    use std::mem::MaybeUninit;
    unsafe {
        let mut s1 = MaybeUninit::<libc::sigset_t>::uninit();
        let mut s2 = MaybeUninit::<libc::sigset_t>::uninit();
        libc::sigemptyset(s1.as_mut_ptr());
        libc::sigemptyset(s2.as_mut_ptr());
        let s1 = s1.assume_init();
        let s2 = s2.assume_init();
        // The two empty sets should compare equal
        assert_eq!(s1, s2);
    }
}
$ cargo r
   Compiling sigset-test v0.1.0 (/home/magicant/Desktop/sigset-test)
    Finished dev [unoptimized + debuginfo] target(s) in 0.20s
     Running `target/debug/sigset-test`
thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `sigset_t { __val: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] }`,
 right: `sigset_t { __val: [0, 0, 0, 0, 0, 0, 0, 0, 0, 139700099176444, 139700099131200, 139700096850408, 1736415839, 1, 139700096840524, 139700099177503] }`', src/main.rs:11:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: bug

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions