Skip to content

Commit 0b3f1c8

Browse files
committed
f2
1 parent 379afb0 commit 0b3f1c8

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

src/CanvasRenderingContext2d.cc

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,20 +1226,26 @@ Napi::Value
12261226
Context2d::CreateImageData(const Napi::CallbackInfo& info){
12271227
Canvas *canvas = this->canvas();
12281228
Napi::Number zero = Napi::Number::New(env, 0);
1229-
int32_t width, height;
1229+
uint32_t width, height;
12301230

12311231
if (info[0].IsObject()) {
12321232
Napi::Object obj = info[0].As<Napi::Object>();
1233-
width = obj.Get("width").UnwrapOr(zero).ToNumber().UnwrapOr(zero).Int32Value();
1234-
height = obj.Get("height").UnwrapOr(zero).ToNumber().UnwrapOr(zero).Int32Value();
1233+
width = obj.Get("width").UnwrapOr(zero).ToNumber().UnwrapOr(zero).Uint32Value();
1234+
height = obj.Get("height").UnwrapOr(zero).ToNumber().UnwrapOr(zero).Uint32Value();
12351235
} else {
1236-
width = info[0].ToNumber().UnwrapOr(zero).Int32Value();
1237-
height = info[1].ToNumber().UnwrapOr(zero).Int32Value();
1236+
width = info[0].ToNumber().UnwrapOr(zero).Uint32Value();
1237+
height = info[1].ToNumber().UnwrapOr(zero).Uint32Value();
12381238
}
12391239

12401240
int stride = canvas->stride();
12411241
double Bpp = static_cast<double>(stride) / canvas->getWidth();
1242-
int nBytes = static_cast<int>(Bpp * width * height + .5);
1242+
int64_t nBytes = Bpp * width * height + .5;
1243+
1244+
if (nBytes > INT32_MAX) {
1245+
std::string msg = "buffer exceeds " + std::to_string(INT32_MAX) + " bytes";
1246+
Napi::Error::New(env, msg).ThrowAsJavaScriptException();
1247+
return env.Undefined();
1248+
}
12431249

12441250
Napi::ArrayBuffer ab = Napi::ArrayBuffer::New(env, nBytes);
12451251
Napi::Value arr;

test/canvas.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,14 @@ describe('Canvas', function () {
976976
assert.equal(0, imageData.data[0])
977977
assert.equal(0, imageData.data[1])
978978
})
979+
980+
it('correctly handles negative values', function () {
981+
const canvas = createCanvas(20, 20)
982+
const ctx = canvas.getContext('2d')
983+
const data = ctx.createImageData(-0xfffffffe, -0xfffffffe);
984+
assert.equal(data.width, 2);
985+
assert.equal(data.height, 2);
986+
})
979987
})
980988

981989
describe('Context2d#measureText()', function () {

0 commit comments

Comments
 (0)