Skip to content

Commit

Permalink
Mark most C-derived enums as non_exhaustive
Browse files Browse the repository at this point in the history
Since libc may add new variants at any time, Nix's consumers should not
use exhaustive match patterns.

Fixes #1182
  • Loading branch information
asomers committed Jul 24, 2021
1 parent 7033d47 commit 358adaf
Show file tree
Hide file tree
Showing 17 changed files with 43 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ This project adheres to [Semantic Versioning](https://semver.org/).
- `FdSet::{contains, highest, fds}` no longer require a mutable reference.
(#[1464](https://github.com/nix-rust/nix/pull/1464))

- Most enums that come from C, for example `Errno`, are now marked as
`#[non_exhaustive]`.
(#[1474](https://github.com/nix-rust/nix/pull/1474))

### Fixed

- Added more errno definitions for better backwards compatibility with
Expand Down
8 changes: 8 additions & 0 deletions src/errno.rs
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,7 @@ fn desc(errno: Errno) -> &'static str {
mod consts {
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(i32)]
#[non_exhaustive]
pub enum Errno {
UnknownErrno = 0,
EPERM = libc::EPERM,
Expand Down Expand Up @@ -1073,6 +1074,7 @@ mod consts {
mod consts {
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(i32)]
#[non_exhaustive]
pub enum Errno {
UnknownErrno = 0,
EPERM = libc::EPERM,
Expand Down Expand Up @@ -1324,6 +1326,7 @@ mod consts {
mod consts {
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(i32)]
#[non_exhaustive]
pub enum Errno {
UnknownErrno = 0,
EPERM = libc::EPERM,
Expand Down Expand Up @@ -1562,6 +1565,7 @@ mod consts {
mod consts {
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(i32)]
#[non_exhaustive]
pub enum Errno {
UnknownErrno = 0,
EPERM = libc::EPERM,
Expand Down Expand Up @@ -1796,6 +1800,7 @@ mod consts {
mod consts {
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(i32)]
#[non_exhaustive]
pub enum Errno {
UnknownErrno = 0,
EPERM = libc::EPERM,
Expand Down Expand Up @@ -2019,6 +2024,7 @@ mod consts {
mod consts {
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(i32)]
#[non_exhaustive]
pub enum Errno {
UnknownErrno = 0,
EPERM = libc::EPERM,
Expand Down Expand Up @@ -2244,6 +2250,7 @@ mod consts {
mod consts {
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(i32)]
#[non_exhaustive]
pub enum Errno {
UnknownErrno = 0,
EPERM = libc::EPERM,
Expand Down Expand Up @@ -2441,6 +2448,7 @@ mod consts {
mod consts {
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(i32)]
#[non_exhaustive]
pub enum Errno {
UnknownErrno = 0,
EPERM = libc::EPERM,
Expand Down
4 changes: 4 additions & 0 deletions src/fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ libc_bitflags!(

#[cfg(not(target_os = "redox"))]
#[derive(Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum FcntlArg<'a> {
F_DUPFD(RawFd),
F_DUPFD_CLOEXEC(RawFd),
Expand Down Expand Up @@ -405,6 +406,7 @@ pub enum FcntlArg<'a> {

#[cfg(target_os = "redox")]
#[derive(Debug, Clone, Copy, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum FcntlArg {
F_DUPFD(RawFd),
F_DUPFD_CLOEXEC(RawFd),
Expand Down Expand Up @@ -454,6 +456,7 @@ pub fn fcntl(fd: RawFd, arg: FcntlArg) -> Result<c_int> {
}

#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum FlockArg {
LockShared,
LockExclusive,
Expand Down Expand Up @@ -649,6 +652,7 @@ mod posix_fadvise {

libc_enum! {
#[repr(i32)]
#[non_exhaustive]
pub enum PosixFadviseAdvice {
POSIX_FADV_NORMAL,
POSIX_FADV_SEQUENTIAL,
Expand Down
2 changes: 2 additions & 0 deletions src/sys/aio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ libc_enum! {
/// Mode for `AioCb::fsync`. Controls whether only data or both data and
/// metadata are synced.
#[repr(i32)]
#[non_exhaustive]
pub enum AioFsyncMode {
/// do it like `fsync`
O_SYNC,
Expand All @@ -57,6 +58,7 @@ libc_enum! {
/// given `aiocb` should be used for a read operation, a write operation, or
/// ignored. Has no effect for any other aio functions.
#[repr(i32)]
#[non_exhaustive]
pub enum LioOpcode {
LIO_NOP,
LIO_WRITE,
Expand Down
1 change: 1 addition & 0 deletions src/sys/epoll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ libc_bitflags!(

#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[repr(i32)]
#[non_exhaustive]
pub enum EpollOp {
EpollCtlAdd = libc::EPOLL_CTL_ADD,
EpollCtlDel = libc::EPOLL_CTL_DEL,
Expand Down
1 change: 1 addition & 0 deletions src/sys/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type type_of_event_filter = i16;
libc_enum! {
#[cfg_attr(target_os = "netbsd", repr(u32))]
#[cfg_attr(not(target_os = "netbsd"), repr(i16))]
#[non_exhaustive]
pub enum EventFilter {
EVFILT_AIO,
/// Returns whenever there is no remaining data in the write buffer
Expand Down
1 change: 1 addition & 0 deletions src/sys/mman.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ libc_enum!{
///
/// Used by [`madvise`](./fn.madvise.html).
#[repr(i32)]
#[non_exhaustive]
pub enum MmapAdvise {
/// No further special treatment. This is the default.
MADV_NORMAL,
Expand Down
1 change: 1 addition & 0 deletions src/sys/ptrace/bsd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ cfg_if! {
libc_enum! {
#[repr(i32)]
/// Ptrace Request enum defining the action to be taken.
#[non_exhaustive]
pub enum Request {
PT_TRACE_ME,
PT_READ_I,
Expand Down
2 changes: 2 additions & 0 deletions src/sys/ptrace/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ libc_enum!{
#[cfg_attr(not(any(target_env = "musl", target_os = "android")), repr(u32))]
#[cfg_attr(any(target_env = "musl", target_os = "android"), repr(i32))]
/// Ptrace Request enum defining the action to be taken.
#[non_exhaustive]
pub enum Request {
PTRACE_TRACEME,
PTRACE_PEEKTEXT,
Expand Down Expand Up @@ -123,6 +124,7 @@ libc_enum!{
/// Using the ptrace options the tracer can configure the tracee to stop
/// at certain events. This enum is used to define those events as defined
/// in `man ptrace`.
#[non_exhaustive]
pub enum Event {
/// Event that stops before a return from fork or clone.
PTRACE_EVENT_FORK,
Expand Down
2 changes: 2 additions & 0 deletions src/sys/quota.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ libc_enum!{
libc_enum!{
/// The scope of the quota.
#[repr(i32)]
#[non_exhaustive]
pub enum QuotaType {
/// Specify a user quota
USRQUOTA,
Expand All @@ -53,6 +54,7 @@ libc_enum!{
libc_enum!{
/// The type of quota format to use.
#[repr(i32)]
#[non_exhaustive]
pub enum QuotaFmt {
/// Use the original quota format.
QFMT_VFS_OLD,
Expand Down
1 change: 1 addition & 0 deletions src/sys/reboot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ libc_enum! {
/// See [`set_cad_enabled()`](fn.set_cad_enabled.html) for
/// enabling/disabling Ctrl-Alt-Delete.
#[repr(i32)]
#[non_exhaustive]
pub enum RebootMode {
RB_HALT_SYSTEM,
RB_KEXEC,
Expand Down
2 changes: 2 additions & 0 deletions src/sys/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ libc_enum!{
// We would prefer to use the libc::c_int alias in the repr attribute. Unfortunately
// this is not (yet) possible.
#[repr(i32)]
#[non_exhaustive]
pub enum Signal {
SIGHUP,
SIGINT,
Expand Down Expand Up @@ -396,6 +397,7 @@ libc_bitflags!{

libc_enum! {
#[repr(i32)]
#[non_exhaustive]
pub enum SigmaskHow {
SIG_BLOCK,
SIG_UNBLOCK,
Expand Down
2 changes: 2 additions & 0 deletions src/sys/socket/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub use self::vsock::VsockAddr;
/// These constants specify the protocol family to be used
/// in [`socket`](fn.socket.html) and [`socketpair`](fn.socketpair.html)
#[repr(i32)]
#[non_exhaustive]
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
pub enum AddressFamily {
/// Local communication (see [`unix(7)`](https://man7.org/linux/man-pages/man7/unix.7.html))
Expand Down Expand Up @@ -641,6 +642,7 @@ impl Hash for UnixAddr {

/// Represents a socket address
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum SockAddr {
Inet(InetAddr),
Unix(UnixAddr),
Expand Down
4 changes: 4 additions & 0 deletions src/sys/socket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ pub use libc::{c_uint, CMSG_SPACE};
/// when creating a socket with [`socket()`](fn.socket.html)
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[repr(i32)]
#[non_exhaustive]
pub enum SockType {
/// Provides sequenced, reliable, two-way, connection-
/// based byte streams. An out-of-band data transmission
Expand All @@ -94,6 +95,7 @@ pub enum SockType {
/// to specify the protocol to use.
#[repr(i32)]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum SockProtocol {
/// TCP protocol ([ip(7)](https://man7.org/linux/man-pages/man7/ip.7.html))
Tcp = libc::IPPROTO_TCP,
Expand Down Expand Up @@ -490,6 +492,7 @@ impl<'a> Iterator for CmsgIterator<'a> {
//
// See https://github.com/nix-rust/nix/issues/999
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum ControlMessageOwned {
/// Received version of
/// [`ControlMessage::ScmRights`][#enum.ControlMessage.html#variant.ScmRights]
Expand Down Expand Up @@ -739,6 +742,7 @@ impl ControlMessageOwned {
///
/// [Further reading](https://man7.org/linux/man-pages/man3/cmsg.3.html)
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum ControlMessage<'a> {
/// A message of type `SCM_RIGHTS`, containing an array of file
/// descriptors passed between processes.
Expand Down
5 changes: 5 additions & 0 deletions src/sys/termios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ libc_enum!{
/// B0 is special and will disable the port.
#[cfg_attr(all(any(target_os = "ios", target_os = "macos"), target_pointer_width = "64"), repr(u64))]
#[cfg_attr(not(all(any(target_os = "ios", target_os = "macos"), target_pointer_width = "64")), repr(u32))]
#[non_exhaustive]
pub enum BaudRate {
B0,
B50,
Expand Down Expand Up @@ -472,6 +473,7 @@ libc_enum! {
///
/// Used as an argument to `tcsetattr()`
#[repr(i32)]
#[non_exhaustive]
pub enum SetArg {
/// The change will occur immediately
TCSANOW,
Expand All @@ -487,6 +489,7 @@ libc_enum! {
///
/// Used as an argument to `tcflush()`.
#[repr(i32)]
#[non_exhaustive]
pub enum FlushArg {
/// Flush data that was received but not read
TCIFLUSH,
Expand All @@ -502,6 +505,7 @@ libc_enum! {
///
/// Used as an argument to `tcflow()`.
#[repr(i32)]
#[non_exhaustive]
pub enum FlowArg {
/// Suspend transmission
TCOOFF,
Expand All @@ -518,6 +522,7 @@ libc_enum! {
libc_enum! {
/// Indices into the `termios.c_cc` array for special characters.
#[repr(usize)]
#[non_exhaustive]
pub enum SpecialCharacterIndices {
VDISCARD,
#[cfg(any(target_os = "dragonfly",
Expand Down
1 change: 1 addition & 0 deletions src/sys/timerfd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ libc_enum! {
/// The type of the clock used to mark the progress of the timer. For more
/// details on each kind of clock, please refer to [timerfd_create(2)](https://man7.org/linux/man-pages/man2/timerfd_create.2.html).
#[repr(i32)]
#[non_exhaustive]
pub enum ClockId {
CLOCK_REALTIME,
CLOCK_MONOTONIC,
Expand Down
2 changes: 2 additions & 0 deletions src/unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1806,6 +1806,7 @@ pub fn mkstemp<P: ?Sized + NixPath>(template: &P) -> Result<(RawFd, PathBuf)> {
/// - [unistd.h](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/unistd.h.html)
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[repr(i32)]
#[non_exhaustive]
pub enum PathconfVar {
#[cfg(any(target_os = "dragonfly", target_os = "freebsd", target_os = "linux",
target_os = "netbsd", target_os = "openbsd", target_os = "redox"))]
Expand Down Expand Up @@ -1985,6 +1986,7 @@ pub fn pathconf<P: ?Sized + NixPath>(path: &P, var: PathconfVar) -> Result<Optio
/// - [limits.h](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/limits.h.html)
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[repr(i32)]
#[non_exhaustive]
pub enum SysconfVar {
/// Maximum number of I/O operations in a single list I/O call supported by
/// the implementation.
Expand Down

0 comments on commit 358adaf

Please sign in to comment.