Skip to content

fs::read_dir() fails with error 0 on Linux if directory is on NTFS and contains file with filename > 255 bytes #86649

Closed

Description

@ariasuni
  1. Create a filename on an NTFS partition with a name > 255 bytes, e.g. 一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十.
  2. Run this code with the path to the directory containing the file with the very long filename:
use std::env;
use std::fs::{self, ReadDir};

fn main() {
    test().unwrap();
}

fn test() -> std::io::Result<ReadDir> {
    let args: Vec<String> = env::args().collect();
    for result in fs::read_dir(&args[1])? {
        result.map(|entry| entry.path()).unwrap();
    }
    todo!()
}

I expected to see this happen: the code works until it panics on todo!()

Instead, this happened: thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Os { code: 0, kind: Other, message: "Success" }', test.rs:11:42. Note that in exa, it fails with Bad address (os error 14).

Meta

rustc --version --verbose:

rustc 1.53.0 (53cb7b09b 2021-06-17)
binary: rustc
commit-hash: 53cb7b09b00cbea8754ffb78e7e3cb521cb8af4b
commit-date: 2021-06-17
host: x86_64-unknown-linux-gnu
release: 1.53.0
LLVM version: 12.0.1

Activity

  1. added
    O-linuxOperating system: Linux
    T-libsRelevant to the library team, which will review and decide on the PR/issue.
    on Jun 26, 2021
  2. ChrisDenton commented on Jun 26, 2021

    @ChrisDenton
    Member

    So NTFS has a limit of 255 UTF-16 code units. However, something here is interpreting that as being a UTF-8 byte limit of 255.

    I think this error originates outside of Rust? If libc::opendir is returning a null pointer then I'm not sure what Rust can do?

  3. RalfJung commented on Jul 3, 2021

    @RalfJung
    Member

    It does not mention that it'll set errno, so I think Error::last_os_error() might not return the correct error code.

    Ah, I was already wondering why the error message says Success. 😂

  4. added 2 commits that reference this issue on Sep 1, 2021
  5. tavianator commented on Sep 1, 2021

    @tavianator
    Contributor

    So this should no longer say "success". It should say something like "file name too long" instead now. A complete fix is just replacing readdir_r() with readdir() on LInux, which I'll do unless someone beats me to it. See #40021 for some historical details.

  6. added a commit that references this issue on Jan 11, 2022
    ea9181b
  7. added a commit that references this issue on Jan 11, 2022
    efcc300
  8. added a commit that references this issue on Jan 11, 2022
    4839b81
  9. added a commit that references this issue on Jan 14, 2022
    749b557
  10. added a commit that references this issue on Jan 26, 2022
    253f64c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.O-linuxOperating system: LinuxT-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions