@@ -1226,20 +1226,26 @@ Napi::Value
12261226Context2d::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;
0 commit comments