Project

Profile

Help

HostedRedmine.com has moved to the Planio platform. All logins and passwords remained the same. All users will be able to login and use Redmine just as before. *Read more...*

Feature #871002 ยป 0004-Factor-out-unit-order-list-sanity-checks.patch

Sveinung Kvilhaugsvik, 2020-04-27 09:33 AM

View differences:

server/unittools.c
}
/**********************************************************************//**
Sanity-check unit order arrays from a packet and create a unit_order array
from their contents if valid.
Returns TRUE iff the unit order array is sane.
**************************************************************************/
struct unit_order *create_unit_orders(int length,
const struct unit_order *orders)
bool unit_order_list_is_sane(int length, const struct unit_order *orders)
{
int i;
struct unit_order *unit_orders;
for (i = 0; i < length; i++) {
if (orders[i].order > ORDER_LAST) {
log_error("invalid order %d at index %d", orders[i].order, i);
return NULL;
return FALSE;
}
switch (orders[i].order) {
case ORDER_MOVE:
case ORDER_ACTION_MOVE:
if (!map_untrusted_dir_is_valid(orders[i].dir)) {
log_error("in order %d, invalid move direction %d.", i, orders[i].dir);
return NULL;
return FALSE;
}
break;
case ORDER_ACTIVITY:
......
/* Only allowed as the last order. */
log_error("activity %d is not allowed at index %d.", orders[i].activity,
i);
return NULL;
return FALSE;
}
break;
case ACTIVITY_BASE:
......
EC_BASE)) {
log_error("at index %d, %s isn't a base.", i,
extra_rule_name(extra_by_number(orders[i].sub_target)));
return NULL;
return FALSE;
}
break;
case ACTIVITY_GEN_ROAD:
......
EC_ROAD)) {
log_error("at index %d, %s isn't a road.", i,
extra_rule_name(extra_by_number(orders[i].sub_target)));
return NULL;
return FALSE;
}
break;
/* Not supported yet. */
......
case ACTIVITY_LAST:
case ACTIVITY_UNKNOWN:
log_error("at index %d, unsupported activity %d.", i, orders[i].activity);
return NULL;
return FALSE;
}
if (orders[i].sub_target == EXTRA_NONE
......
* this activity. */
log_error("activity %d at index %d requires target.", orders[i].activity,
i);
return NULL;
return FALSE;
}
break;
......
if (!action_id_exists(orders[i].action)) {
/* Non-existent action. */
log_error("at index %d, the action %d doesn't exist.", i, orders[i].action);
return NULL;
return FALSE;
}
if (action_id_distance_inside_max(orders[i].action, 2)) {
......
* go_act_menu::create(). */
log_error("at index %d, the action %s isn't supported in unit "
"orders.", i, action_id_name_translation(orders[i].action));
return NULL;
return FALSE;
}
if (!action_id_distance_inside_max(orders[i].action, 1)
......
/* Actor must be on the target tile. */
log_error("at index %d, cannot do %s on a neighbor tile.", i,
action_id_rule_name(orders[i].action));
return NULL;
return FALSE;
}
/* Validate sub target. */
......
/* Sub target is invalid. */
log_error("at index %d, cannot do %s without a target.", i,
action_id_rule_name(orders[i].action));
return NULL;
return FALSE;
}
break;
case ASTK_TECH:
......
/* Target tech is invalid. */
log_error("at index %d, cannot do %s without a target.", i,
action_id_rule_name(orders[i].action));
return NULL;
return FALSE;
}
break;
case ASTK_EXTRA:
......
/* Target extra is invalid. */
log_error("at index %d, cannot do %s without a target.", i,
action_id_rule_name(orders[i].action));
return NULL;
return FALSE;
}
break;
case ASTK_NONE:
......
case ASTK_COUNT:
fc_assert_ret_val_msg(
action_id_get_sub_target_kind(orders[i].action) != ASTK_COUNT,
NULL,
FALSE,
"Bad action %d in order number %d.", orders[i].action, i);
}
......
}
}
return TRUE;
}
/**********************************************************************//**
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,
const struct unit_order *orders)
{
struct unit_order *unit_orders;
if (!unit_order_list_is_sane(length, orders)) {
return NULL;
}
unit_orders = fc_malloc(length * sizeof(*(unit_orders)));
memcpy(unit_orders, orders, length * sizeof(*(unit_orders)));
server/unittools.h
void unit_get_goods(struct unit *punit);
bool unit_order_list_is_sane(int length, const struct unit_order *orders);
struct unit_order *create_unit_orders(int length,
const struct unit_order *orders);
    (1-1/1)