Skip to content

Commit e317b7a

Browse files
committed
libc: wrap padding fields with Padding newtype.
1 parent cf82fdf commit e317b7a

File tree

83 files changed

+743
-725
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+743
-725
lines changed

src/fuchsia/aarch64.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@ s! {
1515
pub st_uid: crate::uid_t,
1616
pub st_gid: crate::gid_t,
1717
pub st_rdev: crate::dev_t,
18-
__pad0: c_ulong,
18+
__pad0: Padding<c_ulong>,
1919
pub st_size: off_t,
2020
pub st_blksize: crate::blksize_t,
21-
__pad1: c_int,
21+
__pad1: Padding<c_int>,
2222
pub st_blocks: crate::blkcnt_t,
2323
pub st_atime: crate::time_t,
2424
pub st_atime_nsec: c_long,
2525
pub st_mtime: crate::time_t,
2626
pub st_mtime_nsec: c_long,
2727
pub st_ctime: crate::time_t,
2828
pub st_ctime_nsec: c_long,
29-
__unused: [c_uint; 2],
29+
__unused: Padding<[c_uint; 2]>,
3030
}
3131

3232
pub struct stat64 {
@@ -37,18 +37,18 @@ s! {
3737
pub st_uid: crate::uid_t,
3838
pub st_gid: crate::gid_t,
3939
pub st_rdev: crate::dev_t,
40-
__pad0: c_ulong,
40+
__pad0: Padding<c_ulong>,
4141
pub st_size: off_t,
4242
pub st_blksize: crate::blksize_t,
43-
__pad1: c_int,
43+
__pad1: Padding<c_int>,
4444
pub st_blocks: crate::blkcnt_t,
4545
pub st_atime: crate::time_t,
4646
pub st_atime_nsec: c_long,
4747
pub st_mtime: crate::time_t,
4848
pub st_mtime_nsec: c_long,
4949
pub st_ctime: crate::time_t,
5050
pub st_ctime_nsec: c_long,
51-
__unused: [c_uint; 2],
51+
__unused: Padding<[c_uint; 2]>,
5252
}
5353

5454
pub struct ipc_perm {
@@ -59,8 +59,8 @@ s! {
5959
pub cgid: crate::gid_t,
6060
pub mode: crate::mode_t,
6161
pub __seq: c_ushort,
62-
__unused1: c_ulong,
63-
__unused2: c_ulong,
62+
__unused1: Padding<c_ulong>,
63+
__unused2: Padding<c_ulong>,
6464
}
6565
}
6666

src/fuchsia/mod.rs

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -143,46 +143,46 @@ s! {
143143
pub ru_stime: timeval,
144144
pub ru_maxrss: c_long,
145145
#[cfg(all(target_arch = "x86_64", target_pointer_width = "32"))]
146-
__pad1: u32,
146+
__pad1: Padding<u32>,
147147
pub ru_ixrss: c_long,
148148
#[cfg(all(target_arch = "x86_64", target_pointer_width = "32"))]
149-
__pad2: u32,
149+
__pad2: Padding<u32>,
150150
pub ru_idrss: c_long,
151151
#[cfg(all(target_arch = "x86_64", target_pointer_width = "32"))]
152-
__pad3: u32,
152+
__pad3: Padding<u32>,
153153
pub ru_isrss: c_long,
154154
#[cfg(all(target_arch = "x86_64", target_pointer_width = "32"))]
155-
__pad4: u32,
155+
__pad4: Padding<u32>,
156156
pub ru_minflt: c_long,
157157
#[cfg(all(target_arch = "x86_64", target_pointer_width = "32"))]
158-
__pad5: u32,
158+
__pad5: Padding<u32>,
159159
pub ru_majflt: c_long,
160160
#[cfg(all(target_arch = "x86_64", target_pointer_width = "32"))]
161-
__pad6: u32,
161+
__pad6: Padding<u32>,
162162
pub ru_nswap: c_long,
163163
#[cfg(all(target_arch = "x86_64", target_pointer_width = "32"))]
164-
__pad7: u32,
164+
__pad7: Padding<u32>,
165165
pub ru_inblock: c_long,
166166
#[cfg(all(target_arch = "x86_64", target_pointer_width = "32"))]
167-
__pad8: u32,
167+
__pad8: Padding<u32>,
168168
pub ru_oublock: c_long,
169169
#[cfg(all(target_arch = "x86_64", target_pointer_width = "32"))]
170-
__pad9: u32,
170+
__pad9: Padding<u32>,
171171
pub ru_msgsnd: c_long,
172172
#[cfg(all(target_arch = "x86_64", target_pointer_width = "32"))]
173-
__pad10: u32,
173+
__pad10: Padding<u32>,
174174
pub ru_msgrcv: c_long,
175175
#[cfg(all(target_arch = "x86_64", target_pointer_width = "32"))]
176-
__pad11: u32,
176+
__pad11: Padding<u32>,
177177
pub ru_nsignals: c_long,
178178
#[cfg(all(target_arch = "x86_64", target_pointer_width = "32"))]
179-
__pad12: u32,
179+
__pad12: Padding<u32>,
180180
pub ru_nvcsw: c_long,
181181
#[cfg(all(target_arch = "x86_64", target_pointer_width = "32"))]
182-
__pad13: u32,
182+
__pad13: Padding<u32>,
183183
pub ru_nivcsw: c_long,
184184
#[cfg(all(target_arch = "x86_64", target_pointer_width = "32"))]
185-
__pad14: u32,
185+
__pad14: Padding<u32>,
186186
}
187187

188188
pub struct in_addr {
@@ -454,11 +454,11 @@ s! {
454454
pub gl_offs: size_t,
455455
pub gl_flags: c_int,
456456

457-
__unused1: *mut c_void,
458-
__unused2: *mut c_void,
459-
__unused3: *mut c_void,
460-
__unused4: *mut c_void,
461-
__unused5: *mut c_void,
457+
__unused1: Padding<*mut c_void>,
458+
__unused2: Padding<*mut c_void>,
459+
__unused3: Padding<*mut c_void>,
460+
__unused4: Padding<*mut c_void>,
461+
__unused5: Padding<*mut c_void>,
462462
}
463463

464464
pub struct ifaddrs {
@@ -543,7 +543,7 @@ s! {
543543
pub ssi_stime: u64,
544544
pub ssi_addr: u64,
545545
pub ssi_addr_lsb: u16,
546-
_pad2: u16,
546+
_pad2: Padding<u16>,
547547
pub ssi_syscall: i32,
548548
pub ssi_call_addr: u64,
549549
pub ssi_arch: u32,
@@ -800,8 +800,8 @@ s! {
800800
pub shm_cpid: crate::pid_t,
801801
pub shm_lpid: crate::pid_t,
802802
pub shm_nattch: c_ulong,
803-
__pad1: c_ulong,
804-
__pad2: c_ulong,
803+
__pad1: Padding<c_ulong>,
804+
__pad2: Padding<c_ulong>,
805805
}
806806

807807
pub struct msqid_ds {
@@ -814,8 +814,8 @@ s! {
814814
pub msg_qbytes: crate::msglen_t,
815815
pub msg_lspid: crate::pid_t,
816816
pub msg_lrpid: crate::pid_t,
817-
__pad1: c_ulong,
818-
__pad2: c_ulong,
817+
__pad1: Padding<c_ulong>,
818+
__pad2: Padding<c_ulong>,
819819
}
820820

821821
pub struct statfs {
@@ -838,10 +838,10 @@ s! {
838838
pub msg_namelen: crate::socklen_t,
839839
pub msg_iov: *mut crate::iovec,
840840
pub msg_iovlen: c_int,
841-
__pad1: c_int,
841+
__pad1: Padding<c_int>,
842842
pub msg_control: *mut c_void,
843843
pub msg_controllen: crate::socklen_t,
844-
__pad2: crate::socklen_t,
844+
__pad2: Padding<crate::socklen_t>,
845845
pub msg_flags: c_int,
846846
}
847847

@@ -929,7 +929,7 @@ s_no_extra_traits! {
929929

930930
pub struct sockaddr_storage {
931931
pub ss_family: sa_family_t,
932-
__ss_pad2: [u8; 128 - 2 - 8],
932+
__ss_pad2: Padding<[u8; 128 - 2 - 8]>,
933933
__ss_align: size_t,
934934
}
935935

@@ -986,7 +986,7 @@ s_no_extra_traits! {
986986

987987
pub struct sockaddr_nl {
988988
pub nl_family: crate::sa_family_t,
989-
nl_pad: c_ushort,
989+
nl_pad: Padding<c_ushort>,
990990
pub nl_pid: u32,
991991
pub nl_groups: u32,
992992
}

src/fuchsia/riscv64.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ s! {
1717
pub st_mode: crate::mode_t,
1818
pub st_uid: crate::uid_t,
1919
pub st_gid: crate::gid_t,
20-
__pad0: c_int,
20+
__pad0: Padding<c_int>,
2121
pub st_rdev: crate::dev_t,
2222
pub st_size: off_t,
2323
pub st_blksize: crate::blksize_t,
@@ -28,7 +28,7 @@ s! {
2828
pub st_mtime_nsec: c_long,
2929
pub st_ctime: crate::time_t,
3030
pub st_ctime_nsec: c_long,
31-
__unused: [c_long; 3],
31+
__unused: Padding<[c_long; 3]>,
3232
}
3333

3434
// Not actually used, IPC calls just return ENOSYS
@@ -40,7 +40,7 @@ s! {
4040
pub cgid: crate::gid_t,
4141
pub mode: crate::mode_t,
4242
pub __seq: c_ushort,
43-
__unused1: c_ulong,
44-
__unused2: c_ulong,
43+
__unused1: Padding<c_ulong>,
44+
__unused2: Padding<c_ulong>,
4545
}
4646
}

src/fuchsia/x86_64.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ s! {
1414
pub st_mode: crate::mode_t,
1515
pub st_uid: crate::uid_t,
1616
pub st_gid: crate::gid_t,
17-
__pad0: c_int,
17+
__pad0: Padding<c_int>,
1818
pub st_rdev: crate::dev_t,
1919
pub st_size: off_t,
2020
pub st_blksize: crate::blksize_t,
@@ -25,7 +25,7 @@ s! {
2525
pub st_mtime_nsec: c_long,
2626
pub st_ctime: crate::time_t,
2727
pub st_ctime_nsec: c_long,
28-
__unused: [c_long; 3],
28+
__unused: Padding<[c_long; 3]>,
2929
}
3030

3131
pub struct stat64 {
@@ -35,7 +35,7 @@ s! {
3535
pub st_mode: crate::mode_t,
3636
pub st_uid: crate::uid_t,
3737
pub st_gid: crate::gid_t,
38-
__pad0: c_int,
38+
__pad0: Padding<c_int>,
3939
pub st_rdev: crate::dev_t,
4040
pub st_size: off_t,
4141
pub st_blksize: crate::blksize_t,
@@ -46,7 +46,7 @@ s! {
4646
pub st_mtime_nsec: c_long,
4747
pub st_ctime: crate::time_t,
4848
pub st_ctime_nsec: c_long,
49-
__reserved: [c_long; 3],
49+
__reserved: Padding<[c_long; 3]>,
5050
}
5151

5252
pub struct mcontext_t {
@@ -61,8 +61,8 @@ s! {
6161
pub cgid: crate::gid_t,
6262
pub mode: crate::mode_t,
6363
pub __seq: c_int,
64-
__unused1: c_long,
65-
__unused2: c_long,
64+
__unused1: Padding<c_long>,
65+
__unused2: Padding<c_long>,
6666
}
6767
}
6868

src/hermit.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ s! {
8787
pub struct sockaddr_storage {
8888
pub ss_len: u8,
8989
pub ss_family: sa_family_t,
90-
__ss_pad1: [u8; 6],
90+
__ss_pad1: Padding<[u8; 6]>,
9191
__ss_align: i64,
92-
__ss_pad2: [u8; 112],
92+
__ss_pad2: Padding<[u8; 112]>,
9393
}
9494

9595
pub struct stat {

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ pub use core::ffi::c_void;
4343
#[allow(unused_imports)] // needed while the module is empty on some platforms
4444
pub use new::*;
4545

46+
mod types;
47+
pub use types::*;
48+
4649
cfg_if! {
4750
if #[cfg(windows)] {
4851
mod primitives;

src/macros.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ macro_rules! prelude {
8686
c_char, c_double, c_float, c_int, c_long, c_longlong, c_short, c_uchar, c_uint,
8787
c_ulong, c_ulonglong, c_ushort, c_void, intptr_t, size_t, ssize_t, uintptr_t,
8888
};
89+
90+
pub(crate) use crate::Padding;
8991
}
9092
};
9193
}

src/new/linux_uapi/linux/can.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ s! {
4646
pub can_id: canid_t,
4747
// FIXME(1.0): this field was renamed to `len` in Linux 5.11
4848
pub can_dlc: u8,
49-
__pad: u8,
49+
__pad: Padding<u8>,
5050
__res0: u8,
5151
pub len8_dlc: u8,
5252
pub data: [u8; CAN_MAX_DLEN],

src/types.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use core::mem::MaybeUninit;
2+
3+
/// A transparent wrapper over `MaybeUninit<T>` to represent uninitialized padding
4+
/// while providing `Default`.
5+
#[repr(transparent)]
6+
#[derive(Clone, Copy)]
7+
pub struct Padding<T: Copy>(MaybeUninit<T>);
8+
9+
impl<T: Copy> Default for Padding<T> {
10+
fn default() -> Self {
11+
Self(MaybeUninit::zeroed())
12+
}
13+
}

src/unix/aix/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,9 @@ s! {
247247
pub struct sockaddr_storage {
248248
pub __ss_len: c_uchar,
249249
pub ss_family: sa_family_t,
250-
__ss_pad1: [c_char; 6],
250+
__ss_pad1: Padding<[c_char; 6]>,
251251
__ss_align: crate::int64_t,
252-
__ss_pad2: [c_char; 1265],
252+
__ss_pad2: Padding<[c_char; 1265]>,
253253
}
254254

255255
pub struct sockaddr_un {

0 commit comments

Comments
 (0)