From e24d0244114f4654012b31058def25c577be3c90 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Mon, 10 Oct 2022 21:16:13 +0300 Subject: [PATCH 50/50] savegame?.c: Use SIZE_T_PRINT when printing strlen() result See osdn #45823 Signed-off-by: Marko Lindqvist --- server/savegame/savegame2.c | 55 ++++++++++++++++--------------- server/savegame/savegame3.c | 66 +++++++++++++++++++------------------ 2 files changed, 62 insertions(+), 59 deletions(-) diff --git a/server/savegame/savegame2.c b/server/savegame/savegame2.c index 3dc1710b31..d91b21a81e 100644 --- a/server/savegame/savegame2.c +++ b/server/savegame/savegame2.c @@ -216,8 +216,8 @@ extern bool sg_success; } else if (strlen(_line) != wld.map.xsize) { \ char buf[64]; \ fc_snprintf(buf, sizeof(buf), secpath, ## __VA_ARGS__, _nat_y); \ - log_verbose("Line too short (expected %d got %lu)='%s'", \ - wld.map.xsize, (unsigned long) strlen(_line), buf); \ + log_verbose("Line too short (expected %d got " SIZE_T_PRINTF \ + ")='%s'", wld.map.xsize, strlen(_line), buf); \ _printed_warning = TRUE; \ continue; \ } \ @@ -1676,9 +1676,9 @@ static void sg_load_game(struct loaddata *loading) "game.global_advances"); if (str != NULL) { sg_failure_ret(strlen(str) == loading->technology.size, - "Invalid length of 'game.global_advances' (%lu ~= %lu).", - (unsigned long) strlen(str), - (unsigned long) loading->technology.size); + "Invalid length of 'game.global_advances' (" + SIZE_T_PRINTF " ~= " SIZE_T_PRINTF ").", + strlen(str), loading->technology.size); for (i = 0; i < loading->technology.size; i++) { sg_failure_ret(str[i] == '1' || str[i] == '0', "Undefined value '%c' within 'game.global_advances'.", @@ -2422,9 +2422,9 @@ static void sg_load_players_basic(struct loaddata *loading) "players.destroyed_wonders"); sg_failure_ret(str != NULL, "%s", secfile_error()); sg_failure_ret(strlen(str) == loading->improvement.size, - "Invalid length for 'players.destroyed_wonders' " - "(%lu ~= %lu)", (unsigned long) strlen(str), - (unsigned long) loading->improvement.size); + "Invalid length for 'players.destroyed_wonders' (" + SIZE_T_PRINTF" ~= " SIZE_T_PRINTF ")", + strlen(str), loading->improvement.size); for (k = 0; k < loading->improvement.size; k++) { sg_failure_ret(str[k] == '1' || str[k] == '0', "Undefined value '%c' within " @@ -2433,6 +2433,7 @@ static void sg_load_players_basic(struct loaddata *loading) if (str[k] == '1') { struct impr_type *pimprove = improvement_by_rule_name(loading->improvement.order[k]); + if (pimprove) { game.info.great_wonder_owners[improvement_index(pimprove)] = WONDER_DESTROYED; @@ -3291,10 +3292,11 @@ static void sg_load_player_main(struct loaddata *loading, /* If not present, probably an old savegame; nothing to be done */ if (str != NULL) { int k; + sg_failure_ret(strlen(str) == loading->improvement.size, - "Invalid length for 'player%d.lost_wonders' " - "(%lu ~= %lu)", plrno, (unsigned long) strlen(str), - (unsigned long) loading->improvement.size); + "Invalid length for 'player%d.lost_wonders' (" + SIZE_T_PRINTF " ~= " SIZE_T_PRINTF ")", + plrno, strlen(str), loading->improvement.size); for (k = 0; k < loading->improvement.size; k++) { sg_failure_ret(str[k] == '1' || str[k] == '0', "Undefined value '%c' within " @@ -3601,9 +3603,9 @@ static bool sg_load_player_city(struct loaddata *loading, struct player *plr, str = secfile_lookup_str(loading->file, "%s.improvements", citystr); sg_warn_ret_val(str != NULL, FALSE, "%s", secfile_error()); sg_warn_ret_val(strlen(str) == loading->improvement.size, FALSE, - "Invalid length of '%s.improvements' (%lu ~= %lu).", - citystr, (unsigned long) strlen(str), - (unsigned long) loading->improvement.size); + "Invalid length of '%s.improvements' (" + SIZE_T_PRINTF " ~= " SIZE_T_PRINTF ").", + citystr, strlen(str), loading->improvement.size); for (i = 0; i < loading->improvement.size; i++) { sg_warn_ret_val(str[i] == '1' || str[i] == '0', FALSE, "Undefined value '%c' within '%s.improvements'.", @@ -4582,17 +4584,16 @@ static void sg_load_player_attributes(struct loaddata *loading, log_sg("attribute_v2_block_parts=%d actual=%d", parts, part_nr); break; } - log_debug("attribute_v2_block_length_quoted=%lu have=%lu part=%lu", - (unsigned long) quoted_length, - (unsigned long) strlen(quoted), - (unsigned long) strlen(current)); + log_debug("attribute_v2_block_length_quoted=%d" + " have=" SIZE_T_PRINTF " part=" SIZE_T_PRINTF, + quoted_length, strlen(quoted), strlen(current)); fc_assert(strlen(quoted) + strlen(current) <= quoted_length); strcat(quoted, current); } fc_assert_msg(quoted_length == strlen(quoted), - "attribute_v2_block_length_quoted=%lu actual=%lu", - (unsigned long) quoted_length, - (unsigned long) strlen(quoted)); + "attribute_v2_block_length_quoted=%d" + " actual=" SIZE_T_PRINTF, + quoted_length, strlen(quoted)); #ifndef FREECIV_NDEBUG actual_length = @@ -4875,9 +4876,9 @@ static bool sg_load_player_vision_city(struct loaddata *loading, str = secfile_lookup_str(loading->file, "%s.improvements", citystr); sg_warn_ret_val(str != NULL, FALSE, "%s", secfile_error()); sg_warn_ret_val(strlen(str) == loading->improvement.size, FALSE, - "Invalid length of '%s.improvements' (%lu ~= %lu).", - citystr, (unsigned long) strlen(str), - (unsigned long) loading->improvement.size); + "Invalid length of '%s.improvements' (" + SIZE_T_PRINTF " ~= " SIZE_T_PRINTF ").", + citystr, strlen(str), loading->improvement.size); for (i = 0; i < loading->improvement.size; i++) { sg_warn_ret_val(str[i] == '1' || str[i] == '0', FALSE, "Undefined value '%c' within '%s.improvements'.", @@ -4988,9 +4989,9 @@ static void sg_load_researches(struct loaddata *loading) str = secfile_lookup_str(loading->file, "research.r%d.done", i); sg_failure_ret(str != NULL, "%s", secfile_error()); sg_failure_ret(strlen(str) == loading->technology.size, - "Invalid length of 'research.r%d.done' (%lu ~= %lu).", - i, (unsigned long) strlen(str), - (unsigned long) loading->technology.size); + "Invalid length of 'research.r%d.done' (" + SIZE_T_PRINTF " ~= " SIZE_T_PRINTF ").", + i, strlen(str), loading->technology.size); for (j = 0; j < loading->technology.size; j++) { sg_failure_ret(str[j] == '1' || str[j] == '0', "Undefined value '%c' within 'research.r%d.done'.", diff --git a/server/savegame/savegame3.c b/server/savegame/savegame3.c index dd662759e2..37f1085c69 100644 --- a/server/savegame/savegame3.c +++ b/server/savegame/savegame3.c @@ -228,8 +228,8 @@ extern bool sg_success; } else if (strlen(_line) != wld.map.xsize) { \ char buf[64]; \ fc_snprintf(buf, sizeof(buf), secpath, ## __VA_ARGS__, _nat_y); \ - log_verbose("Line too short (expected %d got %lu)='%s'", \ - wld.map.xsize, (unsigned long) strlen(_line), buf); \ + log_verbose("Line too short (expected %d got " SIZE_T_PRINTF \ + ")='%s'", wld.map.xsize, strlen(_line), buf); \ _printed_warning = TRUE; \ continue; \ } \ @@ -2112,9 +2112,9 @@ static void sg_load_game(struct loaddata *loading) "game.global_advances"); if (str != NULL) { sg_failure_ret(strlen(str) == loading->technology.size, - "Invalid length of 'game.global_advances' (%lu ~= %lu).", - (unsigned long) strlen(str), - (unsigned long) loading->technology.size); + "Invalid length of 'game.global_advances' (" + SIZE_T_PRINTF " ~= " SIZE_T_PRINTF ").", + strlen(str), loading->technology.size); for (i = 0; i < loading->technology.size; i++) { sg_failure_ret(str[i] == '1' || str[i] == '0', "Undefined value '%c' within 'game.global_advances'.", @@ -3519,12 +3519,12 @@ static void sg_load_players_basic(struct loaddata *loading) /* Load destroyed wonders: */ str = secfile_lookup_str(loading->file, - "players.destroyed_wonders"); + "players.destroyed_wonders"); sg_failure_ret(str != NULL, "%s", secfile_error()); sg_failure_ret(strlen(str) == loading->improvement.size, - "Invalid length for 'players.destroyed_wonders' " - "(%lu ~= %lu)", (unsigned long) strlen(str), - (unsigned long) loading->improvement.size); + "Invalid length for 'players.destroyed_wonders' (" + SIZE_T_PRINTF " ~= " SIZE_T_PRINTF ")", + strlen(str), loading->improvement.size); for (k = 0; k < loading->improvement.size; k++) { sg_failure_ret(str[k] == '1' || str[k] == '0', "Undefined value '%c' within " @@ -4443,10 +4443,11 @@ static void sg_load_player_main(struct loaddata *loading, /* If not present, probably an old savegame; nothing to be done */ if (str != NULL) { int k; + sg_failure_ret(strlen(str) == loading->improvement.size, - "Invalid length for 'player%d.lost_wonders' " - "(%lu ~= %lu)", plrno, (unsigned long) strlen(str), - (unsigned long) loading->improvement.size); + "Invalid length for 'player%d.lost_wonders' (" + SIZE_T_PRINTF " ~= " SIZE_T_PRINTF ")", + plrno, strlen(str), loading->improvement.size); for (k = 0; k < loading->improvement.size; k++) { sg_failure_ret(str[k] == '1' || str[k] == '0', "Undefined value '%c' within " @@ -5103,9 +5104,9 @@ static bool sg_load_player_city(struct loaddata *loading, struct player *plr, str = secfile_lookup_str(loading->file, "%s.improvements", citystr); sg_warn_ret_val(str != NULL, FALSE, "%s", secfile_error()); sg_warn_ret_val(strlen(str) == loading->improvement.size, FALSE, - "Invalid length of '%s.improvements' (%lu ~= %lu).", - citystr, (unsigned long) strlen(str), - (unsigned long) loading->improvement.size); + "Invalid length of '%s.improvements' (" + SIZE_T_PRINTF " ~= " SIZE_T_PRINTF ").", + citystr, strlen(str), loading->improvement.size); for (i = 0; i < loading->improvement.size; i++) { sg_warn_ret_val(str[i] == '1' || str[i] == '0', FALSE, "Undefined value '%c' within '%s.improvements'.", @@ -5114,6 +5115,7 @@ static bool sg_load_player_city(struct loaddata *loading, struct player *plr, if (str[i] == '1') { struct impr_type *pimprove = improvement_by_rule_name(loading->improvement.order[i]); + if (pimprove) { city_add_improvement(pcity, pimprove); } @@ -5556,10 +5558,11 @@ static void sg_save_player_cities(struct savedata *saving, : '1'; } improvement_iterate_end; impr_buf[improvement_count()] = '\0'; + sg_failure_ret(strlen(impr_buf) < sizeof(impr_buf), "Invalid size of the improvement vector (%s.improvements: " - "%lu < %lu).", buf, (long unsigned int) strlen(impr_buf), - (long unsigned int) sizeof(impr_buf)); + SIZE_T_PRINTF " < " SIZE_T_PRINTF ").", buf, + strlen(impr_buf), sizeof(impr_buf)); secfile_insert_str(saving->file, impr_buf, "%s.improvements", buf); worklist_save(saving->file, &pcity->worklist, wlist_max_length, "%s", @@ -6646,17 +6649,16 @@ static void sg_load_player_attributes(struct loaddata *loading, log_sg("attribute_v2_block_parts=%d actual=%d", parts, part_nr); break; } - log_debug("attribute_v2_block_length_quoted=%lu have=%lu part=%lu", - (unsigned long) quoted_length, - (unsigned long) strlen(quoted), - (unsigned long) strlen(current)); + log_debug("attribute_v2_block_length_quoted=%d" + " have=" SIZE_T_PRINTF " part=" SIZE_T_PRINTF, + quoted_length, strlen(quoted), strlen(current)); fc_assert(strlen(quoted) + strlen(current) <= quoted_length); strcat(quoted, current); } fc_assert_msg(quoted_length == strlen(quoted), - "attribute_v2_block_length_quoted=%lu actual=%lu", - (unsigned long) quoted_length, - (unsigned long) strlen(quoted)); + "attribute_v2_block_length_quoted=%d" + " actual=" SIZE_T_PRINTF, + quoted_length, strlen(quoted)); #ifndef FREECIV_NDEBUG actual_length = @@ -6991,9 +6993,9 @@ static bool sg_load_player_vision_city(struct loaddata *loading, str = secfile_lookup_str(loading->file, "%s.improvements", citystr); sg_warn_ret_val(str != NULL, FALSE, "%s", secfile_error()); sg_warn_ret_val(strlen(str) == loading->improvement.size, FALSE, - "Invalid length of '%s.improvements' (%lu ~= %lu).", - citystr, (unsigned long) strlen(str), - (unsigned long) loading->improvement.size); + "Invalid length of '%s.improvements' (" + SIZE_T_PRINTF " ~= " SIZE_T_PRINTF ").", + citystr, strlen(str), loading->improvement.size); for (i = 0; i < loading->improvement.size; i++) { sg_warn_ret_val(str[i] == '1' || str[i] == '0', FALSE, "Undefined value '%c' within '%s.improvements'.", @@ -7193,8 +7195,8 @@ static void sg_save_player_vision(struct savedata *saving, impr_buf[improvement_count()] = '\0'; sg_failure_ret(strlen(impr_buf) < sizeof(impr_buf), "Invalid size of the improvement vector (%s.improvements: " - "%lu < %lu).", buf, (long unsigned int) strlen(impr_buf), - (long unsigned int) sizeof(impr_buf)); + SIZE_T_PRINTF " < " SIZE_T_PRINTF" ).", + buf, strlen(impr_buf), sizeof(impr_buf)); secfile_insert_str(saving->file, impr_buf, "%s.improvements", buf); secfile_insert_str(saving->file, pdcity->name, "%s.name", buf); @@ -7274,9 +7276,9 @@ static void sg_load_researches(struct loaddata *loading) str = secfile_lookup_str(loading->file, "research.r%d.done", i); sg_failure_ret(str != NULL, "%s", secfile_error()); sg_failure_ret(strlen(str) == loading->technology.size, - "Invalid length of 'research.r%d.done' (%lu ~= %lu).", - i, (unsigned long) strlen(str), - (unsigned long) loading->technology.size); + "Invalid length of 'research.r%d.done' (" + SIZE_T_PRINTF " ~= " SIZE_T_PRINTF ").", + i, strlen(str), loading->technology.size); for (j = 0; j < loading->technology.size; j++) { sg_failure_ret(str[j] == '1' || str[j] == '0', "Undefined value '%c' within 'research.r%d.done'.", -- 2.35.1