Clone the reader to prevent cursor misplacement in ReadCString

This commit is contained in:
jiangjianfeng
2025-03-31 03:01:22 +00:00
committed by Ruihan Li
parent f4e79d99d0
commit 1846c680fc
2 changed files with 49 additions and 5 deletions

View File

@ -275,12 +275,13 @@ impl_vm_io_once_pointer!(&mut T, "(**self)");
impl_vm_io_once_pointer!(Box<T>, "(**self)");
impl_vm_io_once_pointer!(Arc<T>, "(**self)");
/// A marker structure used for [`VmReader`] and [`VmWriter`],
/// A marker type used for [`VmReader`] and [`VmWriter`],
/// representing whether reads or writes on the underlying memory region are fallible.
pub struct Fallible;
/// A marker structure used for [`VmReader`] and [`VmWriter`],
pub enum Fallible {}
/// A marker type used for [`VmReader`] and [`VmWriter`],
/// representing whether reads or writes on the underlying memory region are infallible.
pub struct Infallible;
pub enum Infallible {}
/// Copies `len` bytes from `src` to `dst`.
///
@ -393,6 +394,20 @@ pub struct VmReader<'a, Fallibility = Fallible> {
phantom: PhantomData<(&'a [u8], Fallibility)>,
}
// `Clone` can be implemented for `VmReader`
// because it either points to untyped memory or represents immutable references.
// Note that we cannot implement `Clone` for `VmWriter`
// because it can represent mutable references, which must remain exclusive.
impl<Fallibility> Clone for VmReader<'_, Fallibility> {
fn clone(&self) -> Self {
Self {
cursor: self.cursor,
end: self.end,
phantom: PhantomData,
}
}
}
macro_rules! impl_read_fallible {
($reader_fallibility:ty, $writer_fallibility:ty) => {
impl<'a> FallibleVmRead<$writer_fallibility> for VmReader<'a, $reader_fallibility> {