33
33
34
34
using namespace std ;
35
35
36
+ #define CAIRO_MAX_SIZE 32767
37
+
36
38
/*
37
39
* Initialize Canvas.
38
40
*/
@@ -79,6 +81,11 @@ Canvas::Canvas(const Napi::CallbackInfo& info) : Napi::ObjectWrap<Canvas>(info),
79
81
_surface = nullptr ;
80
82
_closure = nullptr ;
81
83
format = CAIRO_FORMAT_ARGB32;
84
+ this ->width = 0 ;
85
+ this ->height = 0 ;
86
+
87
+ int32_t width;
88
+ int32_t height;
82
89
83
90
if (info[0 ].IsNumber ()) {
84
91
width = info[0 ].As <Napi::Number>().Int32Value ();
@@ -104,6 +111,21 @@ Canvas::Canvas(const Napi::CallbackInfo& info) : Napi::ObjectWrap<Canvas>(info),
104
111
type = CANVAS_TYPE_IMAGE;
105
112
}
106
113
114
+ if (width > CAIRO_MAX_SIZE || width < 0 ) {
115
+ std::string msg = " Canvas width must be between 0 and " + std::to_string (CAIRO_MAX_SIZE);
116
+ Napi::Error::New (env, msg).ThrowAsJavaScriptException ();
117
+ return ;
118
+ }
119
+
120
+ if (height > CAIRO_MAX_SIZE || height < 0 ) {
121
+ std::string msg = " Canvas height must be between 0 and " + std::to_string (CAIRO_MAX_SIZE);
122
+ Napi::Error::New (env, msg).ThrowAsJavaScriptException ();
123
+ return ;
124
+ }
125
+
126
+ this ->width = width;
127
+ this ->height = height;
128
+
107
129
cairo_status_t status = cairo_surface_status (ensureSurface ());
108
130
109
131
if (status != CAIRO_STATUS_SUCCESS) {
@@ -156,8 +178,11 @@ Canvas::GetWidth(const Napi::CallbackInfo& info) {
156
178
void
157
179
Canvas::SetWidth (const Napi::CallbackInfo& info, const Napi::Value& value) {
158
180
if (value.IsNumber ()) {
159
- width = value.As <Napi::Number>().Uint32Value ();
160
- resurface (info.This ().As <Napi::Object>());
181
+ int32_t width = value.As <Napi::Number>().Int32Value ();
182
+ if (width >= 0 && width <= CAIRO_MAX_SIZE) {
183
+ this ->width = width;
184
+ resurface (info.This ().As <Napi::Object>());
185
+ }
161
186
}
162
187
}
163
188
@@ -177,8 +202,11 @@ Canvas::GetHeight(const Napi::CallbackInfo& info) {
177
202
void
178
203
Canvas::SetHeight (const Napi::CallbackInfo& info, const Napi::Value& value) {
179
204
if (value.IsNumber ()) {
180
- height = value.As <Napi::Number>().Uint32Value ();
181
- resurface (info.This ().As <Napi::Object>());
205
+ int32_t height = value.As <Napi::Number>().Uint32Value ();
206
+ if (height >= 0 && height <= CAIRO_MAX_SIZE) {
207
+ this ->height = height;
208
+ resurface (info.This ().As <Napi::Object>());
209
+ }
182
210
}
183
211
}
184
212
0 commit comments