Skip to content

Commit

Permalink
depmod: Prevent undefined behavior
Browse files Browse the repository at this point in the history
Calling qsort with NULL argument is invalid, although size 0 would
prevent anything bad from happening. Make sure that UBSAN is not
triggered.

Signed-off-by: Tobias Stoeckmann <[email protected]>
  • Loading branch information
stoeckmann committed Oct 18, 2024
1 parent 20cc140 commit 2261971
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions tools/depmod.c
Original file line number Diff line number Diff line change
Expand Up @@ -1494,10 +1494,12 @@ static void depmod_modules_sort(struct depmod *depmod)
mod->sort_idx = i++;
}

array_sort(&depmod->modules, mod_cmp);
for (idx = 0; idx < depmod->modules.count; idx++) {
struct mod *m = depmod->modules.array[idx];
m->idx = idx;
if (depmod->modules.count > 0) {
array_sort(&depmod->modules, mod_cmp);
for (idx = 0; idx < depmod->modules.count; idx++) {
struct mod *m = depmod->modules.array[idx];
m->idx = idx;
}
}

corrupted:
Expand Down Expand Up @@ -2024,7 +2026,8 @@ static bool mod_get_all_sorted_dependencies(const struct mod *mod, struct array
if (mod_fill_all_unique_dependencies(mod, array) < 0)
return false;

array_sort(array, dep_cmp);
if (array->count > 1)
array_sort(array, dep_cmp);
return true;
}

Expand Down

0 comments on commit 2261971

Please sign in to comment.