mirror of
https://github.com/koreader/koreader.git
synced 2025-08-10 00:52:38 +00:00
A take at better boundary checks in blitbuffer
this will hopefully prevent segfaults resulting from bad coordinate/size input when blitting
This commit is contained in:
43
blitbuffer.c
43
blitbuffer.c
@@ -93,26 +93,53 @@ static int blitToBuffer(lua_State *L) {
|
||||
int x, y;
|
||||
|
||||
// check bounds
|
||||
if(ydest < 0) {
|
||||
// negative ydest, try to compensate
|
||||
if(ydest + h > 0) {
|
||||
// shrink h by negative dest offset
|
||||
h += ydest;
|
||||
// extend source offset
|
||||
yoffs += -ydest;
|
||||
ydest = 0;
|
||||
} else {
|
||||
// effectively no height
|
||||
return 0;
|
||||
}
|
||||
} else if(ydest >= dst->h) {
|
||||
// we're told to paint to off-bound target coords
|
||||
return 0;
|
||||
}
|
||||
if(ydest + h > dst->h) {
|
||||
// clamp height if too large for target size
|
||||
h = dst->h - ydest;
|
||||
}
|
||||
if(yoffs >= src->h) {
|
||||
// recalculated source offset is out of bounds
|
||||
return 0;
|
||||
} else if(yoffs + h > src->h) {
|
||||
// clamp height if too large for source size
|
||||
h = src->h - yoffs;
|
||||
}
|
||||
if(ydest >= dst->h) {
|
||||
// same stuff for x coords:
|
||||
if(xdest < 0) {
|
||||
if(xdest + w > 0) {
|
||||
w += xdest;
|
||||
xoffs += -xdest;
|
||||
xdest = 0;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
} else if(xdest >= dst->w) {
|
||||
return 0;
|
||||
} else if(ydest + h > dst->h) {
|
||||
h = dst->h - ydest;
|
||||
}
|
||||
if(xdest + w > dst->w) {
|
||||
w = dst->w - xdest;
|
||||
}
|
||||
if(xoffs >= src->w) {
|
||||
return 0;
|
||||
} else if(xoffs + w > src->w) {
|
||||
w = src->w - xoffs;
|
||||
}
|
||||
if(xdest >= dst->w) {
|
||||
return 0;
|
||||
} else if(xdest + w > dst->w) {
|
||||
w = dst->w - xdest;
|
||||
}
|
||||
|
||||
uint8_t *dstptr;
|
||||
uint8_t *srcptr;
|
||||
|
||||
Reference in New Issue
Block a user