libc: Wrong structs dirent, dirent64
Your structs dirent, dirent64 are wrong at x86_64-unknown-linux-gnu. Their field d_name contains 256 bytes. But names can be bigger than 256 bytes in NTFS (in Linux). I was able to create NTFS partition in my Linux and then create file with name “ыыыы…” (250 times “ы”, this is Cyrillic letter). Such name contains more than 256 bytes (if you have difficulties with reproducing, I can provide more details).
So, fix structs. I am not sure how exactly fix should be implemented. Maybe [c_char; 512], but in this case we need to double check what is true upper limit. Maybe we can simply put [c_char; 1] or [c_char; 0]. Of course, it would be perfect to make dirent unsized, but with machine word sized reference. Unfortunately, as well as I know, such unsized structs are not possible in stable Rust
About this issue
- Original URL
- State: open
- Created 5 years ago
- Comments: 22 (9 by maintainers)
I don’t have all the context to answer this question definitively, but assuming that
readdiractually returned a contiguous allocation with a null-terminatedd_namefield of longer than 256 bytes, your code as written is suspicious and possibly UB (whether it is actually UB is rust-lang/unsafe-code-guidelines#134) due to creating a reference at(*ent).d_name.as_ptr(), but you can rewrite it to be definitely not UB by usingaddr_of!((*ent).d_name)instead.