From d056ac221b2b944307b561bbfc35e5fb3f782e1d Mon Sep 17 00:00:00 2001 From: Tobias Stoeckmann Date: Fri, 18 Oct 2024 17:37:40 +0200 Subject: [PATCH] depmod: Prevent undefined behavior 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 Reviewed-by: Emil Velikov Link: https://github.com/kmod-project/kmod/pull/193 Signed-off-by: Lucas De Marchi --- tools/depmod.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tools/depmod.c b/tools/depmod.c index 3328103e..42cc0a21 100644 --- a/tools/depmod.c +++ b/tools/depmod.c @@ -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 > 1) { + 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: -- 2.47.3