Skip to content

from_raw_os_error is inconsistent between raw and libc #1593

@stepancheg

Description

@stepancheg

Values like zero panic on raw and ignored on libc.

pub const fn from_raw_os_error(raw: i32) -> Self {
Self::from_errno(raw as u32)
}
/// Convert from a C `errno` value (which is positive) to an `Errno`.
const fn from_errno(raw: u32) -> Self {
// We store error values in negated form, so that we don't have to
// negate them after every syscall.
let encoded = raw.wrapping_neg() as u16;
// TODO: Use Range::contains, once that's `const`.
assert!(encoded >= 0xf001);
// SAFETY: Linux syscalls return negated error values in the range
// `-4095..0`, which we just asserted.
unsafe { Self(encoded) }
}
}

pub const fn from_raw_os_error(raw: i32) -> Self {
Self(raw)
}

I think this behavior should be consistent, but also panic is bad, because it is on error path, so if something goes bad (and probably rare and hardly reproducible), panic would make harder to debug such problems or result in unnecessary avoidable crashes.

Probably reset wrong value to EINVAL or something?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions