Project

General

Profile

Feature #265 ยป 0024-Unhardcode-wld.map-from-unit_order_list_is_sane.patch

Marko Lindqvist, 02/16/2024 11:59 PM

View differences:

common/city.c
}
/**********************************************************************//**
Fill city rally point from the packet.
Fill city rally point from a packet.
**************************************************************************/
void city_rally_point_receive(const struct packet_city_rally_point *packet,
struct city *pcity)
......
pcity->rally_point.orders = NULL;
}
} else {
checked_orders = create_unit_orders(packet->length, packet->orders);
checked_orders = create_unit_orders(&(wld.map),
packet->length, packet->orders);
if (!checked_orders) {
pcity->rally_point.length = 0;
log_error("invalid rally point orders for %s.",
common/unit.c
/**********************************************************************//**
Returns TRUE iff the unit order array is sane.
**************************************************************************/
bool unit_order_list_is_sane(int length, const struct unit_order *orders)
bool unit_order_list_is_sane(const struct civ_map *nmap,
int length, const struct unit_order *orders)
{
int i;
......
paction = action_by_number(orders[i].action);
/* Validate main target. */
if (index_to_tile(&(wld.map), orders[i].target) == NULL) {
if (index_to_tile(nmap, orders[i].target) == NULL) {
log_error("at index %d, invalid tile target %d for the action %d.",
i, orders[i].target, orders[i].action);
return FALSE;
......
Sanity-check unit order arrays from a packet and create a unit_order array
from their contents if valid.
**************************************************************************/
struct unit_order *create_unit_orders(int length,
struct unit_order *create_unit_orders(const struct civ_map *nmap,
int length,
const struct unit_order *orders)
{
struct unit_order *unit_orders;
if (!unit_order_list_is_sane(length, orders)) {
if (!unit_order_list_is_sane(nmap, length, orders)) {
return NULL;
}
common/unit.h
cargo_iter_sizeof, cargo_iter_init, _ptrans)
#define unit_cargo_iterate_end generic_iterate_end
bool unit_order_list_is_sane(int length, const struct unit_order *orders);
struct unit_order *create_unit_orders(int length,
bool unit_order_list_is_sane(const struct civ_map *nmap,
int length, const struct unit_order *orders);
struct unit_order *create_unit_orders(const struct civ_map *nmap,
int length,
const struct unit_order *orders);
enum gen_action activity_default_action(enum unit_activity act);
server/savegame/savegame2.c
players_iterate(pplayer) {
unit_list_iterate_safe(pplayer->units, punit) {
if (!unit_order_list_is_sane(punit->orders.length,
if (!unit_order_list_is_sane(&(wld.map), punit->orders.length,
punit->orders.list)) {
log_sg("Invalid unit orders for unit %d.", punit->id);
free_unit_orders(punit);
server/savegame/savegame3.c
players_iterate(pplayer) {
unit_list_iterate_safe(pplayer->units, punit) {
if (!unit_order_list_is_sane(punit->orders.length,
if (!unit_order_list_is_sane(&(wld.map), punit->orders.length,
punit->orders.list)) {
log_sg("Invalid unit orders for unit %d.", punit->id);
free_unit_orders(punit);
server/unithand.c
{
int length = packet->length;
struct unit *punit = player_unit_by_number(pplayer, packet->unit_id);
struct tile *src_tile = index_to_tile(&(wld.map), packet->src_tile);
const struct civ_map *nmap = &(wld.map);
struct tile *src_tile = index_to_tile(nmap, packet->src_tile);
struct unit_order *order_list;
#ifdef FREECIV_DEBUG
int i;
......
}
if (length) {
order_list = create_unit_orders(length, packet->orders);
order_list = create_unit_orders(nmap, length, packet->orders);
if (!order_list) {
log_error("received invalid orders from %s for %s (%d).",
player_name(pplayer), unit_rule_name(punit), packet->unit_id);
......
}
if (!packet->repeat) {
punit->goto_tile = index_to_tile(&(wld.map), packet->dest_tile);
punit->goto_tile = index_to_tile(nmap, packet->dest_tile);
} else {
/* Make sure that no old goto_tile remains. */
punit->goto_tile = NULL;
    (1-1/1)