From 2a80461c9bca5b8e77708ec31b1b4af316f97b72 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Thu, 24 Aug 2023 12:22:33 +0300 Subject: [PATCH 4/4] Savegame: Fix shuffled players order loading warning Clang analyzer warned about garbage value being used. See osdn #48536 Signed-off-by: Marko Lindqvist --- server/savegame/savegame2.c | 31 ++++++++++++++++--------------- server/savegame/savegame3.c | 31 ++++++++++++++++--------------- 2 files changed, 32 insertions(+), 30 deletions(-) diff --git a/server/savegame/savegame2.c b/server/savegame/savegame2.c index e3dfd238c0..5f5cb365c4 100644 --- a/server/savegame/savegame2.c +++ b/server/savegame/savegame2.c @@ -2610,21 +2610,21 @@ static void sg_load_players_basic(struct loaddata *loading) * handles this ... */ if (secfile_lookup_int_default(loading->file, -1, "players.shuffled_player_%d", 0) >= 0) { - int shuffled_players[player_slot_count()]; - bool shuffled_player_set[player_slot_count()]; - - player_slots_iterate(pslot) { - int plrid = player_slot_index(pslot); + int slots = player_slot_count(); + int plrcount = player_count(); + int shuffled_players[slots]; + bool shuffled_player_set[slots]; + for (i = 0; i < slots; i++) { /* Array to save used numbers. */ - shuffled_player_set[plrid] = FALSE; + shuffled_player_set[i] = FALSE; /* List of all player IDs (needed for set_shuffled_players()). It is * initialised with the value -1 to indicate that no value is set. */ - shuffled_players[plrid] = -1; - } player_slots_iterate_end; + shuffled_players[i] = -1; + } /* Load shuffled player list. */ - for (i = 0; i < player_count(); i++) { + for (i = 0; i < plrcount; i++) { int shuffle = secfile_lookup_int_default(loading->file, -1, "players.shuffled_player_%d", i); @@ -2649,15 +2649,15 @@ static void sg_load_players_basic(struct loaddata *loading) if (shuffle_loaded) { /* Insert missing numbers. */ - int shuffle_index = player_count(); + int shuffle_index = plrcount; - for (i = 0; i < player_slot_count(); i++) { + for (i = 0; i < slots; i++) { if (!shuffled_player_set[i]) { - shuffled_players[shuffle_index] = i; - shuffle_index++; + shuffled_players[shuffle_index++] = i; } - /* shuffle_index must not grow behind the size of shuffled_players. */ - sg_failure_ret(shuffle_index <= player_slot_count(), + + /* shuffle_index must not grow higher than size of shuffled_players. */ + sg_failure_ret(shuffle_index <= slots, "Invalid player shuffle data!"); } @@ -2665,6 +2665,7 @@ static void sg_load_players_basic(struct loaddata *loading) log_debug("[load shuffle] player_count() = %d", player_count()); player_slots_iterate(pslot) { int plrid = player_slot_index(pslot); + log_debug("[load shuffle] id: %3d => slot: %3d | slot %3d: %s", plrid, shuffled_players[plrid], plrid, shuffled_player_set[plrid] ? "is used" : "-"); diff --git a/server/savegame/savegame3.c b/server/savegame/savegame3.c index 783abccc89..8ff0709f19 100644 --- a/server/savegame/savegame3.c +++ b/server/savegame/savegame3.c @@ -3717,21 +3717,21 @@ static void sg_load_players_basic(struct loaddata *loading) * handles this ... */ if (secfile_lookup_int_default(loading->file, -1, "players.shuffled_player_%d", 0) >= 0) { - int shuffled_players[player_slot_count()]; - bool shuffled_player_set[player_slot_count()]; - - player_slots_iterate(pslot) { - int plrid = player_slot_index(pslot); + int slots = player_slot_count(); + int plrcount = player_count(); + int shuffled_players[slots]; + bool shuffled_player_set[slots]; + for (i = 0; i < slots; i++) { /* Array to save used numbers. */ - shuffled_player_set[plrid] = FALSE; + shuffled_player_set[i] = FALSE; /* List of all player IDs (needed for set_shuffled_players()). It is * initialised with the value -1 to indicate that no value is set. */ - shuffled_players[plrid] = -1; - } player_slots_iterate_end; + shuffled_players[i] = -1; + } /* Load shuffled player list. */ - for (i = 0; i < player_count(); i++) { + for (i = 0; i < plrcount; i++) { int shuffle = secfile_lookup_int_default(loading->file, -1, "players.shuffled_player_%d", i); @@ -3756,15 +3756,15 @@ static void sg_load_players_basic(struct loaddata *loading) if (shuffle_loaded) { /* Insert missing numbers. */ - int shuffle_index = player_count(); + int shuffle_index = plrcount; - for (i = 0; i < player_slot_count(); i++) { + for (i = 0; i < slots; i++) { if (!shuffled_player_set[i]) { - shuffled_players[shuffle_index] = i; - shuffle_index++; + shuffled_players[shuffle_index++] = i; } - /* shuffle_index must not grow behind the size of shuffled_players. */ - sg_failure_ret(shuffle_index <= player_slot_count(), + + /* shuffle_index must not grow higher than size of shuffled_players. */ + sg_failure_ret(shuffle_index <= slots, "Invalid player shuffle data!"); } @@ -3772,6 +3772,7 @@ static void sg_load_players_basic(struct loaddata *loading) log_debug("[load shuffle] player_count() = %d", player_count()); player_slots_iterate(pslot) { int plrid = player_slot_index(pslot); + log_debug("[load shuffle] id: %3d => slot: %3d | slot %3d: %s", plrid, shuffled_players[plrid], plrid, shuffled_player_set[plrid] ? "is used" : "-"); -- 2.40.1