Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core, value): add as_bytes_mut() to array buffer and typed array #329

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

stephencroberts
Copy link

Overview

There's currently not a way to modify the bytes of an ArrayBuffer or a typed array like Uint8Array, so this PR adds new as_bytes_mut() methods that follows the typical convention in Rust.

Comment on lines +115 to 118
pub fn as_bytes_mut(&self) -> Option<&mut [u8]> {
let raw = Self::get_raw(self.as_value())?;
Some(unsafe { slice::from_raw_parts_mut(raw.ptr.as_ptr(), raw.len) })
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unsound. It allows you to call as_bytes_mut twice on the same object resulting in aliasing mutable references which is undefined behavior. This also cannot be avoided by making the function take a mutable reference to self as you can have two separate ArrayBuffer values point to the same slice.

In order to make this sound, there needs to be some kind of RefCell/Mutex like lock to prevent aliasing mutable references.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants