Skip to content

Commit 358adaf

Browse files
committed
Mark most C-derived enums as non_exhaustive
Since libc may add new variants at any time, Nix's consumers should not use exhaustive match patterns. Fixes #1182
1 parent 7033d47 commit 358adaf

17 files changed

+43
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ This project adheres to [Semantic Versioning](https://semver.org/).
1414
- `FdSet::{contains, highest, fds}` no longer require a mutable reference.
1515
(#[1464](https://github.com/nix-rust/nix/pull/1464))
1616

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

1923
- Added more errno definitions for better backwards compatibility with

src/errno.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,7 @@ fn desc(errno: Errno) -> &'static str {
768768
mod consts {
769769
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
770770
#[repr(i32)]
771+
#[non_exhaustive]
771772
pub enum Errno {
772773
UnknownErrno = 0,
773774
EPERM = libc::EPERM,
@@ -1073,6 +1074,7 @@ mod consts {
10731074
mod consts {
10741075
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
10751076
#[repr(i32)]
1077+
#[non_exhaustive]
10761078
pub enum Errno {
10771079
UnknownErrno = 0,
10781080
EPERM = libc::EPERM,
@@ -1324,6 +1326,7 @@ mod consts {
13241326
mod consts {
13251327
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
13261328
#[repr(i32)]
1329+
#[non_exhaustive]
13271330
pub enum Errno {
13281331
UnknownErrno = 0,
13291332
EPERM = libc::EPERM,
@@ -1562,6 +1565,7 @@ mod consts {
15621565
mod consts {
15631566
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
15641567
#[repr(i32)]
1568+
#[non_exhaustive]
15651569
pub enum Errno {
15661570
UnknownErrno = 0,
15671571
EPERM = libc::EPERM,
@@ -1796,6 +1800,7 @@ mod consts {
17961800
mod consts {
17971801
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
17981802
#[repr(i32)]
1803+
#[non_exhaustive]
17991804
pub enum Errno {
18001805
UnknownErrno = 0,
18011806
EPERM = libc::EPERM,
@@ -2019,6 +2024,7 @@ mod consts {
20192024
mod consts {
20202025
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
20212026
#[repr(i32)]
2027+
#[non_exhaustive]
20222028
pub enum Errno {
20232029
UnknownErrno = 0,
20242030
EPERM = libc::EPERM,
@@ -2244,6 +2250,7 @@ mod consts {
22442250
mod consts {
22452251
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
22462252
#[repr(i32)]
2253+
#[non_exhaustive]
22472254
pub enum Errno {
22482255
UnknownErrno = 0,
22492256
EPERM = libc::EPERM,
@@ -2441,6 +2448,7 @@ mod consts {
24412448
mod consts {
24422449
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
24432450
#[repr(i32)]
2451+
#[non_exhaustive]
24442452
pub enum Errno {
24452453
UnknownErrno = 0,
24462454
EPERM = libc::EPERM,

src/fcntl.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@ libc_bitflags!(
374374

375375
#[cfg(not(target_os = "redox"))]
376376
#[derive(Debug, Eq, Hash, PartialEq)]
377+
#[non_exhaustive]
377378
pub enum FcntlArg<'a> {
378379
F_DUPFD(RawFd),
379380
F_DUPFD_CLOEXEC(RawFd),
@@ -405,6 +406,7 @@ pub enum FcntlArg<'a> {
405406

406407
#[cfg(target_os = "redox")]
407408
#[derive(Debug, Clone, Copy, Eq, Hash, PartialEq)]
409+
#[non_exhaustive]
408410
pub enum FcntlArg {
409411
F_DUPFD(RawFd),
410412
F_DUPFD_CLOEXEC(RawFd),
@@ -454,6 +456,7 @@ pub fn fcntl(fd: RawFd, arg: FcntlArg) -> Result<c_int> {
454456
}
455457

456458
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
459+
#[non_exhaustive]
457460
pub enum FlockArg {
458461
LockShared,
459462
LockExclusive,
@@ -649,6 +652,7 @@ mod posix_fadvise {
649652

650653
libc_enum! {
651654
#[repr(i32)]
655+
#[non_exhaustive]
652656
pub enum PosixFadviseAdvice {
653657
POSIX_FADV_NORMAL,
654658
POSIX_FADV_SEQUENTIAL,

src/sys/aio.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ libc_enum! {
3939
/// Mode for `AioCb::fsync`. Controls whether only data or both data and
4040
/// metadata are synced.
4141
#[repr(i32)]
42+
#[non_exhaustive]
4243
pub enum AioFsyncMode {
4344
/// do it like `fsync`
4445
O_SYNC,
@@ -57,6 +58,7 @@ libc_enum! {
5758
/// given `aiocb` should be used for a read operation, a write operation, or
5859
/// ignored. Has no effect for any other aio functions.
5960
#[repr(i32)]
61+
#[non_exhaustive]
6062
pub enum LioOpcode {
6163
LIO_NOP,
6264
LIO_WRITE,

src/sys/epoll.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ libc_bitflags!(
2929

3030
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
3131
#[repr(i32)]
32+
#[non_exhaustive]
3233
pub enum EpollOp {
3334
EpollCtlAdd = libc::EPOLL_CTL_ADD,
3435
EpollCtlDel = libc::EPOLL_CTL_DEL,

src/sys/event.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type type_of_event_filter = i16;
3636
libc_enum! {
3737
#[cfg_attr(target_os = "netbsd", repr(u32))]
3838
#[cfg_attr(not(target_os = "netbsd"), repr(i16))]
39+
#[non_exhaustive]
3940
pub enum EventFilter {
4041
EVFILT_AIO,
4142
/// Returns whenever there is no remaining data in the write buffer

src/sys/mman.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ libc_enum!{
155155
///
156156
/// Used by [`madvise`](./fn.madvise.html).
157157
#[repr(i32)]
158+
#[non_exhaustive]
158159
pub enum MmapAdvise {
159160
/// No further special treatment. This is the default.
160161
MADV_NORMAL,

src/sys/ptrace/bsd.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ cfg_if! {
2424
libc_enum! {
2525
#[repr(i32)]
2626
/// Ptrace Request enum defining the action to be taken.
27+
#[non_exhaustive]
2728
pub enum Request {
2829
PT_TRACE_ME,
2930
PT_READ_I,

src/sys/ptrace/linux.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ libc_enum!{
3333
#[cfg_attr(not(any(target_env = "musl", target_os = "android")), repr(u32))]
3434
#[cfg_attr(any(target_env = "musl", target_os = "android"), repr(i32))]
3535
/// Ptrace Request enum defining the action to be taken.
36+
#[non_exhaustive]
3637
pub enum Request {
3738
PTRACE_TRACEME,
3839
PTRACE_PEEKTEXT,
@@ -123,6 +124,7 @@ libc_enum!{
123124
/// Using the ptrace options the tracer can configure the tracee to stop
124125
/// at certain events. This enum is used to define those events as defined
125126
/// in `man ptrace`.
127+
#[non_exhaustive]
126128
pub enum Event {
127129
/// Event that stops before a return from fork or clone.
128130
PTRACE_EVENT_FORK,

src/sys/quota.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ libc_enum!{
4242
libc_enum!{
4343
/// The scope of the quota.
4444
#[repr(i32)]
45+
#[non_exhaustive]
4546
pub enum QuotaType {
4647
/// Specify a user quota
4748
USRQUOTA,
@@ -53,6 +54,7 @@ libc_enum!{
5354
libc_enum!{
5455
/// The type of quota format to use.
5556
#[repr(i32)]
57+
#[non_exhaustive]
5658
pub enum QuotaFmt {
5759
/// Use the original quota format.
5860
QFMT_VFS_OLD,

src/sys/reboot.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ libc_enum! {
1212
/// See [`set_cad_enabled()`](fn.set_cad_enabled.html) for
1313
/// enabling/disabling Ctrl-Alt-Delete.
1414
#[repr(i32)]
15+
#[non_exhaustive]
1516
pub enum RebootMode {
1617
RB_HALT_SYSTEM,
1718
RB_KEXEC,

src/sys/signal.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ libc_enum!{
2323
// We would prefer to use the libc::c_int alias in the repr attribute. Unfortunately
2424
// this is not (yet) possible.
2525
#[repr(i32)]
26+
#[non_exhaustive]
2627
pub enum Signal {
2728
SIGHUP,
2829
SIGINT,
@@ -396,6 +397,7 @@ libc_bitflags!{
396397

397398
libc_enum! {
398399
#[repr(i32)]
400+
#[non_exhaustive]
399401
pub enum SigmaskHow {
400402
SIG_BLOCK,
401403
SIG_UNBLOCK,

src/sys/socket/addr.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub use self::vsock::VsockAddr;
3232
/// These constants specify the protocol family to be used
3333
/// in [`socket`](fn.socket.html) and [`socketpair`](fn.socketpair.html)
3434
#[repr(i32)]
35+
#[non_exhaustive]
3536
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
3637
pub enum AddressFamily {
3738
/// Local communication (see [`unix(7)`](https://man7.org/linux/man-pages/man7/unix.7.html))
@@ -641,6 +642,7 @@ impl Hash for UnixAddr {
641642

642643
/// Represents a socket address
643644
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
645+
#[non_exhaustive]
644646
pub enum SockAddr {
645647
Inet(InetAddr),
646648
Unix(UnixAddr),

src/sys/socket/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ pub use libc::{c_uint, CMSG_SPACE};
7070
/// when creating a socket with [`socket()`](fn.socket.html)
7171
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
7272
#[repr(i32)]
73+
#[non_exhaustive]
7374
pub enum SockType {
7475
/// Provides sequenced, reliable, two-way, connection-
7576
/// based byte streams. An out-of-band data transmission
@@ -94,6 +95,7 @@ pub enum SockType {
9495
/// to specify the protocol to use.
9596
#[repr(i32)]
9697
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
98+
#[non_exhaustive]
9799
pub enum SockProtocol {
98100
/// TCP protocol ([ip(7)](https://man7.org/linux/man-pages/man7/ip.7.html))
99101
Tcp = libc::IPPROTO_TCP,
@@ -490,6 +492,7 @@ impl<'a> Iterator for CmsgIterator<'a> {
490492
//
491493
// See https://github.com/nix-rust/nix/issues/999
492494
#[derive(Clone, Debug, Eq, PartialEq)]
495+
#[non_exhaustive]
493496
pub enum ControlMessageOwned {
494497
/// Received version of
495498
/// [`ControlMessage::ScmRights`][#enum.ControlMessage.html#variant.ScmRights]
@@ -739,6 +742,7 @@ impl ControlMessageOwned {
739742
///
740743
/// [Further reading](https://man7.org/linux/man-pages/man3/cmsg.3.html)
741744
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
745+
#[non_exhaustive]
742746
pub enum ControlMessage<'a> {
743747
/// A message of type `SCM_RIGHTS`, containing an array of file
744748
/// descriptors passed between processes.

src/sys/termios.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ libc_enum!{
256256
/// B0 is special and will disable the port.
257257
#[cfg_attr(all(any(target_os = "ios", target_os = "macos"), target_pointer_width = "64"), repr(u64))]
258258
#[cfg_attr(not(all(any(target_os = "ios", target_os = "macos"), target_pointer_width = "64")), repr(u32))]
259+
#[non_exhaustive]
259260
pub enum BaudRate {
260261
B0,
261262
B50,
@@ -472,6 +473,7 @@ libc_enum! {
472473
///
473474
/// Used as an argument to `tcsetattr()`
474475
#[repr(i32)]
476+
#[non_exhaustive]
475477
pub enum SetArg {
476478
/// The change will occur immediately
477479
TCSANOW,
@@ -487,6 +489,7 @@ libc_enum! {
487489
///
488490
/// Used as an argument to `tcflush()`.
489491
#[repr(i32)]
492+
#[non_exhaustive]
490493
pub enum FlushArg {
491494
/// Flush data that was received but not read
492495
TCIFLUSH,
@@ -502,6 +505,7 @@ libc_enum! {
502505
///
503506
/// Used as an argument to `tcflow()`.
504507
#[repr(i32)]
508+
#[non_exhaustive]
505509
pub enum FlowArg {
506510
/// Suspend transmission
507511
TCOOFF,
@@ -518,6 +522,7 @@ libc_enum! {
518522
libc_enum! {
519523
/// Indices into the `termios.c_cc` array for special characters.
520524
#[repr(usize)]
525+
#[non_exhaustive]
521526
pub enum SpecialCharacterIndices {
522527
VDISCARD,
523528
#[cfg(any(target_os = "dragonfly",

src/sys/timerfd.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ libc_enum! {
5858
/// The type of the clock used to mark the progress of the timer. For more
5959
/// details on each kind of clock, please refer to [timerfd_create(2)](https://man7.org/linux/man-pages/man2/timerfd_create.2.html).
6060
#[repr(i32)]
61+
#[non_exhaustive]
6162
pub enum ClockId {
6263
CLOCK_REALTIME,
6364
CLOCK_MONOTONIC,

src/unistd.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1806,6 +1806,7 @@ pub fn mkstemp<P: ?Sized + NixPath>(template: &P) -> Result<(RawFd, PathBuf)> {
18061806
/// - [unistd.h](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/unistd.h.html)
18071807
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
18081808
#[repr(i32)]
1809+
#[non_exhaustive]
18091810
pub enum PathconfVar {
18101811
#[cfg(any(target_os = "dragonfly", target_os = "freebsd", target_os = "linux",
18111812
target_os = "netbsd", target_os = "openbsd", target_os = "redox"))]
@@ -1985,6 +1986,7 @@ pub fn pathconf<P: ?Sized + NixPath>(path: &P, var: PathconfVar) -> Result<Optio
19851986
/// - [limits.h](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/limits.h.html)
19861987
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
19871988
#[repr(i32)]
1989+
#[non_exhaustive]
19881990
pub enum SysconfVar {
19891991
/// Maximum number of I/O operations in a single list I/O call supported by
19901992
/// the implementation.

0 commit comments

Comments
 (0)