Skip to content

Commit 9d71cfd

Browse files
committed
Fix crash when resurfacing SVG canvases
Fixes #1380
1 parent 4d485aa commit 9d71cfd

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ project adheres to [Semantic Versioning](http://semver.org/).
1212
### Fixed
1313
* Fix crash when changing canvas width/height while `fillStyle` or `strokeStyle`
1414
was set to a `CanvasPattern` or `CanvasGradient` (#1357).
15+
* Fix crash when changing width/height of SVG canvases (#1380).
1516

1617
2.5.0
1718
==================

src/backend/SvgBackend.cc

+8-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <cairo-svg.h>
44
#include "../Canvas.h"
55
#include "../closure.h"
6+
#include <cassert>
67

78
using namespace v8;
89

@@ -13,7 +14,10 @@ SvgBackend::SvgBackend(int width, int height)
1314

1415
SvgBackend::~SvgBackend() {
1516
cairo_surface_finish(surface);
16-
if (_closure) delete _closure;
17+
if (_closure) {
18+
delete _closure;
19+
_closure = nullptr;
20+
}
1721
destroySurface();
1822
}
1923

@@ -22,14 +26,16 @@ Backend *SvgBackend::construct(int width, int height){
2226
}
2327

2428
cairo_surface_t* SvgBackend::createSurface() {
25-
if (!_closure) _closure = new PdfSvgClosure(canvas);
29+
assert(!_closure);
30+
_closure = new PdfSvgClosure(canvas);
2631
surface = cairo_svg_surface_create_for_stream(PdfSvgClosure::writeVec, _closure, width, height);
2732
return surface;
2833
}
2934

3035
cairo_surface_t* SvgBackend::recreateSurface() {
3136
cairo_surface_finish(surface);
3237
delete _closure;
38+
_closure = nullptr;
3339
cairo_surface_destroy(surface);
3440

3541
return createSurface();

test/canvas.test.js

+5
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,11 @@ describe('Canvas', function () {
392392
})
393393
});
394394

395+
it('SVG Canvas#width changes don\'t crash (#1380)', function () {
396+
const myCanvas = createCanvas(100, 100, 'svg')
397+
myCanvas.width = 120;
398+
});
399+
395400
it('Canvas#stride', function() {
396401
var canvas = createCanvas(24, 10);
397402
assert.ok(canvas.stride >= 24, 'canvas.stride is too short');

0 commit comments

Comments
 (0)