From ef1e9a1a3e0704388c01ca583fd66d0f1b5121c5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= <emilio@crisal.io>
Date: Fri, 11 Sep 2020 12:13:30 +0200
Subject: [PATCH] canvas: Properly restore all the remaining items in
 stateStack in endDrawing.

We were correctly finishing the SMask group but not restoring all the extra
transformations applied in stateStack, so if somebody ends up drawing to the
same context after canceling mid-draw we'd get artifacts.

This fixes Mozilla bug 1664178[1].

[1]: https://bugzilla.mozilla.org/show_bug.cgi?id=1664178
---
 src/display/canvas.js | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/display/canvas.js b/src/display/canvas.js
index 58b65d631..6293c75fd 100644
--- a/src/display/canvas.js
+++ b/src/display/canvas.js
@@ -1009,8 +1009,8 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
 
     endDrawing: function CanvasGraphics_endDrawing() {
       // Finishing all opened operations such as SMask group painting.
-      if (this.current.activeSMask !== null) {
-        this.endSMaskGroup();
+      while (this.stateStack.length || this.current.activeSMask !== null) {
+        this.restore();
       }
 
       this.ctx.restore();