Improve exception messages for out-of-bounds image access

This commit is contained in:
sfan5 2018-03-24 14:05:10 +01:00
parent 7288e60cd2
commit 976c690357
1 changed files with 18 additions and 10 deletions

View File

@ -10,12 +10,7 @@
#include "Image.h" #include "Image.h"
#ifndef NDEBUG #ifndef NDEBUG
#define SIZECHECK(x, y) do { \ #define SIZECHECK(x, y) check_bounds((x), (y), m_width, m_height)
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)
#else #else
#define SIZECHECK(x, y) do {} while(0) #define SIZECHECK(x, y) do {} while(0)
#endif #endif
@ -40,6 +35,22 @@ static inline Color int2color(int c)
return c2; 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) : Image::Image(int width, int height) :
m_width(width), m_height(height), m_image(NULL) m_width(width), m_height(height), m_image(NULL)
@ -60,10 +71,7 @@ void Image::setPixel(int x, int y, const Color &c)
Color Image::getPixel(int x, int y) Color Image::getPixel(int x, int y)
{ {
#ifndef NDEBUG SIZECHECK(x, y);
if(x < 0 || x > m_width || y < 0 || y > m_height)
throw std::out_of_range("sizecheck");
#endif
return int2color(m_image->tpixels[y][x]); return int2color(m_image->tpixels[y][x]);
} }