Skip to content

Commit b9438e0

Browse files
alexcrichtonMark-Simulacrum
authored andcommitted
std: Fix a bug on the wasm32-wasi target opening files
This commit fixes an issue pointed out in #82758 where LTO changed the behavior of a program. It turns out that LTO was not at fault here, it simply uncovered an existing bug. The bindings to `__wasilibc_find_relpath` assumed that the relative portion of the path returned was always contained within thee input `buf` we passed in. This isn't actually the case, however, and sometimes the relative portion of the path may reference a sub-portion of the input string itself. The fix here is to use the relative path pointer coming out of `__wasilibc_find_relpath` as the source of truth. The `buf` used for local storage is discarded in this function and the relative path is copied out unconditionally. We might be able to get away with some `Cow`-like business or such to avoid the extra allocation, but for now this is probably the easiest patch to fix the original issue.
1 parent 3313a14 commit b9438e0

File tree

1 file changed

+2
-4
lines changed
  • library/std/src/sys/wasi

1 file changed

+2
-4
lines changed

library/std/src/sys/wasi/fs.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -654,13 +654,11 @@ fn open_parent(p: &Path) -> io::Result<(ManuallyDrop<WasiFd>, PathBuf)> {
654654
);
655655
return Err(io::Error::new(io::ErrorKind::Other, msg));
656656
}
657-
let len = CStr::from_ptr(buf.as_ptr().cast()).to_bytes().len();
658-
buf.set_len(len);
659-
buf.shrink_to_fit();
657+
let relative = CStr::from_ptr(relative_path).to_bytes().to_vec();
660658

661659
return Ok((
662660
ManuallyDrop::new(WasiFd::from_raw(fd as u32)),
663-
PathBuf::from(OsString::from_vec(buf)),
661+
PathBuf::from(OsString::from_vec(relative)),
664662
));
665663
}
666664
}

0 commit comments

Comments
 (0)