Automattic / Automattic/node-canvas

Segmentation fault: 11 when rendering SVG with 0 size

Open
#1,400 2 comments 2 reactions 0 assignees View on GitHub
Bug Help wanted
Dominant language
JavaScript
Stars
10.7k
Forks
1.2k
Avg merge
4d 8h
Merged PRs (30d)
1

Description

## Issue or Feature
Issue: a Segmentation fault is thrown when the canvas size is 0x0 and the width and height passed to `drawImage()` are also `0`.

Why would I do this? Well not deliberately!
Another component was passing in the size of the canvas as a string, instead of a number.
But because node was SegFaulting, it was hard to trace back to what was going wrong.

## Steps to Reproduce
```js
const fs = require('fs')
const { createCanvas, loadImage } = require('canvas')

let canvas = createCanvas('600', '600')
let ctx = canvas.getContext('2d')

console.log('canvas.width: ' + canvas.width)
console.log('canvas.height: ' + canvas.height)
ctx.fillStyle = '#FF0000'
ctx.fillRect(0, 0, canvas.width, canvas.height)

const promise = loadImage('Ghostscript_Tiger.svg')
promise.then((image) => {
// Scale the SVG to fit the canvas
image.width = canvas.width
image.height = canvas.height
console.log('image.width: ' + image.width)
console.log('image.height: ' + image.height)

ctx.drawImage(
image,
0, 0,
image.width, image.height
)
}).then(() => {
const out = fs.createWriteStream('Ghostscript_Tiger-600.png')
const stream = canvas.createPNGStream()
stream.pipe(out)
out.on('finish', () => console.log('Finished'))
})
```

## Your Environment
* Version of node-canvas: 2.4.1
* Version of node: v8.12.0
* Version of cairo: 1.16.0
* Version of Mac OS: 10.12.6

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by running the supplied JavaScript reproduction with the stated node-canvas, Node, Cairo, and macOS versions, focusing on createCanvas(), loadImage(), and drawImage() with zero dimensions. Trace the SVG rendering path to identify where the segmentation fault occurs. Done means the reproduction no longer crashes the process and the zero-size case has regression coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
computer-graphics
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.