Skip to content

Commit

Permalink
Review: Fix leaks in types compilation (done right)
Browse files Browse the repository at this point in the history
  • Loading branch information
wismill committed Nov 6, 2023
1 parent 47e0094 commit 537525f
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/xkbcomp/types.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ static void
ClearKeyTypesInfo(KeyTypesInfo *info)
{
free(info->name);
KeyTypeInfo *type;
darray_foreach(type, info->types)
ClearKeyTypeInfo(type);
darray_free(info->types);
}

Expand Down Expand Up @@ -180,11 +183,6 @@ MergeIncludedKeyTypes(KeyTypesInfo *into, KeyTypesInfo *from,
{
if (from->errorCount > 0) {
into->errorCount += from->errorCount;
// Free unused types
KeyTypeInfo *type;
darray_foreach(type, from->types) {
ClearKeyTypeInfo(type);
}
return;
}

Expand All @@ -197,6 +195,7 @@ MergeIncludedKeyTypes(KeyTypesInfo *into, KeyTypesInfo *from,

if (darray_empty(into->types)) {
into->types = from->types;
/* Types stealen via shallow copy, so reinitialze the array */
darray_init(from->types);
}
else {
Expand All @@ -206,6 +205,9 @@ MergeIncludedKeyTypes(KeyTypesInfo *into, KeyTypesInfo *from,
if (!AddKeyType(into, type, false))
into->errorCount++;
}
/* Types were either shallow copied or reinitialized individually
in `AddKeyType`, so we only need to free the array */
darray_free(from->types);
}
}

Expand Down Expand Up @@ -635,16 +637,16 @@ HandleKeyTypeDef(KeyTypesInfo *info, KeyTypeDef *def, enum merge_mode merge)
.level_names = darray_new(),
};

if (!HandleKeyTypeBody(info, def->body, &type)) {
info->errorCount++;
return false;
}

if (!AddKeyType(info, &type, true)) {
if (!HandleKeyTypeBody(info, def->body, &type) ||
!AddKeyType(info, &type, true))
{
info->errorCount++;
ClearKeyTypeInfo(&type);
return false;
}

/* Type has been either stolen via shallow copy or reinitialized in
`AddKeyType`: no need to free the arrays */
return true;
}

Expand Down

0 comments on commit 537525f

Please sign in to comment.