make var_copy_regions not leaak (coverity)

This commit is contained in:
Enno Rehling 2020-08-16 21:17:23 +02:00
parent 740f4a568b
commit b4fb721db4
2 changed files with 34 additions and 18 deletions

View File

@ -1434,24 +1434,17 @@ enum {
TRAVEL_RUNNING
};
static arg_regions *var_copy_regions(const region_list * begin, int size)
static void var_create_regions(arg_regions *dst, const region_list * begin, int size)
{
const region_list *rsrc;
int i;
if (size > 0) {
int i = 0;
arg_regions *dst;
assert(size > 0);
dst = (arg_regions *)malloc(sizeof(arg_regions) + sizeof(region *) * (size_t)size);
assert_alloc(dst);
dst->nregions = size;
dst->regions = (region **)(dst + 1);
for (rsrc = begin; i != size; rsrc = rsrc->next) {
dst->regions[i++] = rsrc->data;
dst->regions = (region **) malloc(sizeof(region *) * (size_t)size);
assert_alloc(dst->regions);
for (i = 0, rsrc = begin; i != size; rsrc = rsrc->next, ++i) {
dst->regions[i] = rsrc->data;
}
return dst;
}
return NULL;
}
static const region_list *travel_route(unit * u,
@ -1612,9 +1605,14 @@ static const region_list *travel_route(unit * u,
/* Berichte ueber Durchreiseregionen */
if (mode != TRAVEL_TRANSPORTED) {
arg_regions *ar = var_copy_regions(route_begin, steps - 1);
arg_regions *arp = NULL;
if (steps > 1) {
arg_regions ar;
arp = &ar;
var_create_regions(arp, route_begin, steps - 1);
}
ADDMSG(&u->faction->msgs, msg_message("travel",
"unit mode start end regions", u, walkmode, r, current, ar));
"unit mode start end regions", u, walkmode, r, current, arp));
}
mark_travelthru(u, r, route_begin, iroute);

View File

@ -1761,9 +1761,27 @@ static void var_free_resources(variant x)
x.v = 0;
}
static variant var_copy_regions(variant x)
{
arg_regions *src = (arg_regions *)x.v;
if (src) {
arg_regions *dst;
x.v = dst = malloc(sizeof(arg_regions));
dst->nregions = src->nregions;
src->nregions = 0;
dst->regions = src->regions;
src->regions = NULL;
}
return x;
}
static void var_free_regions(variant x) /*-V524 */
{
free(x.v);
arg_regions *arg = (arg_regions *)x.v;
if (arg) {
free(arg->regions);
}
}
const char *trailinto(const region * r, const struct locale *lang)
@ -2388,7 +2406,7 @@ void register_reports(void)
register_argtype("order", var_free_order, var_copy_order, VAR_VOIDPTR);
register_argtype("resources", var_free_resources, var_copy_resources, VAR_VOIDPTR);
register_argtype("items", var_free_resources, var_copy_items, VAR_VOIDPTR);
register_argtype("regions", var_free_regions, NULL, VAR_VOIDPTR);
register_argtype("regions", var_free_regions, var_copy_regions, VAR_VOIDPTR);
/* register functions that turn message contents to readable strings */
add_function("alliance", &eval_alliance);