Skip to content

Commit ef7ae73

Browse files
committed
Fix dirent to match WASI libc's definition.
dirent contains a flexible array member, so don't test its sizeof, don't allow it to be copied, and don't represent it with an artificial size.
1 parent a625c69 commit ef7ae73

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

libc-test/build.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1931,5 +1931,9 @@ fn test_wasi(target: &str) {
19311931
// import the same thing but have different function pointers
19321932
cfg.skip_fn_ptrcheck(|f| f.starts_with("__wasi"));
19331933

1934+
// d_name is declared as a flexible array in WASI libc, so it
1935+
// doesn't support sizeof.
1936+
cfg.skip_field(|s, field| s == "dirent" && field == "d_name");
1937+
19341938
cfg.generate("../src/lib.rs", "main.rs");
19351939
}

src/wasi.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,6 @@ s! {
141141
fds_bits: [c_ulong; FD_SETSIZE / ULONG_SIZE],
142142
}
143143

144-
pub struct dirent {
145-
pub d_ino: ino_t,
146-
pub d_type: c_uchar,
147-
pub d_name: [c_char; 1024],
148-
}
149-
150144
pub struct lconv {
151145
pub decimal_point: *mut c_char,
152146
pub thousands_sep: *mut c_char,
@@ -303,6 +297,20 @@ s_no_extra_traits! {
303297

304298
}
305299

300+
// Declare dirent outside of s! so that it doesn't implement Copy, Eq, Hash,
301+
// etc., since it contains a flexible array member with a dynamic size.
302+
#[repr(C)]
303+
#[allow(missing_copy_implementations)]
304+
#[cfg_attr(feature = "extra_traits", derive(Debug))]
305+
pub struct dirent {
306+
pub d_ino: ino_t,
307+
pub d_type: c_uchar,
308+
/// d_name is declared in WASI libc as a flexible array member, which
309+
/// can't be directly expressed in Rust. As an imperfect workaround,
310+
/// declare it as a zero-length array instead.
311+
pub d_name: [c_char; 0],
312+
}
313+
306314
// intentionally not public, only used for fd_set
307315
cfg_if! {
308316
if #[cfg(target_pointer_width = "32")] {

0 commit comments

Comments
 (0)