Merge pull request #2588 from myk002/myk_fix_screen_crash

fix a crash in Screen when read or paint tile indices are negative
develop
Myk 2023-01-10 21:11:44 -08:00 committed by GitHub
commit b36aa3712c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 27 additions and 14 deletions

@ -116,17 +116,22 @@ bool Screen::inGraphicsMode()
} }
static bool doSetTile_map(const Pen &pen, int x, int y) { static bool doSetTile_map(const Pen &pen, int x, int y) {
size_t max_index = gps->main_viewport->dim_x * gps->main_viewport->dim_y - 1; auto &vp = gps->main_viewport;
size_t index = (x * gps->main_viewport->dim_y) + y;
if (index < 0 || index > max_index) if (x < 0 || x >= vp->dim_x || y < 0 || y >= vp->dim_y)
return false;
size_t max_index = vp->dim_x * vp->dim_y - 1;
size_t index = (x * vp->dim_y) + y;
if (index > max_index)
return false; return false;
long texpos = pen.tile; long texpos = pen.tile;
if (texpos == 0) { if (texpos == 0) {
texpos = init->font.large_font_texpos[(uint8_t)pen.ch]; texpos = init->font.large_font_texpos[(uint8_t)pen.ch];
} }
gps->main_viewport->screentexpos_interface[index] = texpos; vp->screentexpos_interface[index] = texpos;
return true; return true;
} }
@ -137,6 +142,9 @@ static bool doSetTile_default(const Pen &pen, int x, int y, bool map)
if (map && use_graphics) if (map && use_graphics)
return doSetTile_map(pen, x, y); return doSetTile_map(pen, x, y);
if (x < 0 || x >= gps->dimx || y < 0 || y >= gps->dimy)
return false;
size_t index = (x * gps->dimy) + y; size_t index = (x * gps->dimy) + y;
uint8_t *screen = &gps->screen[index * 8]; uint8_t *screen = &gps->screen[index * 8];
@ -215,21 +223,26 @@ bool Screen::paintTile(const Pen &pen, int x, int y, bool map)
} }
static Pen doGetTile_map(int x, int y) { static Pen doGetTile_map(int x, int y) {
size_t max_index = gps->main_viewport->dim_x * gps->main_viewport->dim_y - 1; auto &vp = gps->main_viewport;
size_t index = (x * gps->main_viewport->dim_y) + y;
if (x < 0 || x >= vp->dim_x || y < 0 || y >= vp->dim_y)
return Pen(0, 0, 0, -1);
size_t max_index = vp->dim_x * vp->dim_y - 1;
size_t index = (x * vp->dim_y) + y;
if (index < 0 || index > max_index) if (index < 0 || index > max_index)
return Pen(0, 0, 0, -1); return Pen(0, 0, 0, -1);
int tile = gps->main_viewport->screentexpos[index]; int tile = vp->screentexpos[index];
if (tile == 0) if (tile == 0)
tile = gps->main_viewport->screentexpos_item[index]; tile = vp->screentexpos_item[index];
if (tile == 0) if (tile == 0)
tile = gps->main_viewport->screentexpos_building_one[index]; tile = vp->screentexpos_building_one[index];
if (tile == 0) if (tile == 0)
tile = gps->main_viewport->screentexpos_background_two[index]; tile = vp->screentexpos_background_two[index];
if (tile == 0) if (tile == 0)
tile = gps->main_viewport->screentexpos_background[index]; tile = vp->screentexpos_background[index];
char ch = 0; char ch = 0;
uint8_t fg = 0; uint8_t fg = 0;
@ -249,14 +262,14 @@ static uint8_t to_16_bit_color(uint8_t *rgb) {
} }
static Pen doGetTile_default(int x, int y, bool map) { static Pen doGetTile_default(int x, int y, bool map) {
if (x < 0 || y < 0)
return Pen(0, 0, 0, -1);
bool use_graphics = Screen::inGraphicsMode(); bool use_graphics = Screen::inGraphicsMode();
if (map && use_graphics) if (map && use_graphics)
return doGetTile_map(x, y); return doGetTile_map(x, y);
if (x < 0 || x >= gps->dimx || y < 0 || y >= gps->dimy)
return Pen(0, 0, 0, -1);
size_t index = (x * gps->dimy) + y; size_t index = (x * gps->dimy) + y;
uint8_t *screen = &gps->screen[index * 8]; uint8_t *screen = &gps->screen[index * 8];