Fix memory leaks in key types compilation

When there is no error the types are “stolen” and copied to the keymap.
But when there is an error, `MergeIncludedKeyTypes` just return without
“stealing” nor freeing the types.

Fixed by explicitly freeing the key types.

Fixed another leak in `HandleKeyTypeDef` that may occur if there is an
error in parsing a type definition.
master
Pierre Le Marre 2023-10-25 20:01:22 +02:00 committed by Wismill
parent 0f9c95df06
commit 2b71431007
1 changed files with 13 additions and 6 deletions

View File

@ -118,6 +118,9 @@ static void
ClearKeyTypesInfo(KeyTypesInfo *info) ClearKeyTypesInfo(KeyTypesInfo *info)
{ {
free(info->name); free(info->name);
KeyTypeInfo *type;
darray_foreach(type, info->types)
ClearKeyTypeInfo(type);
darray_free(info->types); darray_free(info->types);
} }
@ -192,6 +195,7 @@ MergeIncludedKeyTypes(KeyTypesInfo *into, KeyTypesInfo *from,
if (darray_empty(into->types)) { if (darray_empty(into->types)) {
into->types = from->types; into->types = from->types;
/* Types stolen via shallow copy, so reinitialize the array */
darray_init(from->types); darray_init(from->types);
} }
else { else {
@ -201,6 +205,9 @@ MergeIncludedKeyTypes(KeyTypesInfo *into, KeyTypesInfo *from,
if (!AddKeyType(into, type, false)) if (!AddKeyType(into, type, false))
into->errorCount++; into->errorCount++;
} }
/* Types were either shallow copied or reinitialized individually
in `AddKeyType`, so we only need to free the array */
darray_free(from->types);
} }
} }
@ -630,16 +637,16 @@ HandleKeyTypeDef(KeyTypesInfo *info, KeyTypeDef *def, enum merge_mode merge)
.level_names = darray_new(), .level_names = darray_new(),
}; };
if (!HandleKeyTypeBody(info, def->body, &type)) { if (!HandleKeyTypeBody(info, def->body, &type) ||
info->errorCount++; !AddKeyType(info, &type, true))
return false; {
}
if (!AddKeyType(info, &type, true)) {
info->errorCount++; info->errorCount++;
ClearKeyTypeInfo(&type);
return false; return false;
} }
/* Type has been either stolen via shallow copy or reinitialized in
`AddKeyType`: no need to free the arrays */
return true; return true;
} }