Skip to content

Commit a842712

Browse files
committed
mapnik.Map - check for valid width/height and throw an exception if not (#990)
1 parent c17f136 commit a842712

File tree

1 file changed

+34
-21
lines changed

1 file changed

+34
-21
lines changed

src/mapnik_map.cpp

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include <mapnik/save_map.hpp> // for save_map, etc
1111
// stl
1212
#include <sstream> // for basic_ostringstream, etc
13+
#include <cmath>
14+
#include <limits>
1315

1416
Napi::FunctionReference Map::constructor;
1517

@@ -128,35 +130,46 @@ Map::Map(Napi::CallbackInfo const& info)
128130
return;
129131
}
130132

131-
if (info.Length() == 2)
133+
if (info.Length() > 1 && info.Length() < 4 )
132134
{
133-
if (!info[0].IsNumber() || !info[1].IsNumber())
135+
if (info[0].IsNumber() && info[1].IsNumber())
134136
{
135-
Napi::TypeError::New(env, "'width' and 'height' must be integers").ThrowAsJavaScriptException();
136-
return;
137-
}
138-
map_ = std::make_shared<mapnik::Map>(info[0].As<Napi::Number>().Int32Value(), info[1].As<Napi::Number>().Int32Value());
139-
return;
140-
}
141-
else if (info.Length() == 3)
142-
{
143-
if (!info[0].IsNumber() || !info[1].IsNumber())
144-
{
145-
Napi::TypeError::New(env, "'width' and 'height' must be integers").ThrowAsJavaScriptException();
146-
return;
137+
std::int32_t width = info[0].As<Napi::Number>().Int32Value();
138+
std::int32_t height = info[1].As<Napi::Number>().Int32Value();
139+
if (width > 0 && width <= std::numeric_limits<std::int32_t>::max()
140+
&& height > 0 && height <= std::numeric_limits<std::int32_t>::max())
141+
{
142+
143+
if (info.Length() == 3)
144+
{
145+
if (!info[2].IsString())
146+
{
147+
Napi::Error::New(env, "'srs' value must be a string").ThrowAsJavaScriptException();
148+
return;
149+
}
150+
151+
map_ = std::make_shared<mapnik::Map>(width, height, info[2].As<Napi::String>());
152+
}
153+
else
154+
{
155+
map_ = std::make_shared<mapnik::Map>(width, height);
156+
}
157+
}
158+
else
159+
{
160+
Napi::TypeError::New(env, "'width' and 'height' must be positive finite numbers").ThrowAsJavaScriptException();
161+
return;
162+
}
147163
}
148-
if (!info[2].IsString())
164+
else
149165
{
150-
Napi::Error::New(env, "'srs' value must be a string").ThrowAsJavaScriptException();
151-
return;
166+
Napi::TypeError::New(env, "'width' and 'height' must positive finite numbers").ThrowAsJavaScriptException();
167+
return;
152168
}
153-
map_ = std::make_shared<mapnik::Map>(info[0].As<Napi::Number>().Int32Value(),
154-
info[1].As<Napi::Number>().Int32Value(),
155-
info[2].As<Napi::String>());
156169
}
157170
else
158171
{
159-
Napi::Error::New(env, "please provide Map width and height and optional srs").ThrowAsJavaScriptException();
172+
Napi::Error::New(env, "please provide Map width, height and optional srs").ThrowAsJavaScriptException();
160173
}
161174
}
162175

0 commit comments

Comments
 (0)