Skip to content

Commit

Permalink
[cg] Be defensive when restoring saved context state
Browse files Browse the repository at this point in the history
  • Loading branch information
cmyr committed May 11, 2020
1 parent e2cfd14 commit 723e95f
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions piet-coregraphics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,15 @@ impl<'a> RenderContext for CoreGraphicsContext<'a> {
Ok(())
}

//TODO: this panics in CoreGraphics if unbalanced. We could try and track stack depth
//and return an error, maybe?
fn restore(&mut self) -> Result<(), Error> {
self.ctx.restore();
self.transform_stack.pop();
Ok(())
if self.transform_stack.pop().is_some() {
// we're defensive about calling restore on the inner context,
// because an unbalanced call will trigger an assert in C
self.ctx.restore();
Ok(())
} else {
Err(Error::StackUnbalance)
}
}

fn finish(&mut self) -> Result<(), Error> {
Expand Down

0 comments on commit 723e95f

Please sign in to comment.