Skip to content

Commit

Permalink
Reimplement WasmPtr::get_utf8_string to be fully sound
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark McCaskey committed Jan 4, 2021
1 parent 316bfa0 commit b592ed8
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion lib/api/src/ptr.rs
Original file line number Diff line number Diff line change
@@ -217,7 +217,25 @@ impl<T: Copy + ValueType> WasmPtr<T, Array> {
///
/// an aliasing `WasmPtr` is used to mutate memory.
pub fn get_utf8_string(self, memory: &Memory, str_len: u32) -> Option<String> {
unsafe { self.get_utf8_str(memory, str_len).map(|s| s.to_owned()) }
let memory_size = memory.size().bytes().0;
if self.offset as usize + str_len as usize > memory.size().bytes().0
|| self.offset as usize >= memory_size
{
return None;
}

// TODO: benchmark the internals of this function: there is likely room for
// micro-optimization here and this may be a fairly common function in user code.
let view = memory.view::<u8>();

let mut vec: Vec<u8> = Vec::with_capacity(str_len as usize);
let base = self.offset as usize;
for i in 0..(str_len as usize) {
let byte = view[base + i].get();
vec.push(byte);
}

String::from_utf8(vec).ok()
}

/// Get a UTF-8 string from the `WasmPtr`, where the string is nul-terminated.

0 comments on commit b592ed8

Please sign in to comment.