From d886b3fa66843b67a2b68309ca6bdc8dc2b2adda Mon Sep 17 00:00:00 2001 From: Rasmus Anthin Date: Tue, 3 Dec 2024 18:48:04 +0100 Subject: [PATCH] SpriteHandler.h: * BitmapSprite::set_sprite_data() now checks the range of the source and destination vectors before commencing the assigning. --- SpriteHandler.h | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/SpriteHandler.h b/SpriteHandler.h index aa2267d..53db098 100644 --- a/SpriteHandler.h +++ b/SpriteHandler.h @@ -78,22 +78,25 @@ class BitmapSprite : public Sprite return false; } std::vector source = {static_cast(args)...}; // Unpack and assign to the target vector + auto N_trg = target.size(); + auto N_src = source.size(); for (int i = 0; i < nr; ++i) { - for (int j = 0; j < nc; ++j) - { - // Map bounding box (i, j) to the target sprite data - int trg_row = bb.r + i; - int trg_col = bb.c + j; - - // Calculate linear index into the target vector and src_data - int trg_index = trg_row * size.c + trg_col; - int src_index = i * nc + j; - - // Assign data to the target vector - target[trg_index] = source[src_index]; - } + for (int j = 0; j < nc; ++j) + { + // Map bounding box (i, j) to the target sprite data + int trg_row = bb.r + i; + int trg_col = bb.c + j; + + // Calculate linear index into the target vector and src_data + int trg_index = trg_row * size.c + trg_col; + int src_index = i * nc + j; + + // Assign data to the target vector + if (trg_index < N_trg && src_index < N_src) + target[trg_index] = source[src_index]; + } } return true;