]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
depmod: Prevent undefined behavior
authorTobias Stoeckmann <tobias@stoeckmann.org>
Fri, 18 Oct 2024 15:37:40 +0000 (17:37 +0200)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Tue, 22 Oct 2024 16:52:48 +0000 (11:52 -0500)
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 <tobias@stoeckmann.org>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/193
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
tools/depmod.c

index 3328103e24c72cbc96e168ef3c1e3f1e629cf43c..42cc0a2133ad0195ceab528082c1c6326e8449c9 100644 (file)
@@ -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: