Fix bug where setting canvas size to 0 was treated as if context was lost

The canvas context lost handling logic was falsely kicking, rendering
the canvas unusable when setting one of its dimensions to zero.

BUG=532148

Review URL: https://codereview.chromium.org/1350883002

git-svn-id: svn://svn.chromium.org/blink/trunk@202645 bbb929c8-8fbe-4397-9dbb-9b2b20218538
diff --git a/LayoutTests/fast/canvas/bug532148-expected.html b/LayoutTests/fast/canvas/bug532148-expected.html
new file mode 100644
index 0000000..0ebe79d
--- /dev/null
+++ b/LayoutTests/fast/canvas/bug532148-expected.html
@@ -0,0 +1,16 @@
+<!doctype html>
+<canvas id='c'></canvas>
+
+<script>
+const canvas = document.getElementById('c'),
+ctx = c.getContext('2d', {alpha: false});
+
+canvas.width = 128;
+canvas.height = 128;
+
+ctx.fillStyle = 'green';
+ctx.fillRect( 0, 0, 128, 128 );
+ctx.fillStyle = 'blue';
+ctx.fillRect( 0, 0, 64, 64 );
+ctx.fillRect( 64, 64, 64, 64 );
+</script>
\ No newline at end of file
diff --git a/LayoutTests/fast/canvas/bug532148.html b/LayoutTests/fast/canvas/bug532148.html
new file mode 100644
index 0000000..5c0d3e4
--- /dev/null
+++ b/LayoutTests/fast/canvas/bug532148.html
@@ -0,0 +1,21 @@
+<!doctype html>
+<canvas id='c'></canvas>
+
+<script>
+const canvas = document.getElementById('c'),
+ctx = c.getContext('2d', {alpha: false});
+
+// The following would permanently damage the canvas (crbug.com/532148)
+canvas.height = null;
+ctx.fillRect( 0, 0, 128, null );
+
+// Try to use canvas normally
+canvas.width = 128;
+canvas.height = 128;
+
+ctx.fillStyle = 'green';
+ctx.fillRect( 0, 0, 128, 128 );
+ctx.fillStyle = 'blue';
+ctx.fillRect( 0, 0, 64, 64 );
+ctx.fillRect( 64, 64, 64, 64 );
+</script>
\ No newline at end of file
diff --git a/LayoutTests/fast/canvas/canvas-lose-restore-googol-size.html b/LayoutTests/fast/canvas/canvas-lose-restore-googol-size.html
index 8f1d8a9..f8d7bd1 100644
--- a/LayoutTests/fast/canvas/canvas-lose-restore-googol-size.html
+++ b/LayoutTests/fast/canvas/canvas-lose-restore-googol-size.html
@@ -19,11 +19,11 @@
 var lostEventHasFired = false;
 verifyContextLost(false);
 
-var googol = Math.pow(10,100);
-canvas.width = googol;
-canvas.height = googol;
+var bigsize = 1000000000;
+canvas.width = bigsize;
+canvas.height = bigsize;
 verifyContextLost(true);
-canvas.width = googol;
+canvas.width = bigsize;
 verifyContextLost(true);
 canvas.width = 100;
 canvas.height = 100;
diff --git a/Source/core/html/HTMLCanvasElement.cpp b/Source/core/html/HTMLCanvasElement.cpp
index 6dc6f92..fc74f13 100644
--- a/Source/core/html/HTMLCanvasElement.cpp
+++ b/Source/core/html/HTMLCanvasElement.cpp
@@ -692,7 +692,7 @@
 void HTMLCanvasElement::createImageBuffer()
 {
     createImageBufferInternal(nullptr);
-    if (m_didFailToCreateImageBuffer && m_context->is2d())
+    if (m_didFailToCreateImageBuffer && m_context->is2d() && !size().isEmpty())
         m_context->loseContext(CanvasRenderingContext::SyntheticLostContext);
 }