Skip to content

Commit

Permalink
alternatives: fix setting the best alternatives
Browse files Browse the repository at this point in the history
We called set->best = set->numAlts; after we already increased
set->numAlts. This caused the best alternative to be set to the
next alternative in the array, not the current one.
  • Loading branch information
lnykryn committed May 30, 2024
1 parent 8f06345 commit e10ca32
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions alternatives.c
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ static int readConfig(struct alternativeSet *set, const char *title,
set->alts = NULL;
set->numAlts = 0;
set->mode = AUTO;
set->best = 0;
set->best = -1;
set->current = -1;

path = alloca(strlen(stateDir) + strlen(title) + 2);
Expand Down Expand Up @@ -486,11 +486,13 @@ static int readConfig(struct alternativeSet *set, const char *title,
}

set->alts = realloc(set->alts, (set->numAlts + 1) * sizeof(*set->alts));
set->alts[set->numAlts++] = newAlt;
set->alts[set->numAlts] = newAlt;

if (newAlt.priority > set->alts[set->best].priority)
if (set->best == -1 || newAlt.priority > set->alts[set->best].priority)
set->best = set->numAlts;

set->numAlts++;

memset(&newAlt, 0, sizeof(struct alternative));

nextLine(&buf, &line);
Expand Down

0 comments on commit e10ca32

Please sign in to comment.