Very crude attempt at validating a path. This is imperfect and AFAIK it is effectively impossible to implement perfect path validation, since it ultimately depends on the underlying filesystem. Note that this function won't be necessary if/when https://github.com/ziglang/zig/issues/15607 is accepted/implemented.
fn validateSearchPath(path: []const u8) error
fn validateSearchPath(path: []const u8) error{BadPathName}!void {
switch (builtin.os.tag) {
.windows => {
// This will return error.BadPathName on non-Win32 namespaced paths
// (e.g. the NT \??\ prefix, the device \\.\ prefix, etc).
// Those path types are something of an unavoidable way to
// still hit unreachable during the openDir call.
var component_iterator = std.fs.path.componentIterator(path);
while (component_iterator.next()) |component| {
// https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file
if (std.mem.indexOfAny(u8, component.name, "\x00<>:\"|?*") != null) return error.BadPathName;
}
},
else => {
if (std.mem.indexOfScalar(u8, path, 0) != null) return error.BadPathName;
},
}
}