Skip to content

Commit

Permalink
Improve exception messages for out-of-bounds image access
Browse files Browse the repository at this point in the history
  • Loading branch information
sfan5 committed Mar 24, 2018
1 parent 7288e60 commit 976c690
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions Image.cpp
Expand Up @@ -10,12 +10,7 @@
#include "Image.h"

#ifndef NDEBUG
#define SIZECHECK(x, y) do { \
if((x) < 0 || (x) >= m_width) \
throw std::out_of_range("sizecheck x"); \
if((y) < 0 || (y) >= m_height) \
throw std::out_of_range("sizecheck y"); \
} while(0)
#define SIZECHECK(x, y) check_bounds((x), (y), m_width, m_height)
#else
#define SIZECHECK(x, y) do {} while(0)
#endif
Expand All @@ -40,6 +35,22 @@ static inline Color int2color(int c)
return c2;
}

static inline void check_bounds(int x, int y, int width, int height)
{
if(x < 0 || x >= width) {
std::ostringstream oss;
oss << "Access outside image bounds (x), 0 < "
<< x << " < " << width << " is false.";
throw std::out_of_range(oss.str());
}
if(y < 0 || y >= height) {
std::ostringstream oss;
oss << "Access outside image bounds (y), 0 < "
<< y << " < " << height << " is false.";
throw std::out_of_range(oss.str());
}
}


Image::Image(int width, int height) :
m_width(width), m_height(height), m_image(NULL)
Expand All @@ -60,10 +71,7 @@ void Image::setPixel(int x, int y, const Color &c)

Color Image::getPixel(int x, int y)
{
#ifndef NDEBUG
if(x < 0 || x > m_width || y < 0 || y > m_height)
throw std::out_of_range("sizecheck");
#endif
SIZECHECK(x, y);
return int2color(m_image->tpixels[y][x]);
}

Expand Down

0 comments on commit 976c690

Please sign in to comment.