Skip to content

Commit 379afb0

Browse files
committed
f1
1 parent 45ae6d5 commit 379afb0

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/CanvasRenderingContext2d.cc

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,6 +1058,16 @@ Context2d::GetImageData(const Napi::CallbackInfo& info) {
10581058
sh = -sh;
10591059
}
10601060

1061+
int srcStride = canvas->stride();
1062+
int bpp = srcStride / width;
1063+
int64_t size = static_cast<int64_t>(sw) * sh * bpp;
1064+
1065+
if (size > INT32_MAX) {
1066+
std::string msg = "buffer exceeds " + std::to_string(INT32_MAX) + " bytes";
1067+
Napi::Error::New(env, msg).ThrowAsJavaScriptException();
1068+
return env.Undefined();
1069+
}
1070+
10611071
// Width and height to actually copy
10621072
int cw = sw;
10631073
int ch = sh;
@@ -1081,9 +1091,6 @@ Context2d::GetImageData(const Napi::CallbackInfo& info) {
10811091
sy = 0;
10821092
}
10831093

1084-
int srcStride = canvas->stride();
1085-
int bpp = srcStride / width;
1086-
int size = sw * sh * bpp;
10871094
int dstStride = sw * bpp;
10881095

10891096
uint8_t *src = canvas->data();

test/canvas.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1741,6 +1741,13 @@ describe('Canvas', function () {
17411741
})
17421742
})
17431743

1744+
it('limits sizes to CAIRO_MAX', function () {
1745+
const canvas = createCanvas(256, 1);
1746+
const ctx = canvas.getContext('2d', {pixelFormat: 'A8'});
1747+
// Integer overflow: 256 * 16777217 * 1(A8) = 0x100000100 → int32 = 256
1748+
assert.throws(() => ctx.getImageData(0, 0, 256, 16777217));
1749+
});
1750+
17441751
describe('does not throw if rectangle is outside the canvas (#2024)', function () {
17451752
it('on the left', function () {
17461753
const ctx = createTestCanvas(true, { pixelFormat: 'A8' })

0 commit comments

Comments
 (0)