Skip to content

Commit 2405fad

Browse files
committed
Shifting the unsafety guarantees around
1 parent c3676bc commit 2405fad

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

src/cache/intern.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ cfg_if! {
88
use std::borrow::ToOwned;
99
use std::cell::RefCell;
1010
use crate::JsValue;
11+
use crate::convert::IntoWasmAbi;
1112
use uluru::{LRUCache, Entry};
1213

1314

@@ -33,13 +34,14 @@ cfg_if! {
3334
cache.find(|p| p.key == key).map(|x| &x.value)
3435
}
3536

36-
pub(crate) fn get_str(s: &str) -> Option<JsValue> {
37+
/// This returns the raw index of the cached JsValue, so you must take care
38+
/// so that you don't use it after it is freed.
39+
pub(crate) fn unsafe_get_str(s: &str) -> Option<<JsValue as IntoWasmAbi>::Abi> {
3740
CACHE.with(|cache| {
3841
let mut cache = cache.entries.borrow_mut();
3942

4043
if let Some(value) = get_js_string(&mut cache, s) {
41-
// This is safe because the cache values are never removed
42-
Some(value._unsafe_clone())
44+
Some(value.into_abi())
4345

4446
} else {
4547
None

src/convert/slices.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,11 @@ cfg_if! {
129129
if #[cfg(feature = "enable-interning")] {
130130
#[inline]
131131
fn get_cached_str(x: &str) -> Option<WasmSlice> {
132-
// This uses 0 for the ptr as an indication that it is a JsValue and not a str
133-
crate::cache::intern::get_str(x).map(|x| WasmSlice { ptr: 0, len: x.into_abi() })
132+
// This is safe because the JsValue is immediately converted into a JS string,
133+
// so use-after-free cannot occur.
134+
//
135+
// This uses 0 for the ptr as an indication that it is a JsValue and not a str.
136+
crate::cache::intern::unsafe_get_str(x).map(|x| WasmSlice { ptr: 0, len: x })
134137
}
135138

136139
} else {

src/lib.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,6 @@ impl JsValue {
125125
}
126126
}
127127

128-
#[inline]
129-
fn _unsafe_clone(&self) -> JsValue {
130-
Self::_new(self.idx)
131-
}
132-
133128
/// Creates a new JS value which is a string.
134129
///
135130
/// The utf-8 string provided is copied to the JS heap and the string will

0 commit comments

Comments
 (0)