mirror of
https://github.com/roc-lang/roc.git
synced 2025-08-04 20:28:02 +00:00
avoid writing to readonly refcounts
This commit is contained in:
parent
9b13b19d08
commit
ad41b509cf
2 changed files with 16 additions and 4 deletions
|
@ -174,7 +174,10 @@ where
|
|||
/// should be considered for marking read-only.
|
||||
pub unsafe fn set_readonly(&mut self) {
|
||||
if let Some((_, storage)) = self.elements_and_storage() {
|
||||
storage.set(Storage::Readonly);
|
||||
// Only safe to write to the pointer if it is not constant (0)
|
||||
if !matches!(storage.get(), Storage::Readonly) {
|
||||
storage.set(Storage::Readonly);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -676,7 +679,10 @@ where
|
|||
let ptr = self.ptr_to_refcount();
|
||||
unsafe {
|
||||
let value = std::ptr::read(ptr);
|
||||
std::ptr::write(ptr, Ord::max(0, ((value as isize) + 1) as usize));
|
||||
// Only safe to write to the pointer if it is not constant (0)
|
||||
if value != 0 {
|
||||
std::ptr::write(ptr, (value as isize + 1) as usize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -943,14 +943,20 @@ impl BigString {
|
|||
assert_ne!(self.capacity(), 0);
|
||||
|
||||
let ptr = self.ptr_to_refcount();
|
||||
unsafe { std::ptr::write(ptr, 0) }
|
||||
// Only safe to write to the pointer if it is not constant (0)
|
||||
if unsafe { std::ptr::read(ptr) } != 0 {
|
||||
unsafe { std::ptr::write(ptr, 0) }
|
||||
}
|
||||
}
|
||||
|
||||
fn inc(&mut self) {
|
||||
let ptr = self.ptr_to_refcount();
|
||||
unsafe {
|
||||
let value = std::ptr::read(ptr);
|
||||
std::ptr::write(ptr, Ord::max(0, ((value as isize) + 1) as usize));
|
||||
// Only safe to write to the pointer if it is not constant (0)
|
||||
if value != 0 {
|
||||
std::ptr::write(ptr, (value as isize + 1) as usize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue