Skip to content

std.fs.Dir.Iterator cannot handle long file names on Windows #8268

Closed

Description

@jack1243star

I triggered an assertion failure when iterating a long file name with std.fs.walkPath:

D:\application\zig-windows-x86_64-0.8.0-dev.1431+c760532be\lib\zig\std\debug.zig:223:14: 0x7ff667831078 in std.debug.assert (walk.obj)
    if (!ok) unreachable; // assertion failure
             ^
D:\application\zig-windows-x86_64-0.8.0-dev.1431+c760532be\lib\zig\std\unicode.zig:43:11: 0x7ff66785762e in std.unicode.utf8Encode (walk.obj)
    assert(out.len >= length);
          ^
D:\application\zig-windows-x86_64-0.8.0-dev.1431+c760532be\lib\zig\std\unicode.zig:604:36: 0x7ff66784f9d5 in std.unicode.utf16leToUtf8 (walk.obj)
        end_index += try utf8Encode(codepoint, utf8[end_index..]);
                                   ^
D:\application\zig-windows-x86_64-0.8.0-dev.1431+c760532be\lib\zig\std\fs.zig:615:68: 0x7ff66784f4c5 in std.fs.Iterator::std.fs.Iterator.next (walk.obj)
                    const name_utf8_len = std.unicode.utf16leToUtf8(self.name_data[0..], name_utf16le) catch unreachable;
                                                                   ^
D:\application\zig-windows-x86_64-0.8.0-dev.1431+c760532be\lib\zig\std\fs.zig:2266:36: 0x7ff66784c0f9 in std.fs.Walker::std.fs.Walker.next (walk.obj)
            if (try top.dir_it.next()) |base| {
                                   ^

It seems that std.fs.Dir.Iterator on Windows wrongly assumes that the filename converted into UTF-8 will fit in 256 bytes.

name_data: [256]u8,

Changing it to 512 fixed my particular use case.

I don't know much about this, perhaps assuming 256 Unicode code points would be a safer bet?

Activity

  1. added
    bug
    standard library
    os-windows
    on Mar 16, 2021
  2. added this to the 0.8.0 milestone on Mar 16, 2021
  3. LemonBoy commented on Mar 16, 2021

    @LemonBoy
    Contributor

    What's the FileNameLength value for the failing directory entry?
    I guess this is the usual problem with long path names introduced by Windows 10 and PATH_MAX being hardcoded to 260.

  4. jack1243star commented on Mar 16, 2021

    @jack1243star
    Author

    What's the FileNameLength value for the failing directory entry?

    One entry has FileNameLength == 416, which required setting name_data: [1024]u8 to pass.

    Creating a file with random long Japanese file name can reliably trigger this.
    I guess this is partially due to CJK characters being encoded as three bytes in UTF-8?

  5. LemonBoy commented on Mar 16, 2021

    @LemonBoy
    Contributor

    Oh, good catch. The longest UCS-2 codepoint is utf8-encoded as a sequence that's 43 bytes long so 260*43 is a good upper limit.

  6. modified the milestones: 0.8.0, 0.8.1 on Jun 4, 2021
  7. modified the milestones: 0.8.1, 0.9.1 on Aug 31, 2021
  8. modified the milestones: 0.9.1, 0.9.0, 0.10.0 on Nov 20, 2021
  9. modified the milestones: 0.10.0, 0.11.0 on Apr 16, 2022
  10. added a commit that references this issue on Oct 14, 2022
    f2e1d64
  11. added a commit that references this issue on Oct 29, 2022
    c6ff1a7
  12. modified the milestones: 0.11.0, 0.10.0 on Oct 30, 2022
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

    bugos-windowsstandard library

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions