Skip to content

Commit bacf048

Browse files
Philippe Plantierchearon
authored andcommitted
fix a crash in getImageData if the rectangle is outside the canvas
1 parent da33bbe commit bacf048

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ project adheres to [Semantic Versioning](http://semver.org/).
1515
* Support for accessibility and links in PDFs
1616

1717
### Fixed
18+
* Fix a crash in `getImageData` when the rectangle is entirely outside the canvas. ([#2024](https://github.com/Automattic/node-canvas/issues/2024))
1819

1920
3.0.1
2021
==================

src/CanvasRenderingContext2d.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,10 +1014,6 @@ Context2d::GetImageData(const Napi::CallbackInfo& info) {
10141014
if (sx + sw > width) sw = width - sx;
10151015
if (sy + sh > height) sh = height - sy;
10161016

1017-
// WebKit/moz functionality. node-canvas used to return in either case.
1018-
if (sw <= 0) sw = 1;
1019-
if (sh <= 0) sh = 1;
1020-
10211017
// Non-compliant. "Pixels outside the canvas must be returned as transparent
10221018
// black." This instead clips the returned array to the canvas area.
10231019
if (sx < 0) {
@@ -1029,6 +1025,10 @@ Context2d::GetImageData(const Napi::CallbackInfo& info) {
10291025
sy = 0;
10301026
}
10311027

1028+
// WebKit/moz functionality. node-canvas used to return in either case.
1029+
if (sw <= 0) sw = 1;
1030+
if (sh <= 0) sh = 1;
1031+
10321032
int srcStride = canvas->stride();
10331033
int bpp = srcStride / width;
10341034
int size = sw * sh * bpp;

test/canvas.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,6 +1274,18 @@ describe('Canvas', function () {
12741274
ctx.getImageData(0, 0, 3, 6)
12751275
})
12761276
})
1277+
1278+
it('does not throw if rectangle is outside the canvas (#2024)', () => {
1279+
const canvas = createCanvas(200, 200)
1280+
const ctx = canvas.getContext('2d')
1281+
1282+
ctx.rect(0, 0, 100, 100);
1283+
ctx.fill();
1284+
1285+
const imageData = ctx.getImageData(0, -11, 10, 10);
1286+
assert.equal(10, imageData.width)
1287+
assert.equal(1, imageData.height)
1288+
})
12771289
})
12781290

12791291
it('Context2d#createPattern(Canvas)', function () {

0 commit comments

Comments
 (0)