Skip to content

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

@jack1243star

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behavioros-windowsMicrosoft Windowsstandard libraryThis issue involves writing Zig code for the standard library.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions