aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaul Rouget <me@paulrouget.com>2020-08-03 16:56:50 +0200
committerPaul Rouget <me@paulrouget.com>2020-08-03 17:36:52 +0200
commit43670c6eaa37ba8dd8258af28ea479f5248aec10 (patch)
tree88b0d554bfd0e4b8f5d7d9577d868f7075f43bd8
parent52f01a8a1491584f0f8915fbcd4f1473a3fe64bf (diff)
downloadservo-43670c6eaa37ba8dd8258af28ea479f5248aec10.tar.gz
servo-43670c6eaa37ba8dd8258af28ea479f5248aec10.zip
Box C preferences to avoid dangling pointers
-rw-r--r--ports/libsimpleservo/capi/src/prefs.rs14
1 files changed, 7 insertions, 7 deletions
diff --git a/ports/libsimpleservo/capi/src/prefs.rs b/ports/libsimpleservo/capi/src/prefs.rs
index 100c0a47969..70534084f57 100644
--- a/ports/libsimpleservo/capi/src/prefs.rs
+++ b/ports/libsimpleservo/capi/src/prefs.rs
@@ -18,7 +18,7 @@ thread_local! {
// The CPREFS are structs holding pointers to values held alive by LOCALCPREFS.
// This is emptied in free_prefs the next time perform_updates is called.
static CPREFS: RefCell<Vec<CPref>> = RefCell::new(Vec::new());
- static LOCALCPREFS: RefCell<BTreeMap<String, LocalCPref>> = RefCell::new(BTreeMap::new());
+ static LOCALCPREFS: RefCell<BTreeMap<String, Box<LocalCPref>>> = RefCell::new(BTreeMap::new());
}
struct LocalCPref {
@@ -70,7 +70,7 @@ pub struct CPref {
}
impl CPref {
- fn new(local: &LocalCPref) -> CPref {
+ fn new(local: &Box<LocalCPref>) -> CPref {
let (pref_type, value) = match &local.value {
LocalCPrefValue::Float(v) => (CPrefType::Float, v as *const f64 as *const c_void),
LocalCPrefValue::Int(v) => (CPrefType::Int, v as *const i64 as *const c_void),
@@ -154,11 +154,11 @@ pub extern "C" fn get_pref(key: *const c_char) -> CPref {
let key = unsafe { CStr::from_ptr(key) };
let key = key.to_str().expect("Can't read string");
let (value, is_default) = simpleservo::get_pref(key);
- let local = LocalCPref {
+ let local = Box::new(LocalCPref {
key: CString::new(key).unwrap(),
value: LocalCPrefValue::new(&value),
is_default: is_default,
- };
+ });
let cpref = CPref::new(&local);
localmap.borrow_mut().insert(key.to_string(), local);
cpref
@@ -199,14 +199,14 @@ pub extern "C" fn get_prefs() -> CPrefList {
// Called from any thread
debug!("get_prefs");
let map = simpleservo::get_prefs();
- let local: BTreeMap<String, LocalCPref> = map
+ let local: BTreeMap<String, Box<LocalCPref>> = map
.into_iter()
.map(|(key, (value, is_default))| {
- let l = LocalCPref {
+ let l = Box::new(LocalCPref {
key: CString::new(key.clone()).unwrap(),
value: LocalCPrefValue::new(&value),
is_default: is_default,
- };
+ });
(key, l)
})
.collect();