Skip to content

Commit

Permalink
bug: fix process crash in getImageData for PDF/SVG canvases
Browse files Browse the repository at this point in the history
Fixes #1853
  • Loading branch information
zbjornson committed Jan 22, 2022
1 parent 83a8df1 commit 7c26dc6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ project adheres to [Semantic Versioning](http://semver.org/).
### Fixed
* Stringify CanvasGradient, CanvasPattern and ImageData like browsers do. (#1639, #1646)
* Add missing include for `toupper`.
* Throw an error instead of crashing the process if `getImageData` or `putImageData` is called on a PDF or SVG canvas (#1853)

2.9.0
==================
Expand Down
8 changes: 4 additions & 4 deletions src/CanvasRenderingContext2d.cc
Original file line number Diff line number Diff line change
Expand Up @@ -937,8 +937,8 @@ NAN_METHOD(Context2d::PutImageData) {
}
#endif
default: {
Nan::ThrowError("Invalid pixel format");
break;
Nan::ThrowError("Invalid pixel format or not an image canvas");
return;
}
}

Expand Down Expand Up @@ -1111,8 +1111,8 @@ NAN_METHOD(Context2d::GetImageData) {
#endif
default: {
// Unlikely
Nan::ThrowError("Invalid pixel format");
break;
Nan::ThrowError("Invalid pixel format or not an image canvas");
return;
}
}

Expand Down
20 changes: 20 additions & 0 deletions test/canvas.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,14 @@ describe('Canvas', function () {
const ctx = createTestCanvas()
assert.throws(function () { ctx.getImageData(0, 0, 0, 0) }, /IndexSizeError/)
})

it('throws if canvas is a PDF canvas (#1853)', function () {
const canvas = createCanvas(3, 6, 'pdf')
const ctx = canvas.getContext('2d')
assert.throws(() => {
ctx.getImageData(0, 0, 3, 6)
})
})
})

it('Context2d#createPattern(Canvas)', function () {
Expand Down Expand Up @@ -1402,6 +1410,18 @@ describe('Canvas', function () {
assert.throws(function () { ctx.putImageData(undefined, 0, 0) }, TypeError)
})

it('throws if canvas is a PDF canvas (#1853)', function () {
const canvas = createCanvas(3, 6, 'pdf')
const ctx = canvas.getContext('2d')
const srcImageData = createImageData(new Uint8ClampedArray([
1, 2, 3, 255, 5, 6, 7, 255,
0, 1, 2, 255, 4, 5, 6, 255
]), 2)
assert.throws(() => {
ctx.putImageData(srcImageData, -1, -1)
})
})

it('works for negative source values', function () {
const canvas = createCanvas(2, 2)
const ctx = canvas.getContext('2d')
Expand Down

0 comments on commit 7c26dc6

Please sign in to comment.