Skip to content

Commit

Permalink
alternatives: ensure the current group is freed
Browse files Browse the repository at this point in the history
... even if the function fails before the group is fully initialized
(and numGroups is incremented).

Follow-up-for: 3842524
  • Loading branch information
dtardon committed May 21, 2024
1 parent 7b109ba commit 08a5ab3
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions alternatives.c
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ static int readConfig(struct alternativeSet *set, const char *title,
struct {
char *facility;
char *title;
} *groups = NULL;
} *groups = NULL, newGroup = {};
int numGroups = 0;
char linkBuf[PATH_MAX];
int r = 0;
Expand Down Expand Up @@ -364,8 +364,7 @@ static int readConfig(struct alternativeSet *set, const char *title,
goto finish;
}

groups = realloc(groups, sizeof(*groups) * (numGroups + 1));
groups[numGroups].title = strsteal(&line);
newGroup.title = strsteal(&line);

nextLine(&buf, &line);
if (!line || !*line) {
Expand All @@ -374,7 +373,11 @@ static int readConfig(struct alternativeSet *set, const char *title,
goto finish;
}

groups[numGroups++].facility = strsteal(&line);
newGroup.facility = strsteal(&line);

groups = realloc(groups, sizeof(*groups) * (numGroups + 1));
groups[numGroups++] = newGroup;
newGroup.title = newGroup.facility = NULL;

nextLine(&buf, &line);
}
Expand Down Expand Up @@ -517,6 +520,8 @@ static int readConfig(struct alternativeSet *set, const char *title,
free(groups[i].facility);
}
free(groups);
free(newGroup.title);
free(newGroup.facility);
free(line);
return r;
}
Expand Down

0 comments on commit 08a5ab3

Please sign in to comment.