From dc8b23c8dbcd1c25a9759a66be47fc3d74663f26 Mon Sep 17 00:00:00 2001 From: Ihnatus Date: Tue, 31 May 2022 23:42:11 +0300 Subject: [PATCH] Disallow city_size out of range for all units in units.ruleset. In compatibility mode, check for city founders, not for "Settlers"; check the upper bound. See OSDN#44730 Signed-off-by: Ihnatus --- server/rscompat.c | 20 ++++++++++++++++++++ server/rscompat.h | 4 ++++ server/rssanity.c | 16 ++++++++++++++++ server/ruleset.c | 11 +++++++---- 4 files changed, 47 insertions(+), 4 deletions(-) diff --git a/server/rscompat.c b/server/rscompat.c index 00234b169a..cab2066686 100644 --- a/server/rscompat.c +++ b/server/rscompat.c @@ -496,3 +496,23 @@ void rscompat_req_adjust_3_2(const struct rscompat_info *compat, } } } + +/**********************************************************************//** + Reset individual property that had a formerly tolerated illegal value +**************************************************************************/ +void rscompat_set_valid_value(const struct rscompat_info *compat, + int *pfield, int defval, + const char *fname, const char *tname, + const char *objname) +{ + char buf[1024]; + + if (compat->log_cb != NULL) { + fc_snprintf(buf, sizeof(buf), + "Set '%s' parameter for %s %s to %d " + "(illegal value %d is not tolerated any more)", + fname, tname, objname, defval, *pfield); + compat->log_cb(buf); + } + *pfield = defval; +} diff --git a/server/rscompat.h b/server/rscompat.h index fe11de8d9b..70a2e293dc 100644 --- a/server/rscompat.h +++ b/server/rscompat.h @@ -65,6 +65,10 @@ const char *rscompat_req_range_3_2(struct rscompat_info *compat, void rscompat_req_adjust_3_2(const struct rscompat_info *compat, const char **ptype, const char **pname, bool *ppresent, const char *sec_name); +void rscompat_set_valid_value(const struct rscompat_info *compat, + int *pfield, int defval, + const char *fname, const char *tname, + const char *objname); #ifdef __cplusplus } diff --git a/server/rssanity.c b/server/rssanity.c index f1499f5c0c..b3b4b6954b 100644 --- a/server/rssanity.c +++ b/server/rssanity.c @@ -1002,6 +1002,22 @@ bool sanity_check_ruleset_data(struct rscompat_info *compat) putype->paratroopers_range, UNIT_MAX_PARADROP_RANGE); ok = FALSE; } + if ((putype->city_size <= 0 || putype->city_size > MAX_CITY_SIZE)) { + fc_assert(compat->compat_mode && compat->version < RSFORMAT_3_2); + if (utype_is_cityfounder(putype)) { + ruleset_error(LOG_ERROR, + "Unit type '%s' would build size %d cities. " + "City sizes must be from 1 to %d.", + utype_rule_name(putype), putype->city_size, + MAX_CITY_SIZE); + ok = FALSE; + } else { + /* Set to the default value for consistency */ + rscompat_set_valid_value(compat, &putype->city_size, 1, + "city_size", "unit type", + utype_rule_name(putype)); + } + } } unit_type_iterate_end; memset(&els, 0, sizeof(els)); diff --git a/server/ruleset.c b/server/ruleset.c index e805d89321..607906a51b 100644 --- a/server/ruleset.c +++ b/server/ruleset.c @@ -2533,10 +2533,13 @@ static bool load_ruleset_units(struct section_file *file, break; } - if (utype_has_flag(u, UTYF_SETTLERS) - && u->city_size <= 0) { - ruleset_error(LOG_ERROR, "\"%s\": Unit %s would build size %d cities", - filename, utype_rule_name(u), u->city_size); + if ((u->city_size <= 0 || MAX_CITY_SIZE <= u->city_size) + && (!compat->compat_mode || compat->version < RSFORMAT_3_2)) { + ruleset_error(LOG_ERROR, + "\"%s\": Unit %s city_size is %d. " + "The valid range is from 1 to %d.", + filename, utype_rule_name(u), u->city_size, + MAX_CITY_SIZE); u->city_size = 1; ok = FALSE; break; -- 2.34.1