From: Holger Schemel Date: Wed, 19 Feb 2020 15:20:36 +0000 (+0100) Subject: improved error handling code when mapping EM engine cave/game elements X-Git-Tag: 4.2.0.0~91 X-Git-Url: https://git.artsoft.org/?a=commitdiff_plain;h=f277b6c7e5926b448fb3ae123b383a540162ef2b;p=rocksndiamonds.git improved error handling code when mapping EM engine cave/game elements --- diff --git a/src/game_em/convert.c b/src/game_em/convert.c index 9ebf6f4c..4a4b9cf4 100644 --- a/src/game_em/convert.c +++ b/src/game_em/convert.c @@ -213,12 +213,14 @@ static const short map[CAVE_TILE_MAX] = int map_em_element_C_to_X(int element_em_cave) { - if (element_em_cave >= 0 && element_em_cave < CAVE_TILE_MAX) - return map[element_em_cave]; + if (element_em_cave < 0 || element_em_cave >= CAVE_TILE_MAX) + { + Error(ERR_WARN, "invalid EM cave element %d", element_em_cave); - Error(ERR_WARN, "invalid EM cave element %d", element_em_cave); + return Xblank; + } - return Xblank; + return map[element_em_cave]; } int map_em_element_X_to_C(int element_em_game) @@ -240,19 +242,19 @@ int map_em_element_X_to_C(int element_em_game) map_reverse_initialized = TRUE; } - if (element_em_game >= 0 && element_em_game < GAME_TILE_MAX) + if (element_em_game < 0 || element_em_game >= GAME_TILE_MAX) { - int element_em_cave = map_reverse[element_em_game]; + Error(ERR_WARN, "invalid EM game element %d", element_em_game); - if (element_em_cave == Cblank && element_em_game != Xblank) - Error(ERR_WARN, "unknown EM game element %d", element_em_game); - - return element_em_cave; + return Cblank; } - Error(ERR_WARN, "invalid EM game element %d", element_em_game); + int element_em_cave = map_reverse[element_em_game]; + + if (element_em_cave == Cblank && element_em_game != Xblank) + Error(ERR_WARN, "unknown EM game element %d", element_em_game); - return Cblank; + return element_em_cave; } void prepare_em_level(void) diff --git a/src/tools.c b/src/tools.c index d7298fc1..d05d6365 100644 --- a/src/tools.c +++ b/src/tools.c @@ -7730,12 +7730,14 @@ int map_element_RND_to_EM_cave(int element_rnd) mapping_initialized = TRUE; } - if (element_rnd >= 0 && element_rnd < NUM_FILE_ELEMENTS) - return map_em_element_X_to_C(mapping_RND_to_EM[element_rnd]); + if (element_rnd < 0 || element_rnd >= NUM_FILE_ELEMENTS) + { + Error(ERR_WARN, "invalid RND level element %d", element_rnd); - Error(ERR_WARN, "invalid RND level element %d", element_rnd); + return EL_UNKNOWN; + } - return EL_UNKNOWN; + return map_em_element_X_to_C(mapping_RND_to_EM[element_rnd]); } int map_element_EM_to_RND_cave(int element_em_cave) @@ -7758,12 +7760,14 @@ int map_element_EM_to_RND_cave(int element_em_cave) mapping_initialized = TRUE; } - if (element_em_cave >= 0 && element_em_cave < CAVE_TILE_MAX) - return mapping_EM_to_RND[map_em_element_C_to_X(element_em_cave)]; + if (element_em_cave < 0 || element_em_cave >= CAVE_TILE_MAX) + { + Error(ERR_WARN, "invalid EM cave element %d", element_em_cave); - Error(ERR_WARN, "invalid EM cave element %d", element_em_cave); + return EL_UNKNOWN; + } - return EL_UNKNOWN; + return mapping_EM_to_RND[map_em_element_C_to_X(element_em_cave)]; } int map_element_EM_to_RND_game(int element_em_game) @@ -7786,12 +7790,14 @@ int map_element_EM_to_RND_game(int element_em_game) mapping_initialized = TRUE; } - if (element_em_game >= 0 && element_em_game < GAME_TILE_MAX) - return mapping_EM_to_RND[element_em_game]; + if (element_em_game < 0 || element_em_game >= GAME_TILE_MAX) + { + Error(ERR_WARN, "invalid EM game element %d", element_em_game); - Error(ERR_WARN, "invalid EM game element %d", element_em_game); + return EL_UNKNOWN; + } - return EL_UNKNOWN; + return mapping_EM_to_RND[element_em_game]; } void map_android_clone_elements_RND_to_EM(struct LevelInfo *level)