Skip to content

Commit

Permalink
fix a crash in getImageDate if the rectangle is outside the canvas
Browse files Browse the repository at this point in the history
  • Loading branch information
Philippe Plantier committed Oct 26, 2022
1 parent 0e6504a commit 66ab835
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ project adheres to [Semantic Versioning](http://semver.org/).
### Added
### Fixed
* `textBaseline` and `textAlign` were not saved/restored by `save()`/`restore()`. ([#1936](https://github.com/Automattic/node-canvas/issues/2029))
* Fix a crash in `getImageData` when the rectangle is entirely outside the canvas. ([#2024](https://github.com/Automattic/node-canvas/issues/2024))

2.10.1
==================
Expand Down
8 changes: 4 additions & 4 deletions src/CanvasRenderingContext2d.cc
Original file line number Diff line number Diff line change
Expand Up @@ -979,10 +979,6 @@ NAN_METHOD(Context2d::GetImageData) {
if (sx + sw > width) sw = width - sx;
if (sy + sh > height) sh = height - sy;

// WebKit/moz functionality. node-canvas used to return in either case.
if (sw <= 0) sw = 1;
if (sh <= 0) sh = 1;

// Non-compliant. "Pixels outside the canvas must be returned as transparent
// black." This instead clips the returned array to the canvas area.
if (sx < 0) {
Expand All @@ -994,6 +990,10 @@ NAN_METHOD(Context2d::GetImageData) {
sy = 0;
}

// WebKit/moz functionality. node-canvas used to return in either case.
if (sw <= 0) sw = 1;
if (sh <= 0) sh = 1;

int srcStride = canvas->stride();
int bpp = srcStride / width;
int size = sw * sh * bpp;
Expand Down
12 changes: 12 additions & 0 deletions test/canvas.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1242,6 +1242,18 @@ describe('Canvas', function () {
ctx.getImageData(0, 0, 3, 6)
})
})

it('does not throw if rectangle is outside the canvas (#2024)', () => {
const canvas = createCanvas(200, 200)
const ctx = canvas.getContext('2d')

ctx.rect(0, 0, 100, 100);
ctx.fill();

const imageData = ctx.getImageData(0, -11, 10, 10);
assert.equal(10, imageData.width)
assert.equal(1, imageData.height)
})
})

it('Context2d#createPattern(Canvas)', function () {
Expand Down

0 comments on commit 66ab835

Please sign in to comment.