Skip to content

Commit 544ec49

Browse files
committed
Shifting the unsafe responsibility a bit
1 parent ca15a59 commit 544ec49

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

src/convert/slices.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,17 +128,14 @@ vectors! {
128128
cfg_if! {
129129
if #[cfg(feature = "enable-interning")] {
130130
#[inline]
131-
fn get_cached_str(x: &str) -> Option<WasmSlice> {
132-
// This is safe because the JsValue is immediately looked up in the heap and
133-
// then returned, so use-after-free cannot occur.
134-
//
131+
fn unsafe_get_cached_str(x: &str) -> Option<WasmSlice> {
135132
// This uses 0 for the ptr as an indication that it is a JsValue and not a str.
136133
crate::cache::intern::unsafe_get_str(x).map(|x| WasmSlice { ptr: 0, len: x })
137134
}
138135

139136
} else {
140137
#[inline]
141-
fn get_cached_str(_x: &str) -> Option<WasmSlice> {
138+
fn unsafe_get_cached_str(_x: &str) -> Option<WasmSlice> {
142139
None
143140
}
144141
}
@@ -175,7 +172,9 @@ if_std! {
175172

176173
#[inline]
177174
fn into_abi(self) -> Self::Abi {
178-
get_cached_str(&self).unwrap_or_else(|| self.into_bytes().into_abi())
175+
// This is safe because the JsValue is immediately looked up in the heap and
176+
// then returned, so use-after-free cannot occur.
177+
unsafe_get_cached_str(&self).unwrap_or_else(|| self.into_bytes().into_abi())
179178
}
180179
}
181180

@@ -202,7 +201,9 @@ impl<'a> IntoWasmAbi for &'a str {
202201

203202
#[inline]
204203
fn into_abi(self) -> Self::Abi {
205-
get_cached_str(self).unwrap_or_else(|| self.as_bytes().into_abi())
204+
// This is safe because the JsValue is immediately looked up in the heap and
205+
// then returned, so use-after-free cannot occur.
206+
unsafe_get_cached_str(self).unwrap_or_else(|| self.as_bytes().into_abi())
206207
}
207208
}
208209

0 commit comments

Comments
 (0)