Skip to content

Commit 1ccc7c7

Browse files
authored
Fix exception safety violation in AbiBuffer::advance (#1649)
* Fix exception safety violation in AbiBuffer::advance * Add more explanation
1 parent f1e5557 commit 1ccc7c7

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

crates/guest-rust/src/rt/async_support/abi_buffer.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,12 @@ impl<O: StreamOps> AbiBuffer<O> {
133133
let (mut ptr, len) = self.abi_ptr_and_len();
134134
assert!(amt <= len);
135135
for _ in 0..amt {
136+
// Update self.cursor incrementally for exception safety.
137+
// When `self.ops.dealloc_lists` panics (which is a user-provided
138+
// callback), we can make sure any item before (including the
139+
// panic one) will not be dealloced again, and the remaining items
140+
// can still get advanced properly.
141+
self.cursor += 1;
136142
// SAFETY: we're managing the pointer passed to `dealloc_lists` and
137143
// it was initialized with a `lower`, and then the pointer
138144
// arithmetic should all be in-bounds.
@@ -141,7 +147,6 @@ impl<O: StreamOps> AbiBuffer<O> {
141147
ptr = ptr.add(self.ops.elem_layout().size());
142148
}
143149
}
144-
self.cursor += amt;
145150
}
146151

147152
fn take_vec(&mut self) -> Vec<O::Payload> {

0 commit comments

Comments
 (0)