Skip to content

Commit

Permalink
protect against negative image dimensions - closes #1927
Browse files Browse the repository at this point in the history
  • Loading branch information
Dane Springmeyer committed Jun 26, 2013
1 parent fd428cf commit ba4d06a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
18 changes: 14 additions & 4 deletions include/mapnik/image_data.hpp
Expand Up @@ -29,6 +29,8 @@
// stl
#include <cassert>
#include <cstring>
#include <stdexcept>


namespace mapnik
{
Expand All @@ -37,11 +39,19 @@ template <class T> class ImageData
public:
typedef T pixel_type;

ImageData(unsigned width,unsigned height)
: width_(width),
height_(height),
pData_((width!=0 && height!=0)? static_cast<T*>(::operator new(sizeof(T)*width*height)):0)
ImageData(int width,int height)
: width_(static_cast<unsigned>(width)),
height_(static_cast<unsigned>(height))
{
if (width < 0)
{
throw std::runtime_error("negative width not allowed for image_data");
}
if (height < 0)
{
throw std::runtime_error("negative height not allowed for image_data");
}
pData_ = (width!=0 && height!=0)? static_cast<T*>(::operator new(sizeof(T)*width*height)):0;
if (pData_) std::memset(pData_,0,sizeof(T)*width_*height_);
}

Expand Down
4 changes: 4 additions & 0 deletions tests/python_tests/image_test.py
Expand Up @@ -12,6 +12,10 @@ def setup():
# from another directory we need to chdir()
os.chdir(execution_path('.'))

@raises(RuntimeError)
def test_negative_image_dimensions():
im = mapnik.Image(-40,40)

def test_tiff_round_trip():
filepath = '/tmp/mapnik-tiff-io.tiff'
im = mapnik.Image(255,267)
Expand Down

0 comments on commit ba4d06a

Please sign in to comment.