about summary refs log tree commit diff
path: root/library/std/src/sys
diff options
context:
space:
mode:
authorYuki Okushi <huyuumi.dev@gmail.com>2021-03-14 13:07:33 +0900
committerGitHub <noreply@github.com>2021-03-14 13:07:33 +0900
commit9ce0820eefaa6dbfeadbca75f99063ae668cb30d (patch)
tree759700bd3a4f09c6b3a4640319721f56cbb0bf35 /library/std/src/sys
parent33614023a186133841322ac9bae70a6123f853f0 (diff)
parentd6b06b8a327ff32d083bc0494cc3195d9f8082d2 (diff)
downloadrust-9ce0820eefaa6dbfeadbca75f99063ae668cb30d.tar.gz
rust-9ce0820eefaa6dbfeadbca75f99063ae668cb30d.zip
Rollup merge of #82804 - alexcrichton:fix-wasi, r=pnkfelix
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.
Diffstat (limited to 'library/std/src/sys')
-rw-r--r--library/std/src/sys/wasi/fs.rs6
1 files changed, 2 insertions, 4 deletions
diff --git a/library/std/src/sys/wasi/fs.rs b/library/std/src/sys/wasi/fs.rs
index bcf7da46b4b..63c22136273 100644
--- a/library/std/src/sys/wasi/fs.rs
+++ b/library/std/src/sys/wasi/fs.rs
@@ -650,13 +650,11 @@ fn open_parent(p: &Path) -> io::Result<(ManuallyDrop<WasiFd>, PathBuf)> {
                 );
                 return Err(io::Error::new(io::ErrorKind::Other, msg));
             }
-            let len = CStr::from_ptr(buf.as_ptr().cast()).to_bytes().len();
-            buf.set_len(len);
-            buf.shrink_to_fit();
+            let relative = CStr::from_ptr(relative_path).to_bytes().to_vec();
 
             return Ok((
                 ManuallyDrop::new(WasiFd::from_raw(fd as u32)),
-                PathBuf::from(OsString::from_vec(buf)),
+                PathBuf::from(OsString::from_vec(relative)),
             ));
         }
     }